Using structure, input records of 1000 employees & display records of employee having highest & lowest salary- C

Q. Write a program using structure to input records of 1000 employees and display records of employee, having the highest and lowest salary. Also display the records of all employee working in the "computer" department. Structure member include Id, Name, Salary and Department. 

#BIT-FirstSem-2018-GroupB-5No-8Marks-Solution
==> Code goes like this:

#include<stdio.h>
#include<string.h>
#define SIZE 1000
int main()
{
struct employee
{
    char name[30],department[30];
    int id;
    float salary;
};
struct employee emp[SIZE],temp;
int i,j;
float temforsalary;
const char *compare="computer";
for(i=0;i<SIZE;i++)
{
    printf("\n Enter Information of Employee no %d\n",i+1);
    printf("\n Name:\t");
    gets(emp[i].name);
    printf("\n Department Name:\t");
    gets(emp[i].department);
    printf("\n Id:\t");
    scanf("%d",&emp[i].id);
    printf("\n Salary:\t");
    scanf("%f",&temforsalary);
    emp[i].salary=temforsalary;
    fflush(stdin);
}

//for highest salary
for(i=0;i<SIZE-1;i++)
{
    for(j=i+1;j<SIZE;j++)
    {
        if(emp[i].salary <emp[j].salary)
        {
            temp=emp[i];
            emp[i]=emp[j];
            emp[j]=temp;
        }    
    }
}
printf("\n Employee having highest Salary:\n");
printf("\n Employee Id\t Employee Name\t Salary\t\t Department\n");
printf("\n ----------------------------------------------------------\n");
printf("\n %d\t\t %s\t\t %.2f \t %s\n",emp[0].id,emp[0].name,emp[0].salary,emp[0].department);

//for lowest salary
for(i=0;i<SIZE-1;i++)
{
    for(j=i+1;j<SIZE;j++)
    {
        if(emp[i].salary >emp[j].salary)
        {
            temp=emp[i];
            emp[i]=emp[j];
            emp[j]=temp;
        }    
    }
}
printf("\n");
printf("\n Employee having lowest Salary:\n");
printf("\n Employee Id\t Employee Name\t Salary\t\t Department\n");
printf("\n ----------------------------------------------------------\n");
printf("\n %d\t\t %s\t\t %.2f \t %s\n",emp[0].id,emp[0].name,emp[0].salary,emp[0].department);

//For Department
printf("\n");
for(i=0;i<SIZE;i++)
{
if((strcmp((emp[i].department),compare))==0)
{
printf("\n Employees of computer department:\n");
printf("\n Employee Id\t Employee Name\t Salary\t\t Department\n");
printf("\n ----------------------------------------------------------\n");
printf("\n %d\t\t %s\t\t %.2f \t %s\n",emp[i].id,emp[i].name,emp[i].salary,emp[i].department);
}
}
return 0;
}

Output:



















Output1:


















  
If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you  
Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment