Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

in c++ you are to create an array of structs and sort the array. 1. create a str

ID: 3753036 • Letter: I

Question

 in c++ you are to create an array of structs and sort the array.  1.  create a struct with (at least) 3 fields 2.  create an array of structs 3.  read data into the array of structs (9-11 records) 4.  print all of the fields in the array (line up the columns) 5.  sort the array (in ascending order) on 1 field of the struct 6.  print all of the fields in the array 7.  sort the array (in descending order) on another field of the struct 8.  print all of the fields in the array  Keep in mind that the fields of a struct all go together, and when you swap 1 field of a struct you need to swap ALL of the fields in the struct.  

Explanation / Answer

program:- C++

#include<iostream>
#include <fstream>   // for file read
using namespace std;
struct emp       //structure
{
   string name;   //for name
   int age,sal;   //for age, sal
}e[11];
int main()
{
   string name1;
   int age1,sal1,n,c;
   cout<<"Enter 1 for give data manuvally or enter 2 for give through file"<<endl;
   cin>>c;
   if(c==1)   //giving input from user
   {
       cout<<"How many employs do you want to enter :"<<endl;
       cin>>n;
       for(int i=0;i<n;i++)
       {
           cout<<"Enter name, age, salary of person "<<i+1<<":";
           cin>>e[i].name>>e[i].age>>e[i].sal;       //reading input
           cout<<endl;
       }
   }
   if(c==2)   //giving input from file
   {
       n=0;
       int i=0;
       fstream myfile("emp.txt", std::ios_base::in);   //open file named emp.txt
       while(myfile>>e[i].name>>e[i].age>>e[i].sal)
       {
           n++;   //count number employs in file
           i++;
       }
   }
   cout<<"Printing Original Data... "<<endl;
   for(int i=0;i<n;i++)
       cout<<e[i].name<<" "<<e[i].age<<" "<<e[i].sal<<endl;   //print readed data
   cout<<"Sort assending order with age "<<endl;
   for(int i=0;i<n;i++)       //sorting data with age as assending
       for(int j=i+1;j<n;j++)
           if(e[i].age>e[j].age)
           {
               //swap name
                   name1=e[i].name;
                   e[i].name=e[j].name;
                   e[j].name=name1;
               //swap age
                   age1=e[i].age;
                   e[i].age=e[j].age;
                   e[j].age=age1;
               //swap sal
                   sal1=e[i].sal;
                   e[i].sal=e[j].sal;
                   e[j].sal=sal1;
           }
   for(int i=0;i<n;i++)
       cout<<e[i].name<<" "<<e[i].age<<" "<<e[i].sal<<endl; //printing sorted data
   cout<<"Sort decending order with sal "<<endl;
   for(int i=0;i<n;i++)       //sorting data with sal as decending
       for(int j=i+1;j<n;j++)
           if(e[i].sal<e[j].sal)
           {
               //swap name
                   name1=e[i].name;
                   e[i].name=e[j].name;
                   e[j].name=name1;
               //swap age
                   age1=e[i].age;
                   e[i].age=e[j].age;
                   e[j].age=age1;
               //swap sal
                   sal1=e[i].sal;
                   e[i].sal=e[j].sal;
                   e[j].sal=sal1;
           }
   for(int i=0;i<n;i++)
       cout<<e[i].name<<" "<<e[i].age<<" "<<e[i].sal<<endl;   // printing sorted data
   return 0;  
}


emp.txt input file:-

phani 24 55000
surya 23 100000
rk 24 00000
deepas 24 33000
deepika 23 40000
latha 23 42220
madhu 22 20000
madhavi 23 55000


Output:-
1)

Enter 1 for give data manuvally or enter 2 for give through file
1
How many employs do you want to enter :
9
Enter name, age, salary of person 1:phani 24 55000

Enter name, age, salary of person 2:surya 23 100000

Enter name, age, salary of person 3:rk 24 00000

Enter name, age, salary of person 4:deepas 24 33000

Enter name, age, salary of person 5:deepika 23 40000

Enter name, age, salary of person 6:latha 23 42220

Enter name, age, salary of person 7:madhu 22 20000

Enter name, age, salary of person 8:madhavi 23 55000

Enter name, age, salary of person 9:janu 22 60000

Printing Original Data...
phani 24 55000
surya 23 100000
rk 24 0
deepas 24 33000
deepika 23 40000
latha 23 42220
madhu 22 20000
madhavi 23 55000
janu 22 60000
Sort assending order with age
madhu 22 20000
janu 22 60000
latha 23 42220
surya 23 100000
madhavi 23 55000
deepika 23 40000
deepas 24 33000
phani 24 55000
rk 24 0
Sort decending order with sal
surya 23 100000
janu 22 60000
madhavi 23 55000
phani 24 55000
latha 23 42220
deepika 23 40000
deepas 24 33000
madhu 22 20000
rk 24 0


Process exited normally.
Press any key to continue . . .

2)

Enter 1 for give data manuvally or enter 2 for give through file
2
Printing Original Data...
phani 24 55000
surya 23 100000
rk 24 0
deepas 24 33000
deepika 23 40000
latha 23 42220
madhu 22 20000
madhavi 23 55000
janu 22 60000
Sort assending order with age
madhu 22 20000
janu 22 60000
latha 23 42220
surya 23 100000
madhavi 23 55000
deepika 23 40000
deepas 24 33000
phani 24 55000
rk 24 0
Sort decending order with sal
surya 23 100000
janu 22 60000
madhavi 23 55000
phani 24 55000
latha 23 42220
deepika 23 40000
deepas 24 33000
madhu 22 20000
rk 24 0


Process exited normally.
Press any key to continue . .