a) modify the program to use pointers to access the array elements. b) move the
ID: 645731 • Letter: A
Question
a) modify the program to use pointers to access the array elements.
b) move the calculation of average, variance and standard deviation from main into a separate function. Set up the function to accept an array by its pointer (the address of the zeroth element). Test your modified program with a simple array specified when the array is initialized.
//find average, variance, and standard deviation
#include //includes standard input output
#include //includes math functions
#define n 400 //sets n equal to 400
int main()
{
//array
float a[n]= {1.8, 4.3, 15.6, 0.2, 10.7, 1.2, 0.9, 5.0, 4.6, 6.8,
4.6, 0.7, 5.2,5.5, 4.5, 5.5, 3.8, 6.8, 5.0, 1.2, 2.0, 1.1, 5.0, 3.3,
2.1, 10.9, 1.4, 1.3, 0.1, 5.0, 2.6, 1.5, 10.3, 23.9, 1.3, 1.8, 1.8,
7.1, 3.6, 9.5, 9.2, 16.1, 15.0, 0.7, 10.0, 5.9, 1.6, 2.8, 2.7, 0.8, 0.2,
6.5, 7.4, 5.2, 0.6, 2.7, 14.8, 6.8, 9.8, 14.9, 5.1, 0.9, 1.3, 4.2, 3.5,
2.2, 1.2, 6.8, 9.0, 8.9, 5.7, 11.5, 1.6, 13.2, 1.1, 3.3, 4.7, 9.9, 1.5,
9.3, 2.6, 8.5, 13.0, 8.2, 1.5, 6.0, 18.4, 1.6, 0.6, 9.1, 12.9, 7.4, 0.2,
1.1, 1.1, 1.4, 12.2, 0.3, 8.4, 11.1, 11.5, 3.2, 1.9, 0.1, 1.2, 7.6, 7.1,
3.5, 6.7, 1.5, 16.5, 1.1, 3.7, 5.7, 7.5, 13.0, 4.5, 10.9, 4.3, 3.8, 2.7,
0.6, 2.4, 7.4, 2.8, 6.2, 1.7, 3.8, 1.6, 2.4, 11.9, 6.4, 4.4, 1.3, 2.8, 0.2,
11.7, 3.9, 2.0, 5.0, 2.9, 11.9, 0.2, 0.2, 5.7, 1.3, 3.7, 1.5, 0.9, 5.6,
2.6, 5.2, 1.2, 4.6, 3.3, 1.6, 10.9, 0.3, 2.3, 1.1, 1.8, 5.5, 6.2, 6.0, 6.4,
2.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7, 2.1, 0.2, 2.9, 0.9, 3.9, 6.4, 1.9, 6.1, 10.6, 11.4, 9.9, 4.3,
10.0, 5.5, 3.6, 3.0, 0.7, 2.4, 0.9, 3.3, 2.9, 9.2, 6.5, 3.8, 5.5, 3.1, 6.1,
2.9, 1.5};
//set values to floats or integers
float sum=0, avg, var, sd;
int i;
//summing and averaging of numbers
for (i=0;i
avg=sum/n;
//variance and its square root (sd)
for (i=0;i
sd=sqrt((var)/(n-1.0));
//print answers
printf("Average= %f Variance= %f S.D.= %f ", avg, var, sd);
return 0;
}
Explanation / Answer
Below is the modified C code:-
//find average, variance, and standard deviation
#include<stdio.h> //includes standard input output
#include<math.h> //includes math functions
#define n 400 //sets n equal to 400
void calculateAvgVarSd(float *a){
//set values to floats or integers
float sum=0, avg, var, sd;
int i;
//summing and averaging of numbers
for (i=0;i<n;i++){
sum+=*(a+i);
}
avg=sum/n;
//variance and its square root (sd)
sum=0;
for (i=0;i<n;i++){
sum = sum + pow((*(a+i) - avg), 2);
}
var=sum/(float)n;
sd=sqrt((var)/(n-1.0));
//print answers
printf("Average= %f Variance= %f S.D.= %f ", avg, var, sd);
}
int main()
{
//array
float a[n]= {1.8, 4.3, 15.6, 0.2, 10.7, 1.2, 0.9, 5.0, 4.6, 6.8,
4.6, 0.7, 5.2,5.5, 4.5, 5.5, 3.8, 6.8, 5.0, 1.2, 2.0, 1.1, 5.0, 3.3,
2.1, 10.9, 1.4, 1.3, 0.1, 5.0, 2.6, 1.5, 10.3, 23.9, 1.3, 1.8, 1.8,
7.1, 3.6, 9.5, 9.2, 16.1, 15.0, 0.7, 10.0, 5.9, 1.6, 2.8, 2.7, 0.8, 0.2,
6.5, 7.4, 5.2, 0.6, 2.7, 14.8, 6.8, 9.8, 14.9, 5.1, 0.9, 1.3, 4.2, 3.5,
2.2, 1.2, 6.8, 9.0, 8.9, 5.7, 11.5, 1.6, 13.2, 1.1, 3.3, 4.7, 9.9, 1.5,
9.3, 2.6, 8.5, 13.0, 8.2,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, 4.7, 1.1, 8.1, 22.6, 0.9, 4.2, 6.8, 0.4,
3.4, 2.8, 2.9, 9.2, 14.6, 7.6, 16.6, 2.5, 9.9, 0.2, 1.6, 2.3, 2.9, 5.2,
1.2, 1.9, 2.9, 6.8, 1.4, 6.1, 3.6, 3.6, 2.3, 12.3, 2.4, 5.3, 11.5, 14.9,
4.9, 1.6, 4.1, 4.9, 2.5, 3.9, 4.2, 8.7, 7.2, 4.6, 3.4, 1.1, 5.8, 1.8, 9.3,
3.7, 6.4, 18.1, 8.7, 4.5, 1.9, 3.1, 3.5, 0.6, 16.6, 9.5, 2.5, 7.9, 13.1,
6.7, 2.7, 1.8, 1.8, 2.6, 11.9, 8.2, 2.7, 5.2, 2.3, 8.6, 3.9, 0.9, 6.1, 3.9,
1.5, 0.7, 2.1, 0.2, 2.9, 0.9, 3.9, 6.4, 1.9, 6.1, 10.6, 11.4, 9.9, 4.3,
10.0, 5.5, 3.6, 3.0, 0.7, 2.4, 0.9, 3.3, 2.9, 9.2, 6.5, 3.8, 5.5, 3.1, 6.1,
2.9, 1.5};
calculateAvgVarSd(a);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.