average of numbers entered by users

hello everyone. i got a question here.. i need to create a program to prompt the user to enter the values and find the average of these values.. can u guys have a look on it and gives some advises to solve this problems...

#include <iostream>
using namespace std;

int main()
{
int n,average;
cout<<"Enter the number of values"<<endl;
cin>>n;
for(int i=1;i<=n;i++)
{
int average,value;
cin>>value;
value+=value;
average=value/n;
cout<<average<<endl;
}
system("pause");
return 0;
}


the program can execute but the output is always zero, can u correct it for me? thanks:)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    int n;
    double average(0); // <- NOTE
    std::cout << "Enter the number of values" << std::endl;
    std::cin >> n;
    for(int i = 0; i < n; ++i) {
        int value;
        std::cin >> value;
        average += value;
    }
    average /= n;
    std::cout << average;
}
closed account (3qX21hU5)
Here is what you need to do.

Step 1: You need 3 variables. A int input; to hold the number the user enters. A int sum; to hold the sum of all the numbers the user entered. A int average; to hold the average of all the numbers.

Step 2: Get user input in a loop, if you want to get 10 numbers you can do something like this for (int i = 0; i != 10; ++i). That will loop through 10 times. Now every time through the loop you should ask the user for a number, store that number in the input variable. After you do that you need to add whatever the user entered to the sum variable like so sum += input;.

Step 3: After the user is done entering numbers it is time to find the average. This is very easy actually. All we need to do is divide the variable sum by the number of numbers entered by the user which in the case above is 10. So average = sum / 10;

And boom we have 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
// How many numbers to enter
const int numToEnter = 10;

int main()
{
    int sum = 0;
    int average = 0;
    int input;
    
    // Get 10 numbers from the user
    for (int i = 0; i != numToEnter; ++i)
    {
        cout << "Enter a number: ";
        cin >> input;
        
        sum += input;
    }
    
    // Get average and print it
    average = sum / numToEnter;
    cout << "The average is: " << average << endl;
    
    return 0;
}
Last edited on
sorry.. can you explain where section that i done wrong... please further explain...thanks.
First: You don't say what is the value of 'average'. You should make it 0;
int average = 0;

Second: It will be better if in the loop you sum average + value.
average += value;

Third: You can output the result in the end by just writing:
cout << average/n

And why do you declare average two times? Better declare all your variables in the beginning, like you have made with 'n':
int n, value, average = 0;

So the program will be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{
   int n, value, average = 0;
   cout << "Enter the number of values" << endl;
   cin >> n;
   for(int i=1;i<=n;i++)
     {
        cin >> value;
        average+=value;
    }
    cout << average/n << endl;
    system("pause");
    return 0;
}
closed account (3qX21hU5)
@OP What more is needed to explain both Miini and I have gave you examples and walked you through exactly what you need to do....

@Snaksa

Better declare all your variables in the beginning, like you have made with 'n':

It is actually easier to read the program if you declare variables right before you use them.

Third: You can output the result in the end by just writing:
cout << average/n


That code is incorrect and will generate a error. I think you were looking for cout << average << "\n"; or even better cout << average << endl;


Snaksa wrote:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
   int n, value, average = 0;
   cout << "Enter the number of values" << endl;
   cin >> n;
   for(int i=1;i<=n;i++)
     {
        cin >> value;
        average+=value;
    }
    cout << average/n << endl;
    system("pause");
    return 0;
}


Is wrong you never find the average you just print out the sum...
Last edited on
average/n
He is dividing average by n (number of inputs). However there is integer division here, so result will be unprecise.
thanks everyone!! all of your helping are really appreciated^^
Yes, you are right that it's better to declare them right before using them, but this is a very simple program.

This is outputting average(which contains the sum of all values) divided by 'n'(the number of the values). It's working when I run it. Check again.
 
cout << average/n << endl;


@MiiNiPaa, you are right. It would be better to use double
Last edited on
closed account (3qX21hU5)
Gezz you guys really need better naming conventions ;p it looked like you were trying to do a newline totally few over my head he was dividing. That is also another reason why I like to put spaces between operators and operands.
Topic archived. No new replies allowed.