Radical Developers welcomes all the visitors to be a member of the team. Come join us. Its all about open-source concept. All Programs are written and tested in MacOS X, Unix, Linux and may not match with the outputs of Turbo C++ in Windows

Tuesday, January 4, 2011

Write a Program to take some values into an array and find the average of these array elements using pointer and user defined function

Source Code :

#include<stdio.h>
void avg(int *p)   //User Defined Function for Calculation of Average
{
      float avg=0;
      int i;
      for(i=0;i<5;i++,p++)
      {
            avg+=*p;
      }
      avg/=5;     //Calculation of Average
      printf("\n\nAverage : %5.2f",avg);  //Display Result
}
void main()
{
      int a[5],i;
      clrscr();
      printf("\nEnter Elements : ");
      for(i=0;i<5;i++)
      {
            scanf("%d",&a[i]);
      }
      avg(&a[0]); //Passing Address of Array as Argument
      getch();
}

Output :


Enter Elements : 2 4 6 8 1

Average : 4.20

No comments:

Post a Comment