I have to make a program for my C class and im stuck on the
I have to make a program for my C class and im stuck on the part where i have to calculate the variance with elements from my array. This is exactly what im suppose to do:
Reset the sum variable to 0
Using another for loop (which needs its own loop variable), run through all valid elements of the array, starting from index 0. Inside the loop:
Use the sum variable to accumulate the numerator of the variance formula shown on page 1.
After the end of this loop, compute the variance by dividing the new sum by the dataElementsToProcess parameter.
this is the entire function
bool GenerateStatistics(FILE *fp2, double myArray[], int dataElementsToProcess)
{
if (fp2 == NULL && myArray == NULL && dataElementsToProcess < 1)
{
return false;
}
else
{
int i = 0,o = 0;
int sum = 0, sum1 = 0;
double maxValue = 0;
double minValue = DBL_MAX;
double mean, variance, stdDeviation;
for (int i = 0; i < 600; i++)
{
sum += myArray[i];
if (myArray[i] > maxValue)
{
maxValue = myArray[i];
}
if (myArray[i] < minValue)
{
minValue = myArray[i]; }
}
mean = sum / dataElementsToProcess;
sum = 0;
for (int o = 0; o < 600; o++)
{
sum += myArray[o];
}
variance = sqrt(pow((sum - mean), 2) / (dataElementsToProcess));
stdDeviation = sqrt(variance);
fprintf(fp2, \"Reaction Rate Statistics:\ \ \");
fprintf(fp2, \"Number of experiments = %d\ \", dataElementsToProcess);
fprintf(fp2, \"Minimum rate\\t\\t= %f\ \", minValue);
fprintf(fp2, \"Maximum rate\\t\\t= %f\ \", maxValue);
fprintf(fp2, \"Arithmetic mean\\t\\t= %f\ \", mean);
fprintf(fp2, \"Variance\\t\\t= %f\ \", variance);
fprintf(fp2, \"Standard deviation\\t= %f\ \ \ \", stdDeviation);
}
return true;
}
Solution
Here in the function you have
average - mean
Noofelements - dataElementsTo Process
Variance formula is
V = summation over i=1 to n ((xi - mean )*(xi - mean))
So perform like this using a for loop over my_array
double V=0.0
for(int i=0;i<600;i++)
{
V=V+pow((my_Array[i]-mean),2)
}
Now calculate standard deviation using sqrt of variance
sd=0.0
sd=sqrt(V)

