Program to illustrate nested union


C File =>



/* Write a program to accept details of ‘n’ cricket players (name, type). If type = 1 (batsman), accept batting_average. If type=2 (bowler), accept number_wickets. Use nested union. Display information of all batsmen and bowlers. */


#include<stdio.h>
#include<string.h>
typedef union player
{
    char name[30];
    union type
   {
       int type;
   }t;
};
int main()
{
     union player p[50];
     char batsman[50][50],bowler[50][50],s[30];
     float avg[50];
     int i,j=0,k=0,wkts[50],n1,n2,n;
     printf("\n How many played? ");
     scanf("%d",&n);
    // Accepting input
     for(i=0;i<n;i++)
    {
       printf("\n Enter player name : ");
       scanf("%s",p->name);
       strcpy(s,p->name);
       printf("\n Enter type : ");
       scanf("%d",&p->t.type);
       if(p->t.type==1)
      {
          strcpy(batsman[j],s);
          printf("\n Enter batting average : ");
          scanf("%f",&avg[j]);
          j++;
       }
      else if(p->t.type==2)
     {
          strcpy(bowler[k],s);
          printf("\n Enter no of wickets : ");
          scanf("%d",&wkts[k]);
          k++;
      }
   } //end for
  n1=j;
  n2=k;
  printf("\n\n Details of Batsman are :");
  for(j=0;j<n1;j++)
     printf("\n\n Name : %s\n Batting avg : %f",batsman[j],avg[j]);
  printf("\n\n Details of Bowlers :");
  for(k=0;k<n2;k++)
        printf("\n\n Name : %s\n No. of wickets : %d",bowler[k],wkts[k]);
  return 0;
}

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.