Tuesday, December 28, 2010

Array of structures in C Programming


Today I will discuss about array of structure. Suppose we want to store the test record of our cricket team players (Name, number of test matches played, number of innings, number of not out and total runs scored, average). For this type of program we have to declare a structure with six members and the variable of the structure will be of array type.

program on array of structure 

#include< stdio.h >
struct player
{
 int match, innings, notout, runs;
 float average;
 char name [20];
 };
 void main ()
 {
 struct player play [3];
 int i;
    clrscr ();
    for (i=0;i< 3;i++)
    {
   printf ("Enter the name-");
   scanf ("%s", play [i]. name);
   printf ("Enter the test matches played :-");
  scanf ("%d", &play [i]. match);
   printf ("Enter the test innings played: -");
  scanf ("%d", &play [i]. innings);
   printf ("Enter the not out: -");
  scanf ("%d", &play [i]. notout);
   printf ("Enter the runs scored:-");
  scanf ("%d", &play [i]. runs);
  play [i]. average=(float) play [i]. runs/(play [i]. innings-play [i]. notout);
  }
  printf ("Name Match Innings Not out Runs Average\n");
  for (i=0;i< 3;i++)
  {
  printf ("%s  %d  %d  %d  %d  %.2f\n", play [i]. name, play [i]. match,
  play [i]. innings, play [i]. Notout, play [i]. runs, play [i]. average);
  }
  getch ();
  }

  About the program

‘struct player play [3]’ is the array of structure means the variable play can hold records of three players. Without using the array of structure, we had to create three variables to store the records.

1 comment:

Subscribe via email

Enter your email address:

Delivered by FeedBurner