14. C PROGRAMMING LAB | Check Now
C PROGRAMMING LAB –14] Develop a program using pointers to compute the sum, mean, and standard deviation of all elements stored in an array of n real numbers.
C PROGRAMMING -Algorithm
- Step-1: Start
- Step-2: Read n
- Step-3: For every value of n read the x
- Step-4: Initialize sum=0 and i=0
- Step-5: For every value of n and i, comuter sum using sum = sum + (*(x+i) – mean) * ( * ( x+i) – mean)
- Step-6: Using the sum value computer variance = sum / n and deviation = sqrt ( variance )
- Step-7: Display mean, variance, deviation
- Step-8: Stop
Flow Chart
Program -14 source code
#include<stdio.h> #include<math.h> int main() { int n , i; float x[20],sum,mean; float variance , deviation; printf("Enter the value of n \n"); scanf("%d",&n); printf("enter %d real values \n",n); for (i=0;i<n;i++) { scanf("%f",(x+i)); } sum=0; for(i=0;i<n;i++) { sum= sum+*(x+i); } printf("sum=%f\n",sum); mean=sum/n; sum=0; for(i=0;i<n;i++) { sum=sum+(*(x+i)-mean)*(*(x+i)-mean); } variance=sum/n; deviation=sqrt(variance); printf("mean(Average)=%f\n",mean); printf("variance=%f\n",variance); printf("standard deviation=%f\n",deviation); }
Output
Enter the value of n
5
Enter the 5 real values
3
7
23
1
4
Sum = 38.0000
Mean ( Average ) = 7.6000
Variance = 63.039997
Standard deviation = 7.9397
C PROGRAMMING –Viva Questions
1] Define pointer?
2] Define an array of pointer
3] Difference between ( x + i ) and * ( x + i )
4] Define array