Skip to main content

Develop a program using pointers to compute the sum, mean and standard deviation of all elements stored in an array of N real numbers Program and Algorithm Computer Science Engineering VTU

pointers
#include <stdio.h>
#include <math.h>
void main()
{
float a[10], *ptr, mean, sum=0, std,sumstd=0;
int n,i;
printf("Enter the number of elements : ");
scanf("%d",&n);
printf("Enter the array elements :\n");
for( i=0; i<n; i++)
scanf("%f",&a[i]);
//assigning address of a to pointer ptr
ptr = a;
//compute the sum
for(i=0;i<n;i++)
{
sum += *ptr;
ptr++;
}
//compute the mean
mean = sum/n;
//compute standard deviation
ptr = a;
for(i=0; i<n; i++)
{
sumstd = sumstd + pow((*ptr-mean),2);
ptr++;
}
std = sqrt(sumstd/n);
//print sum, mean, & standard deviation
printf("Sum = %f\n",sum);
printf("Mean = %f\n",mean);
printf("Standard Deviation : %f\n",std);
}
POINTERS Algorithm 
Start

Read number of elements as n 

Read array elements 

For i = 0 to n

Read a[i]

Ptr = a

For i = 0 to n

sum = sum + *ptr

increment ptr by 1

mean = sum/n

ptr = a

For i = 0 to n

sumstd=sumstd + pow((*ptr - mean),2); 

increment ptr by 1 

std = sqrt(sumstd/n)

Display sum, mean, std

Stop 

Comments

Popular posts from this blog

Raju is a Civil Engineer. He is writing software to automate his work. As a part of his requirement, he wants to calculate the circle diameter, circumference, and area. Help Raju to complete his task. Get radius as input.

OUTPUT

Select the option that is most nearly OPPOSITE in meaning to the word or phrase is given in bold. Birds are quarantined to prevent the spread of bird flu.

Select the option that is most nearly OPPOSITE in meaning to the word or phrase is given in bold. Birds are  quarantined  to prevent the spread of bird flu. immunized butchered secluded CORRECT mingled