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

Using C++ Use the same scenario that we dealt in Problem 1. But now, there is a

ID: 3835654 • Letter: U

Question

Using C++ Use the same scenario that we dealt in Problem 1. But now, there is a change in requirement. You need to define an array of Employee instances, not just one instant. Assume array size of 20. This is how you can do it Employee emp[20]; Now, read the parameters from the file and assign it to suitable variable. In Problem 1, since you had one instance, you were continuously updating the variable as you read the next line. In this problem, since you are using arrays, you can store each line/set of parameters in different array location. This is how you can do it, for example, emp[i].salary=read_salary; // Assign salary read from file to salary variable of emp[i] structure instance Now, write a programmer defined function of void return type that will sort the array elements in the order of increasing salary. Pass your array to this function. Display the sorted list in the main function. HINT: Remember when you swap in your function, swap the structures, not the salary variable. You may use the bubble sort example on Chapter 7 Slide 57 (Display 7.13).

Explanation / Answer

#include<iostream>
#include<fstream>

using namespace std;
struct Employee {
string lastName;
string firstName;
double salary;
int id;
};

void buubleSort(struct Employee a[], int n) {


Employee temp;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(a[j-1].salary > a[j].salary){
//swap the elements!
temp = a[j-1];
a[j-1] = a[j];
a[j] = temp;
}

}
}
  
   }
int main() {
Employee emp[20];
int i = 0;
ifstream inputFile;
inputFile.open("HW10Prob1.dat");
if (inputFile.is_open()) {
while (!inputFile.eof()) {
inputFile>>emp[i].lastName>>emp[i].firstName>>emp[i].salary>>emp[i].id;
i++;
}
}
buubleSort(emp, i);
for(int j=0;j<i;j++){
cout<<"Last Name: "<<emp[j].lastName<<" "<<"First Name: "<<emp[j].firstName<<" "<<"Salary: "<<emp[j].salary<<" "<<"ID: "<<emp[j].id<<endl;
  
}
inputFile.close();

return 0;
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                                                                                              

sh-4.2$ main                                                                                                                                                                                                                                                           

Last Name: Lorena First Name: Emil Salary: 43000 ID: 225478                                                                                                                                                                                                            

Last Name: Tereza First Name: Santeri Salary: 48000 ID: 136478                                                                                                                                                                                                         

Last Name: John First Name: Harris Salary: 50000 ID: 113785                                                                                                                                                                                                            

Last Name: Yannic First Name: Lennart Salary: 58000.6 ID: 369854                                                                                                                                                                                                       

Last Name: Adam First Name: Johnson Salary: 68500 ID: 321258                                                                                                                                                                                                           

Last Name: Lisa First Name: Smith Salary: 75000.5 ID: 254785                                                                                                                                                                                                           

Last Name: Tristen First Name: Major Salary: 75800.8 ID: 102596                                                                                                                                                                                                        

Last Name: Sheila First Name: Smith Salary: 150000 ID: 165478

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote