A code to claculate the average !

I'm trying to answer this question on Spoj ,, It's about claculating the average of numbers witth many test cases..
This is all i could write in such code,, It works with only the 1st test case,,but doesn't with the rest!!
don't know why!!
can I get some help with this code please???

Here is the problem : http://www.spoj.pl/problems/AVG/

and here's my answer:

#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{int t;

int sum=0;
cin>>t;
for(int i=0;i<t;i++)
{int n;
cin>>n;
for(int j=0;j<n;j++)
{int x;
cin>>x;
sum=sum+x;
}
int avg=0;
avg=sum/n;
cout<<avg<<endl;

}

system("PAUSE");
return 0;
}

thanx in advance :)
Your code is hard to read..
You declared sum=0 only at the beginning, which means after the first test sum is not reset so it adds up to the previous sums.
Topic archived. No new replies allowed.