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 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.
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
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.
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?
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.
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).
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