a challenge for me and maybe you Variance!!!

hi there!this program is suppose to read students number and saves it in a array
and then calculate A Variance but the answer is wrong!!!

[code]
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <fstream>
#include <math.h>

using namespace std;
void main()
{

int g[4][30],ava[4][3]={0},sum=0,j=0,n;

for(int i=0;i<4;i++)
{
cout<<"Numer of students : ";
cin>>n;

cout<<"\t\t\t\tCLASS "<<i+1<<endl;
sum=0;
for(int j=0;j<n;j++)
{
cout<<"Enter grades : ";
cin>>g[i][j];
sum+=g[i][j];
ava[i][0]=sum/n;

}
ava[i][1]+=( (ava[i][0]-g[i][j])*(ava[i][0]-g[i][j]) );
ava[i][1]=ava[i][1]/n;

ava[i][2]=sqrt(ava[i][1]);
}


cout<<" \n variance class 1 : "<<ava[0][1]<<"\n variance class 2 : "<<ava[1][1]<<"\n variance class 3 : "<<ava[2][1]<<"\n variance class 4 : "<<ava[3][1]<<endl;

Hi there, I think the problem can be in your divide statement. Don't forget that in C++ when you divide an int by an int, you get an int. For example: 5/3 = 1
And this is precisely what you do !

One way to do is to multiply by 1.0, for example: 5*1.0/3 = 1.666666667

Hope this helps !
move this line

ava[i][0]=sum/n;

out of the inner j loop
Topic archived. No new replies allowed.