NO POINTERS TO BE USED. This is C programming.
I am trying to make a function to calculate standard deviation from
a hard coded array. Why is my function not returning my stddev
value back to main? I keep getting an error saying that stddev is
undeclared.
#include <stdio.h>
#include <math.h>
float calcstddev(float examgrades[], int size)
{
float sum =0.0;
float mean;
int temp;
float variance;
float stddev;
float difference;
for (temp=0;temp<size;++temp)
{
sum+=examgrades[temp];
}
mean = sum/size;
for (temp=0;temp<size;++temp)
{
stddev+=pow(examgrades[temp]-mean,2);
}
stddev/=size;
stddev=sqrt(stddev);
return stddev;
}
int main()
{
float examgrades[]={90,95,90,82,80,87,92};
calcstddev(examgrades,7);
printf("The standard deviation is %.4f",
stddev);
}
Without pointer there is only one method get value by a function(Not main) which is to collect data returned by function in a varaible.
Code with small modication is given:
#include <stdio.h>
#include <math.h>
float calcstddev(float examgrades[], int size)
{
float sum =0.0;
float mean;
int temp;
float variance;
float stddev;
float difference;
for (temp=0;temp<size;++temp)
{
sum+=examgrades[temp];
}
mean = sum/size;
for (temp=0;temp<size;++temp)
{
stddev+=pow(examgrades[temp]-mean,2);
}
stddev/=size;
stddev=sqrt(stddev);
return stddev;
}
int main()
{
float examgrades[]={90,95,90,82,80,87,92};
float stddev=calcstddev(examgrades,7);
printf("The standard deviation is %.4f", stddev);
}
If you didn't mean that in question please comment.
Get Answers For Free
Most questions answered within 1 hours.