Hi i am a beginner in c++ and just start learning array, i am not able to output the average any idea why ???

Hi i am a beginner in c++ and just start learning array, i am not able to output the average any idea why ???

#include<iostream>
using namespace std;
int main ()
{
const int MAX=200;
int age[MAX],i=0,size,sum,avg,highest=0,lowest=9999;
cout<<"Please enter ages:< type 0 to endl!> \n";

cin>>age[i];
while (age[i] != 0 )
{
i++;
cin>>age[i];
sum= sum +age[i];
avg=1.0*sum/avg;
}

size=i;

cout<<size<<" ages entered: ";
for (int i=0;i<size;i++)

cout<<"The average of all students is "<< avg;
cout<<endl;

return 0;
}
1. check the initial values of your variables. e.g. if you are calculating a sum you need a starting value!

2. you have code missing. e.g. the o/p of the ages.

Andy

P.S. I am a menber of the "one variable declaration per line" school. This makes it a lot easier to see what's going on!
Okay thanks.
However, now i got a question on how to show number of age>20, any idea how ??
Sorry, but is that a serious question?

Ans: you count them!

Sort out your display loop. Once you can loop through you result you'll be able to do other things to, like counting the number of entries which are older than 20.
Topic archived. No new replies allowed.