Download as pdf
Download as pdf
You are on page 1of 5
‘S.No: 40 Exp, Name: Write the code to read the employee details and perform the operations _—_Date:2024-01-21 Declare a structure to store the following information of an employee: Employee code, Employee name, Salary, Department number, Date of joining (itis itself a structure consisting of day, month, and year). Write a C program to store the data of N employees where N is given by the user (Use dynamic memory allocation). Include a Menu that allows user to select any of the following features: i) Use a function to display the employee information who are drawing the maximum and minimum salary. ii) Use a function to display the employee records in ascending order according to their date of joining, ‘Source Code: fees] #include #include #include int num; void menu(){ printf("1) Display the maximum and minimum salary.\n"); printf("2) Display the employee records in ascending order.\n"); printf("3) Exit.\n"); printf("Enter your choice: \n"); scanf ("%d" , &num) ; + struct employee{ int empcode: char empname[20] ; float salary; int deptno; struct doj{ int year: int month; int date; dd; 7 struct employee ptr; int n; void max_sal(); void asen_order(); void swap(int *numt, int *num2){ int temp=*numt ; +numt = *num2; *num2 = temp; , void main(){ int i; printf("Enter the number of employees: scanf("“kd" ,n); ptr =(struct employee *)malloc(n*sizeof(struct employee)); for(i = O;icn;i++)4 printf("Enter employee code: "); scanf("%d" ,&(ptr+i)->empcode) ; printf("Enter name: "); dv: = '2023-2027-CSD Page No: 1 6G. Narayanamma Institute of Technology and Science Scanf("%s" , (ptr+i)->enpnane); printf("Enter salary: "): scanf("Kf" ,(ptr+i)->salary); printf("Enter department number scanf( "kd" ,&(ptr+i)->deptno); printf("Enter the joining date:\nEnter year: "): scanf ("Kd ,&(ptr+i)->d.year): printf("Enter month: "); scanf("“éd" ,&(ptr+i)->d. month); printf("Enter date: "): scanf("%d" ,&(ptr+i)->d.date); } while(1){ menu(); switch(num){ case 1: max_sal(); break; case 2: asen_order(); break; case 3: exit(0); default : printf("Wrong choice. Try again.\n"); break; } } } void max_sal(){ int curr,max=0,min= INT_MAX; int xidx = 0, nidx = 0; for(int i =O;isn;it+)¢ curr = (ptr+i)->salary: if (maxccurr){ max = curr; xidx = i; } if (minreurr){ min = curr; nidx = i; } } printf("Maximum salary employee details: \n"): printf("Employee code:%d\t Employee name:%s\t Department number :%d\t Salar y:%f\t Joining date:%d-%d-d\n", (ptr+xidx)->empcode, (ptr+xidx)->empname, (ptr+x idx)->deptno, (ptr+xidx)->salary, (ptr+xidx)->d. year, (ptr+xidx)->d.month, (ptr+xi dx)->d.date); printf("Minimum salary employee details: \n"): printf("Employee code:%d\t Employee name:%s\t Department number :%d\t Salary:%f \t Joining date:%d-%d-%d\n" , (ptr+nidx)->empcode, (ptr+nidx)->empname, (ptr+nidx) ->deptno, (ptr+nidx)-salary, (ptr+nidx)->d.year, (ptr+nidx)->d.month, (ptr+nidx)- >d.date); } void asen_order(){ int idx[151; int date_arr[15]; for(int i = O;icn;it+){ idx[i}=i: } = '2023-2027-CSD Page No: 2 6G. Narayanamma Institute of Technology and Science for(int i =O;id.year)+100*((ptr+i)-»d.month) + ((ptr+i)-> d.date); } for(int j = 0; j date_arr[idx(itt]]){ swap(Bidx[i],BidxLi+1]); } , + printf("Employees details based on their date of joining:\n"); for(int i = O;icnsit+){ printf("Employee code:%d\t Employee name:%s\t Department number :%d\t Salary:%f\t Joining date:%sd-%d-%d\n" , (ptr+idx[i] )->empcode, (ptr+idx[ i] )->empna me, (ptr+idx[i])->deptno, (ptr+idx[i])-> salary, (ptr+idx[i])->d.year, (ptr+idx [i])->d.month, (ptr+idxlil)->d.date); + Execution Results - ali test cases have succeeded! Test Case-1 User Output Enter the number of employees: 2 Enter employee code: 12345 Enter name: Tharun Enter salary: 20000 Enter department number: 2 Enter the joining date: 2020 Enter year: 2020 Enter month: 05 Enter date: 21 Enter employee code: 12346 Enter name: Varun Enter salary: 21000 Enter department number: 1 Enter the joining date: 2019 Enter 2019 Enter 208 Enter date: 19 1) Display the maximum and minimum salary. 1 2) Display the employee records in ascending order. 1 3) Exit. 1 Enter your choice: 1 Maximum salary employee details: 2 Employee code:12346 Employee name:Varun Department number:1 _Salary:210 Minimum salary employee details: 2 Employee code: 12345 Employee name:Tharun Department number:2 Salary: 200 1) Display the maximum and minimum salary. 2 2) Display the employee records in ascending order. 2 3) Exit. 2 ID: 2325146762 Page No:3 '2023-2027-CSD 6G. Narayanamma Institute of Technology and Science Enter your choice: 2 Employees details based on their date of joining: 3 Employee code: 12346 Employee name:Varun Department number:1 _Salary:210 Employee code: 12345 Employee name:Tharun Department number:2 Salary: 200 1) Display the maximum and minimum salary. 3 2) Display the employee records in ascending order. 3 3) Exit. 3 Enter your choice: 3 Test Case-2 User Output Enter the number of employees: 2 Enter employee code: 23 Enter name: Ravi Enter salary: 20000 Enter department number: 3 Enter the joining date: 2001 Enter year: 2001 Enter month: 4 Enter date: 21 Enter employee code: 43 Enter name: Sunny Enter salary: 30000 Enter department number: 3 Enter the joining date: 2003 Enter 2003 Enter 3 Enter date: 1 1) Display the maximum and minimum salary. 1 2) Display the employee records in ascending order. 1 3) Exit. 1 Enter your choice: 1 Maximum salary employee details: 2 Employee code:43 Employee name: Sunny Department number:3 _Salary:300 Minimum salary employee details: 2 Employee code: 23 Employee name:Ravi Department number:3 _ Salary:200 1) Display the maxinun and minimum salary. 2 2) Display the employee records in ascending order. 2 3) Exit. 2 Enter your choice: 2 Employees details based on their date of joining: 1 Employee code: 23 Employee name:Ravi Department number:3 Salary:200 Employee code:43 Employee name: Sunny Department number:3 _ Salary:300 1) Display the maxinum and minimum salary. 1 2) Display the employee records in ascending order. 1 3) Exit. 1 Enter your choice: 1 Maximum salary employee details: 34 Employee code:43 Employee name: Sunn) Department number:3Salary:300 Minimum salary employee details: 34 Employee code:23 Employee name:Ravi Department nunber:3__ Salary:200, 1) Display the maximum and minimum salary. 34 2) Display the employee records in ascending order. 34 3) Exit. 34 ID: 23251A6762 '2023-2027-CSD Page No: 4 6G. Narayanamma Institute of Technology and Science Enter your choice: 34 Wrong choice. Try again. 3 1) Display the maximum and minimum salary. 3 2) Display the employee records in ascending order. 3 3) Exit. 3 Enter your choice: 3 ID: 2325146762 Page No: 5 '2023-2027-CSD 6G. Narayanamma Institute of Technology and Science

You might also like