Write a program to compute sum and average of all elements in an array using pointer.

C File =>

/* Write a program to compute sum and average of all elements in an array using pointer */


#include<stdio.h>
int main() {
int num[30],n,i,sum;
float avg;
//declaring pointer to array
int (*p)[30]=&num;
printf("\n Enter how many elements :");
scanf("%d",&n);
printf("\n Accepting data :\n");
for(i=0;i<n;i++)
{
printf("\n Enter number%d:",i+1);
scanf("%d",&num[i]);
}
//calculating sum using pointer to array
for(i=0;i<n;i++)
sum=sum+(*p)[i];
avg=sum/n;
printf("\n Sum of array elements = %d\n Average=%f",sum,avg);
return 0;
}

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.