// my second project
//
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <fstream>
#include <math.h>
usingnamespace std;
void main()
{
int n[4];// number of class students
float grade[4][30],// grades
sum=0, // sum
avg[4], // average
var[4], // variance
SD[4], // std deviation
min = 0, // minimum
max = 0; // maximum
cout<<"\n In The Name Of Allah"
<<"\n ________________________________________________________\n";
cout<<"\n Rahmat programs\n"
<<" ________________________________________________________\n"
<<"\n in this program,you enter grade of 4 classes"
<<"\n and then program tells you with class is Best class or...\n";
cout<<"\n CLASSES STATUS"
<<" ________________________________________\n";
for( int i = 1 ; i <= 4 ; i++ )
{
cout<<"\n enter number of class "<<i<<" students ( <=30 ): ";
cin>>n[i];
}
system("cls");
///////////////////////////////////////////////////////////////// class 1
cout<<"\n\n Class 1 :"
<<"\n ________________\n";
for ( int i = 1 ; i <= n[1] ; i++ )
{
cout<<"\n enter grade : ";
cin>>grade[1][i];
sum += grade[1][i];
}
avg[1] = sum / n[1];
sum = 0 ;
for (int k =1 ;k <= n[1];k++)
{
sum += (( avg[1] - grade[1][k] ) * ( avg[1] - grade[1][k] ));
}
var[1]= sum / n[1];
SD[1] = sqrt(var[1]);
min = SD[1];
cout<<"\n average is : "<<avg[1]<<"\n variance is : "<<var[1]<<"\n std deviation is : "<<SD[1];
_getch();system("cls");
///////////////////////////////////////////////////////////////// class 2
cout<<"\n\n Class 2 :"
<<"\n ________________\n";
sum = 0 ;
for ( int i = 1 ; i <= n[2] ; i++ )
{
cout<<"\n enter grade : ";
cin>>grade[2][i];
sum += grade[2][i];
}
avg[2] = sum / n[2];
sum = 0 ;
for ( int k = 1 ; k <= n[2] ; k++ )
{
sum += ((avg[2] - grade[2][k] )* (avg[2] - grade[2][k] ));
}
var[2]= sum / n[2];
SD[2] = sqrt(var[2]);
cout<<"\n average is : "<<avg[2]<<"\n variance is : "<<var[2]<<"\n std deviation is : "<<SD[2];
_getch();system("cls");
///////////////////////////////////////////////////////////////// class 3
cout<<"\n\n Class 3 :"
<<"\n ________________\n";
sum = 0 ;
for ( int i = 1 ; i <= n[3] ; i++ )
{
cout<<"\n enter grade : ";
cin>>grade[3][i];
sum += grade[3][i];
}
avg[3] = sum / n[3];
sum = 0 ;
for ( int k = 1 ; k <= n[3] ; k++ )
{
sum += (( avg[3] - grade[3][k] ) * ( avg[3] - grade[3][k] ));
}
var[3]= sum / n[3];
SD[3] = sqrt (var[3] );
cout<<"\n average is : "<<avg[3]<<"\n variance is : "<<var[3]<<"\n std deviation is : "<<SD[3];
_getch();system("cls");
///////////////////////////////////////////////////////////////// class 4
cout<<"\n\n Class 4 :"
<<"\n ________________\n";
sum = 0 ;
for ( int i = 1 ; i <= n[4] ; i++ )
{
cout<<"\n enter grade : ";
cin>>grade [4][i];
sum += grade[4][i];
}
avg[4] = sum / n[4];
sum = 0 ;
for (int k = 1 ; k <= n[4] ; k++)
{
sum += ((avg[4] - grade[4][k] )* (avg[4] - grade[4][k] ));
}
var[4]= sum/n[4];
SD[4] = sqrt (var[4]);
cout<<"\n average is : "<<avg[4]<<"\n variance is : "<<var[4]<<"\n std deviation is : "<<SD[4];
_getch();system("cls");
///////////////////////////////////////////////////////////////// finding minimum average
cout<<"\n RESULTS_______________________________________\n\n";
for ( int i = 1 ; i <= 4 ; i++ )// for max
{
if ( avg[i] > max )
max = avg[i];
}
if ( max == avg[1] )
cout<<"\n Better Class is : Class 1 \n";
elseif ( max == avg[2] )
cout<<"\n Better Class is : Class 2 \n";
elseif ( max == avg[3] )
cout<<"\n Better Class is : Class 3 \n";
elseif ( max == avg[4] )
cout<<"\n Better Class is : Class 4 \n";
///////////////////////////////////////////////////////////////// finding minimum maximum of e.a.m.
for ( int i = 1 ; i <= 4 ; i++ )// for min
{
if ( SD[i] < min )
min = SD [i];
}
if ( min == SD[1] )
cout<<"\n The most consistent Class is : Class 1 \n";
elseif ( min == SD[2] )
cout<<"\n The most consistent Class is : Class 2 \n";
elseif ( min == SD[3] )
cout<<"\n The most consistent Class is : Class 3 \n";
elseif ( min == SD[4] )
cout<<"\n The most consistent Class is : Class 4 \n";
////////////////////////////////////////////////////////////////// finding Best class
if ( max == avg[1] && min == SD[1] )
cout<<"\n Best class is : Class 1";
elseif ( max == avg[2] && min == SD[2] )
cout<<"\n Best class is : Class 2";
elseif ( max == avg[3] && min == SD[3] )
cout<<"\n Best class is : Class 3";
elseif ( max == avg[4] && min == SD[4] )
cout<<"\n Best class is : Class 4";
else
cout<<"\n error !!! program can not find Best class ";
//////////////////////////////////////////////////////////////////
_getch();
}// main
That error usually means you are stepping outside of array bounds. And you are... just about everywhere.
Remember that array indexes start at ZERO not at one. So if you have an array with 4 elements... elements [0], [1], [2], and [3] are valid. [4] is not valid and is out of bounds.
So yeah... indexes should be 0-3, not 1-4 as they are in your code.