Wednesday, 10 January 2018

Dynamic records using structure

Q : Write a program to input name of age record of a company. How many records have to input? It will ask at run time. After input complete, program will print all the records with average age.

Solution : 

/*==============================
     Girfa Student Help
     Program : Age calculation in dynamic structure
     More Program : http://girfahelp.blogspot.in/p/c-language-structure-programming.html
================================*/
#include<stdio.h>
#include<conio.h>
typedef struct stu
{
     char name[20];
     int age;
}student;
void main()
{

     student *pt;
     int i,sum=0,count,j;
     clrscr();
     pt=(student*) malloc(sizeof(student)*i);
     for(j=0;j<i;j++)
     {
          printf("\nEnter Age for %d record>> ",j+1);
          scanf("%d",&pt[j].age);
          sum+=pt[j].age;
          printf("Enter Name for %d record>> ",j+1);
          fflush(stdin);
          gets(pt[j].name);

     }
     clrscr();
     for(j=0;j<i;j++)
     {
          printf("\nName=%s, Age=%d",pt[j].name,pt[j].age);
     }

     printf("\n\n\tAverage=%d",sum/i);
     getch();

}