Program to demonstrate arrays of structures in C

#include <stdio.h>
# define limit 20
struct pair{
   int first;
   int second;
   float average;
};
 
void findavg(struct pair *,int);
 
int main()
{
   struct pair triplet[limit];
   int i;
 
   printf("Enter the integer pairs (a,b):n");
   for(i=0;i<limit;i++)
      scanf("%d,%d",&triplet[i].first,&triplet[i].second);
 
   findavg(triplet,limit);
 
   for(i=0;i<limit;i++)
      printf("%d,%d - %fn",(triplet+i)->first,
            (triplet+i)->second,(triplet+i)->average);
   return 0;
}
 
void findavg(struct pair *ptr,int lim)
{
   while(--lim>=0)
  {
       ptr->average=(float)(ptr->first + ptr->second)/2;
       ++ptr;
  }
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.