Learn about Find Sum and Average of an Array Using the Pointer in C in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C program to find Sum and Average of an Array Using the Pointer
here, we develop program that takes the end-input user’s for the array of numbers and compute the sum and average. Display the results on the screen later. To execute all of these activities, we use the pointer.
#include<stdio.h>
int main()
{
float x[5], sum=0.0, avg;
int i;
float *px, *psum, *pavg;
px = &x[0]; // Or, px = &x;
psum = &sum, pavg = &avg;
printf("Enter array Elements: ");
for (i=0;i<5;i++)
{
scanf("%f",(px+i));
*psum += *(px + i);
}
*pavg = *psum / 5;
printf("Sum of elements = %.2f \t Average of elements= %.2f\n", *psum, *pavg);
return 0;
}
Output:
Enter array Elements: 3
56
46
32
11
Sum of elements = 148.00 Average of elements= 29.60
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
Read and write an array using function and pointer
C Program to Read and write an array using the pointer