Showing posts with label array of structure. Show all posts
Showing posts with label array of structure. Show all posts

Thursday 16 February 2017

Program using array of structures

Array of structure is a way to process a group of related data. Define a structure to represent the general structure of the data and then declare an array variable of type structure. This way a group of records of related data can be processed easily.

//C program to process employee records
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct employee
{
     int id;
     char name[20],address[100],job[50];
     float salary;
}e[10];

main()
{
     int n,i;
     struct employee getdata();
    
     clrscr();
     printf("Enter the number of details required\t");
     scanf("%d",&n);
     for(i=0;i<n;i++)
    {
         printf("Enter employee %d details\n",i+1);
         e[i]=getdata();
    }
    for(i=0;i<n;i++)
    {
        printf("Employee %d details",i+1);
        printf("\n----------------------\n");
        display(e[i]);
    }
   getch();
}
struct employee getdata()
{
      struct employee e;
      printf("enter name   ");
      scanf("%s",e.name);
      printf("enter emp_id  ");
      scanf("%d",&e.id);
      printf("enter job  ");
      scanf("%s",e.job);
      printf("enter salary  ");
      scanf("%f",&e.salary);
      printf("enter address  ");
      scanf("%s",e.address);
      return e;
 }
 display(struct employee e)
 {
     printf("Name: %s\n",e.name);
     printf("Emp-id: %d\n",e.id);
     printf("Job: %s\n",e.job);
     printf("Salary: %6.2f\n",e.salary);
     printf("Address: %s\n",e.address);
 }
 /*
 OUTPUT
 -------
Enter the number of details required   2
Enter employee 1 details
enter emp_id  1
enter job  SSA
enter salary  400000
enter address  SG
Enter employee 2 details
enter name   sasha
enter emp_id  2
enter job  AP
enter salary  444444
enter address  IN
Employee 1 details
----------------------
Name: shan
Emp-id: 1
Job: SSA
Salary: 400000.00
Address: SG
Employee 2 details
----------------------
Name: sasha
Emp-id: 2
Job: AP
Salary: 444444.00
Address: IN
*/