How to obtain the total of numbers greater than average without using an array

Pages: 12
Hi guys , im facing a problem which i could not obtain the total numbers which is greater than the average value. For example:

#include <iostream>
using namespace std;

int main ()
{
int size , count;
double no, max, min ,total, sum , average;
sum=0;
max=0;
min =0;
total =0;

cout << "How many number do you want to input? " ;
cin >> size;

cout << endl << endl;

cout << "Please input the numbers for number 1: " ;
cin >> no;

sum += no;
min = no;
max = no;

for(count =1 ; count < size ; count++)
{

cout << "Please input the numbers for number " << count+1 << ": ";
cin >> no;

sum += no;

if (max < no)
max = no;

if (min > no)
min = no;

}

average = sum/size;

cout << endl << endl;
cout << "The average of the series of numbers is: " << average << endl;
cout << "The largest number among the series is: " << max << endl;
cout << "The smallest number among the series is: " << min << endl;


}


In this case im able to compute the average of the numbers but when it comes to capture the total of numbers which is greater than the average value, im confuse and i have no idea how to compile the code , because the average number is only been compute once all the value capture by the input of user is sum up. Can someone please help me.
You'd need to save the numbers entered to determine which was greater than the overall average.

You can use an array like you started to in your other thread.
http://www.cplusplus.com/forum/general/139268/

You just need to make sure whatever number is used as the subscript to access the 0th, 1st, 2nd etc element of the array (which might contain a double, char, string, or other type) is defined as an int.

but the problem is in this version of program i cannot use an array and it is a request from my lecturer , so any suggestion please ?
Do you need to output actual numbers or just a count of numbers?
Ok , the problem is from the 1st version im using the array to store the data from the user input and then i can use to compare the data in array if it is greater than average number . But for the second version i cannot use the array to store the data ... so my problem is i cannot obtain the value i have been key in to compare the average value .... since i cannot use an array
Can you output the numbers entered to a file and then read them back in after computing the average?
ermm can you explain further , because i dont get it by output the numbers entered to a file and read them back
If they want you to output the actual numbers entered that were greater than the average, you need to save that information somewhere. You can't use an array, so what other ways would you have to store data? You don't know how many numbers are going to be entered until you ask the user. I suppose you could dynamically allocate variables, but maybe writing the data to a file instead of a variable is an option.

http://www.cplusplus.com/doc/tutorial/files/

Maybe there are some other creative ideas out there?
well thx for the suggestion ! I will get to you once if i face any problem
I think a solution with recursion is possible - is this what you are searching for and if so how far have you reached?
urm what is recursion , can you please explain because i went through other site about recursion and i still dont get it
recursion is the act of calling a function within itself, generally passing through different parameters such that a base case condition is reached and the function exits (ie. it does not call itself anymore).

here's the recursive solution - it was small but not too straight forward so decided to post - does it work for you?

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
30
31
32
33
34
35
36
using namespace std;


void avg(int nCnt, int nLO, double & fAvg, double fA);


int main()
{
   int nCnt;
   cout << "How many numbers do you want to input: ";
   cin >> nCnt;

   double fAvg = 0.0;
   avg(nCnt, nCnt, fAvg, 0.0);

   cout << "Avg = " << fAvg << endl;

	return 0;
}



void avg(int nCnt, int nLO, double & fAvg, double fA)
{
   fAvg += fA;
   if (!nLO)      return;

   int nVal;
   cout << "Enter Number: ";
   cin >> nVal;

   avg(nCnt, nLO-1, fAvg, nVal*1.0/nCnt);
   
   if (nVal > fAvg)      cout << nVal << " > " << fAvg << endl;
   else                  cout << nVal << " <= " << fAvg << endl;
}
Last edited on
just noticed that according to your question we should be outputting the amount of numbers inputted that is greater than the average but the above function does not - instead it outputs exactly which numbers were greater than the average and which were less.

i have already tested this function and can verify that it does work correctly, so the only step needed is to simply increment a counter for each number greater than the average.

hope this helps.
Wow thanks but can you please explain about the line 26 ,and why do the cout statement at line 29 keep repeating itself until the number of nCnt ?
line 26 is the base case condition that causes the avg function to exit.

the nLO variable is used as a "Left Over Counter".

we check if this is not equal to zero (ie. we still have some left over) in order to proceed otherwise we return.

line 26 can be written more explicitly as:

 
if (nLO != 0)      return;


or more correctly

 
if (nLO > 0)      return;


when we first call avg function we pass the nCnt value for this nLO parameter - thus at start nLO == nCnt

then each time we recall avg we decreament nLO by 1 as can be seen on line 32: nLO-1

this ensures that at some point the nLO value will become zero at which point our function will exit.

the cout on line 29 gets called nCnt number of times due to the function recalling itself on line 32 - this is the recursive call.

when the function calls itself again it starts from the top of avg (but with a new function stack ...) evaluates line 26 and if not zero will proceed to the cout statement.

once all the numbers have been entered, nLO will be zero and the avg function will exit (ie all its calls will exit and execute lines 34 and 35).
You cannot use an array doesn't mean you cannot use another data structure. A simple linked list could have worked as well
Is this an intro class? Has the professor covered recursion or linked lists?
well thx for the explanation SIK , but i think for line 26 your explanation about the
 if (nLO != 0) return; 
and also
if (nLO >0) return ;

should be :

if (nLO ==0)
or
 if (nLO <=0) 

because if our input for nCnt is 5 then the if statement will be true so it will exit the avg function immediately
Smac89 what you mean simple linked list ?
wildblue im sorry to said that our lecture never taught us yet about the recursion and also the input or output file so they expect us to learn by ourself :) and also thanks for guide me so far
Pages: 12