Skip to main content

ProwessApps

Learn · Practice · Excel

Define a structure to store the employee name,id,date of birth,basic salary of employee. WAP to store the 5 employee details. Display the information of employee having highest salary. Sort the employee details according to the basic salary.

C++ Code Example — Structure Programs

ADVERTISEMENT

Define a structure to store the employee name,id,date of birth,basic salary of employee. WAP to store the 5 employee details. Display the information of employee having highest salary. Sort the employee details according to the basic salary.

Objective

Write a C++ program using a nested structure to manage Employee data, find the highest salary, and sort them.

Algorithm / Approach

  1. Define a struct Emp. Inside it, define a nested struct DOB (Date of Birth).
  2. Read the details for 5 employees into an array e[5].
  3. Find the employee with the highest salary using a standard maximum-search loop and print their details.
  4. Use Bubble Sort to sort the entire array of employees based on their bsal property.
  5. Print the sorted employee list.
main.cpp
#include<iostream>
using namespace std;
struct Emp {
 string name;
 int id;
 int bsal;
 struct DOB {
  int d;
  int m;
  int y;
 }dob;
};
int main() {
 Emp e[5];
 for(int i = 0;i<5;i++) {
  cout<<"Enter Name ";
  getline(cin,e[i].name);
  cout<<"Enter id";
  cin>>e[i].id;
  cout<<"Enter basic salary ";
  cin>>e[i].bsal;
  cout<<"Enter Date(day)";
  cin>>e[i].dob.d;
  cout<<"Enter Month ";
  cin>>e[i].dob.m;
  cout<<"Enter Year ";
  cin>>e[i].dob.y;
  cin.ignore(1,'\n');
 }
 int max = e[0].bsal;
 Emp t = e[0];
 for(int i = 0;i<5;i++) {
  if(e[i].bsal>max){
   max = e[i].bsal;
   t = e[i];
  }
 }
 cout<<"Emp. having highest salary \n";
 cout<< t.name<<"\t"<< t.id<<"\t";
 cout<< t.bsal<<"\t"<< t.dob.d<<"-";
 cout<< t.dob.m<<"-"<< t.dob.y<< endl;
cout<<"\n\nSorted Detail \n";
for(int i = 1; i<=5; i++) {
 for(int j = 0; j<5-i;i++) {
  if(e[j].bsal>e[j+1].bsal) {
   t = e[j];
   e[j] = e[j+1];
   e[j+1] = t;
  }
 }
}
for(int i = 0; i<5; i++) {
 cout<< e[i].name<<"\t"<< e[i].id;
 cout<<"\t"<< e[i].bsal<<"\t";
 cout<< e[i].dob.d<<"-";
 cout<< e[i].dob.m<<"-";
 cout<< e[i].dob.y<< endl;
 }
 return 0;
}

Expected Output

Enter Name Anup
Enter id 12
Enter basic salary 22000
Enter Date(day) 11
Enter Month 10
Enter year 1994
Enter Name Ashok
Enter id 15
Enter basic salary 18000
Enter Date(day) 12
Enter Month 11
Enter year 1990
Enter Name Ayan
Enter id 51
Enter basic salary 50000
Enter Date(day) 17
Enter Month 12
Enter year 2014
Enter Name XYZ
Enter id 11
Enter basic salary 8000
Enter Date(day) 3
Enter Month 3
Enter year 1993
Enter Name Vinay
Enter id 23
Enter basic salary 15000
Enter Date(day) 12
Enter Month 12
Enter year 1990
Employee having highest salary
Ayan	51	50000		17-12-2014

Sorted Detail
XYZ	11	8000		3-3-1993
Vinay	23	15000		12-12-1990
Ashok	15	18000		12-11-1990
Anup	12	22000		11-10-1994
Ayan	51	50000		17-12-2014

Explanation of the Program

  • Structures can be nested inside other structures! Here, Date of Birth (day, month, year) is grouped into its own mini-structure inside the main Employee structure to keep data heavily organized.
  • Notice the Bubble Sort logic: if(e[j].bsal &gt; e[j+1].bsal). We compare their salaries, but when a swap is needed, we swap the ENTIRE employee object (e[j] = e[j+1]), keeping all their name and DOB data perfectly attached to their salary.

Complexity

Time Complexity O(n^2) - Due to the Bubble Sort.
Space Complexity O(n)
ADVERTISEMENT