In: Computer Science
WITHOUT USING POINTERS. This is C programming. I am writing a program which should calculate standard deviation of an array of exam scores and then add the standard deviation to each array element. It should print the original array, the standard deviation, and the new rounded adjusted array. I included my question as a comment in the line of code that is giving me an issue. (Removing the a from E-x-a-m as Chegg doesn't allow the word.)
#include <stdio.h>
#include <math.h>
float calcstddev(float exmgrades[], int size)
{
float sum =0.0;
float mean;
int temp;
float variance;
float stddev;
float difference;
for (temp=0;temp<size;++temp)
{
sum+=exmgrades[temp];
}
mean = sum/size;
for (temp=0;temp<size;++temp)
{
stddev+=pow(exmgrades[temp]-mean,2);
}
stddev/=size;
stddev=sqrt(stddev);
return stddev;
}
float adjustscores(float exmgrades[],int size)
{
float stddev=calcstddev(exmgrades,7);
int temp=0;
float adjustedscores[size];
for(temp=0;temp<size;++temp)
{
adjustedscores[temp]+=exmgrades[temp]+stddev;
}
return adjustedscores; //////HERE is the line that
gives me an error. It states:[Error] incompatible types when
returning type 'float *' but 'float' was
expected. If I remove this line, my output for
the adjusted array elements is .0f, so the data isn't passed to
main. What should I do here? ///////////////////
}
int main()
{
float exmgrades[]={90,95,90,82,80,87,92};
float stddev=calcstddev(exmgrades,7);
int size;
int arrayprint;
float adjustedscores[size];
printf("The exm scores are: ");
for (arrayprint = 0; arrayprint < 7;
arrayprint++)
{
printf("%.0f ", exmgrades[arrayprint]);
}
printf("\nThe standard deviation is %.4f",
stddev);
printf("\nThe adjusted scores are: ");
for (arrayprint =0; arrayprint <7;
arrayprint++)
{
printf(".0f", adjustedscores[arrayprint]);
}
}
Complete Working Code
#include <stdio.h>
#include <math.h>
float calcstddev(float exmgrades[], int size)
{
float sum =0.0;
float mean;
int temp;
float variance;
float stddev;
float difference;
for (temp=0;temp<size;++temp)
{
sum+=exmgrades[temp];
}
mean = sum/size;
for (temp=0;temp<size;++temp)
{
stddev+=pow(exmgrades[temp]-mean,2);
}
stddev/=size;
stddev=sqrt(stddev);
return stddev;
}
float adjustscores(float exmgrades[],int size)
{
float stddev=calcstddev(exmgrades,7);
int temp=0;
float adjustedscores[size];
for(temp=0;temp<size;temp++)
{
adjustedscores[temp] = 0; // change 3: initializing with 0, as it was giving garbage value
adjustedscores[temp]+= exmgrades[temp] + stddev;
}
//change 2: printing the adjusted array in the function itself
printf("\nThe adjusted scores are: ");
for(temp=0; temp <size; temp++)
{
printf("%0.0f ", adjustedscores[temp]);
}
}
int main()
{
float exmgrades[]={90,95,90,82,80,87,92};
float stddev=calcstddev(exmgrades,7);
int size;
int arrayprint;
float adjustedscores[size];
printf("The exm scores are: ");
for (arrayprint = 0; arrayprint < 7; arrayprint++)
{
printf("%0.0f ", exmgrades[arrayprint]);
}
printf("\nThe standard deviation is %.4f", stddev);
adjustscores(exmgrades, 7); // change 1: added function call
}
Output Screenshot
I have made few changes in the code that has been mentioned in the code as comments:
change 1: Function call added to the main function
change 2: Function will not return the value, printing the adjusted array in the function itself.
change 3:initialize the elements of adjusted array, as it was giving the garbage value.
Please copy and paste the code and execute in your IDE to see the output.