# C++ Sum & Average not working

Discussion in 'Software Development' started by aushaheed, Sep 6, 2001.

Not open for further replies.

Joined:
Sep 6, 2001
Messages:
85
Hi:
can anyone figure out why the sum and average isn't displaying based on what the user enters? it runs and i get values of 0 for each sum and average...any help by MIDNIGHT WOULD BE GREAT!

Code:

#include<iostream.h>

int main()
{
int y, i;
double g, m=0, a;
cout << "Please enter number of values to be processed: ";
cin >> y;//receiving number of values
cout << "Please enter the " << y << " values:\n"; //asking for those values
a = m/y;
for(i = 1; i <= y; i++)
{

cin >> g;
g += m;
}

cout << "Sum: " << m << "\n"; cout << "Average: " << a << "\n";
cout << "Thank you come again!";
return 0;
}

2. ### MacFromOK

Joined:
Mar 18, 2001
Messages:
1,741
Here ya go.

BTW, I suggest using var names that
give you an idea of what you're doing,
such as "total", "numentries", etc.

int main()
{
int y, i;
double g, m=0, a;
cout << "Please enter number of values to be processed: ";
cin >> y;//receiving number of values
cout << "Please enter the " << y << " values:\n"; //asking for those values
for(i = 1; i <= y; i++)
{
cin >> g;
m += g; // Add entry (g) to total (m).
}
a = m/y; // divide m (total) by y (number of entries)
cout << "Sum: " << m << "\n"; cout << "Average: " << a << "\n";
cout << "Thank you come again!";
return 0;
}

Hope I'm not doing somebody's homework here...

As Seen On