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.
Algorithm:
Step 1: [Start]
Step 2: [Input the elements]
Read n, a[i]
Step 3: [Initialize] sum=0,sumstd=0
Step 4: [Compute sum, mean, standard deviation]
Ptr=a
For i = 0 thru n in steps of 1
Sum=sum+*ptr
Ptr++
Mean=sum/n
Ptr=a
For i 0 thru n in steps of 1
Sumstd=sumstd+pow((*ptr-mean),2)
Ptr++
std=sqrt(sumstd/n)
Step 5: [Output]
Print sum, mean, standard deviation
Step 6: [Stop]
FLOWCHART:
PROGRAM:
#include<stdio.h>
#include<math.h>
void main ()
{
float a[20], sum1 = 0, sum2 = 0, mean, var, dev;
int i, n;
printf ("Enter no of elements:");
scanf ("%d", &n);
printf ("Enter array elements:");
for (i = 0; i < n; i++)
{
scanf ("%f", a + i);
sum1 = sum1 + * (a + i);
}
mean = sum1 / n;
for (i = 0; i < n; i++)
{
sum2 = sum2 + pow ((*(a + i) - mean), 2);
}
var = sum2 / n;
dev = sqrt (var);
printf ("Sum :%f\n", sum1);
printf ("Mean :%f\n", mean);
printf ("Variance :%f\n", var);
printf ("Deviation :%f\n", dev);
}
OUTPUT:
Algorithm:
Step 1: [Start]
Step 2: [Input the elements]
Read n, a[i]
Step 3: [Initialize] sum=0,sumstd=0
Step 4: [Compute sum, mean, standard deviation]
Ptr=a
For i = 0 thru n in steps of 1
Sum=sum+*ptr
Ptr++
Mean=sum/n
Ptr=a
For i 0 thru n in steps of 1
Sumstd=sumstd+pow((*ptr-mean),2)
Ptr++
std=sqrt(sumstd/n)
Step 5: [Output]
Print sum, mean, standard deviation
Step 6: [Stop]
FLOWCHART:
PROGRAM:
#include<stdio.h>
#include<math.h>
void main ()
{
float a[20], sum1 = 0, sum2 = 0, mean, var, dev;
int i, n;
printf ("Enter no of elements:");
scanf ("%d", &n);
printf ("Enter array elements:");
for (i = 0; i < n; i++)
{
scanf ("%f", a + i);
sum1 = sum1 + * (a + i);
}
mean = sum1 / n;
for (i = 0; i < n; i++)
{
sum2 = sum2 + pow ((*(a + i) - mean), 2);
}
var = sum2 / n;
dev = sqrt (var);
printf ("Sum :%f\n", sum1);
printf ("Mean :%f\n", mean);
printf ("Variance :%f\n", var);
printf ("Deviation :%f\n", dev);
}
No comments:
Post a Comment