Problem about getting the number greater than the average

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
using namespace std;
int main()
{
	int k,num,sum=0,GreaterNO=0;
	double average;

	cout<<"Enter how many numbers you want to enter:";
	cin>>k;

	for(int x=1;x<=k;x++)
	{
		cout<<"\nEnter an integer value:";
		cin>>num;
		
		sum+=num;
	}

	average=(double)sum/k;

	cout<<"\n\nThe average of the series of the numbers is "<<average<<endl;
	cout<<"The total of numbers greater than the average "<<GreaterNO<<endl;
		
	return 0;
}

above is my program,
I have received an assignment which the lecturer request us to find a series of k numbers then
-display the average
-display the total of numbers greater than the average

our lecturer request us do in 2 version, one is by using array, one is without using array, I have facing the coding problem when doing the VERSION WITHOUT USING ANY ARRAY to get the display the total numbers greater than the average, is there possible to get it?? because there are only one variable keep on looping, but the average will only get after get all the user input....??
ps. I have done my array version, but this version I really dun have any idea about it...
Last edited on
Recursion.
recursion?? can you further explain about it?? i not so familiar with it ....
sorry.... seriously cannot get the point and link to my problem......
>< sorry , my understanding maybe little bit...
What did you do in the array version? Was it:
1. Read the numbers one by one and tally up the sum.
2. Compute average
3. Look through the numbers again and tally up the count.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
using std::cin;
using std::cout;

void foo( int i )
{
  if ( 0 < i )
  {
      int num;
      cout<<"\nEnter an integer value:";
      cin >> num;
      
      foo( i - 1 );

      cout << "Pos " << i << " was " << num << '\n';
  }
  return;
}

int main()
{
  int k;
  cout<<"Enter how many numbers you want to enter:";
  cin>>k;

  foo( k );

  return 0;
}

The foo is recursive. It has the end condition on line 7 to prevent infinite recursion.
Each call reads one number and leaves the rest for the recursive call.
After those other calls have returned, it still remembers the value that it did read.

If k==4, there will be five function calls: foo(4), foo(3), foo(2), foo(1), and foo(0). Each has its own 'num' (except the foo(0) ).

The foo(0) basically just returns, but by that time all numbers have been read already. If you had been incrementing and passing a sum, you could calculate an average now.

Therefore, by the time these functions reach line 14, the average is known and can be used.

In your case you need to pass more information forth and back than foo() does.
i dun understand around the line 15, there are only one cout, but why it can display all the value of all num value
Last edited on
It doesn't, because there isn't. If we have:
1
2
3
4
5
void foo( int i = 4 )
{
  foo( i-1 );
  cout << i; // prints 4
}

Lets expand that line 3:
1
2
3
4
5
6
7
8
9
{
  int i = 4;
  {
    int j = i-1;
    foo( j-1 );
    cout << j; // prints 3
  }
  cout << i; // prints 4
}

Lets expand that line 5:
1
2
3
4
5
6
7
8
9
10
11
12
13
{
  int i = 4;
  {
    int j = i-1;
    {
      int k = j-1;
      foo( k-1 );
      cout << k; // prints 2
    }
    cout << j; // prints 3
  }
  cout << i; // prints 4
}

We should now expand the line 7, but do you start to see from this (pseudocode) example how recursion works?
Get the point dy, thx alot
Topic archived. No new replies allowed.