Average of Array With Pointer – Write a C Program to calculate average of array elements using pointers.
#include <stdio.h> int main() { int a[50], i,n, sum=0; float average; int *ptr; printf("\n Write a C Program To:"); printf("\n Input elements and Find Average in"); printf("\n Array Using Pointers in C:"); printf("\n ------------------------------------\n"); printf(" Enter the number of elements of the array:"); scanf("%d",&n); ptr = a; for(i=0;i<n;i++) { printf(" Enter Element a[%d] : ",i); scanf("%d",ptr++); } ptr = a; for(i=0;i<n;i++) { sum = sum + *ptr++; } average = (float)sum / n; printf(" Average of Array Elements using Pointer=%.2f",average); return 0; }
Output
Write a C Program To:
Input elements and Find Average in
Array Using Pointers in C:
————————————
Enter the number of elements of the array:5
Enter Element a[0] : 10
Enter Element a[1] : 20
Enter Element a[2] : 30
Enter Element a[3] : 10
Enter Element a[4] : 5
Average of Array Elements using Pointer=15.00
You may also like more on C Pointers:
- Understanding C Pointers
- Pointer Declaration With Example Program
- Using Pointers C ProgramĀ to Add Two Numbers
- Sum of Array Using Pointers