Pages

Thursday, June 27, 2013

C++ SORTING IN STRUCTURES

QUESTION :
Sorting Employees


Make a structure named as Employee, containing two member variables name of type

char[] and salary of type float. Make an array dynamically querying the size from the

user to store the Employee. Input each Employee data from user and store it in this array.

Sort the Array with respect to the salary of the Employee. Finally display the output in

ascending order as follows:

Ali‟s Salary is: xxxxxx

Abc*s Salay is: xxxxxx

Def‟s Salary is :xxxxxx

CODE :

 ////////////////////////////////////////////////////
#include<iostream>
using namespace std;
struct Employee{

 char name[30];
 float salary;



};
int main(){
 int size;

 cout <<"\nEnter the size\n";
 cin>>size;
 Employee *ptr = new Employee [size];

 for (int i=0; i<size ; i++){
  cout<<"\nEnter the name:\n";
  cin>>ptr[i].name;//
 
  cout<<"\nEnter the salary\n";
  cin>>ptr[i].salary;//
 }

 for(int j=0;j<size-1;j++){
  for(int k=0;k<size-j-1;k++){//
   if (ptr[k].salary > ptr[k+1].salary){
    Employee temp;//
    temp = ptr[k];
    ptr[k] = ptr[k+1];
    ptr[k+1] = temp;
   }
  
  }
 
 
 }

 for (int x=0;x<size;x++){
  cout<<ptr[x].name<<"'s salary is : "<<ptr[x].salary<<endl;
 }

 return 0;
}
/////////////////////////////////////////////////// 

No comments:

Post a Comment