Averaging Ques?

I was wondering, what is wrong with my code....I want to input 10 ints and return the sum and avergage to the screen...

main()

{

constint num = 10;

int sum;

cout << "Enter 10 numbers" << endl;

for (int item = 0; item < num; item++)

sum += num;

cin.ignore();

cout << "The sum is"<< sum <<endl;

cout << "The average is" << sum/num << endl;

cin.ignore();

return 0;

}

[1332 byte] By [Dpowers] at [2007-12-16]
# 1

sum+=num doesn't seem correct since that is always 10 and hence you are not actually adding the input numbers. Also, make sure you initialize sum before using it.

Here is a small sample that gets the input of 10 different numbers from the user and calculates the sum in integer and float values and dsiplay such values to the screen.

#include <iostream>
using namespace std;

int main()

{

const int num = 10;

int sum = 0;
float exact_sum = 0;
int input = 0;

cout << "Enter 10 numbers" << endl;

for (int item = 0; item < num; item++)
{
cin >> input;
sum += input;
exact_sum+=input;
}

cout << "The sum is "<< sum <<endl;
cout << "The integer average is " << sum/num << endl;
cout << "The exact average is " << exact_sum/num << endl;

return 0;
}

The output will look something like the following:
Enter 10 numbers
1
2
3
4
5
6
7
8
9
10
The sum is 55
The integer average is 5
The exact average is 5.5

Hope this helps!

Thanks,
Ayman shoukry
VC++

AymanShoukry at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...
# 2
ThanksBig Smile
Dpowers at 2007-9-9 > top of Msdn Tech,Visual C++,Visual C++ General...