C++ code A company keeps the database of its employees in the following format L
ID: 3582028 • Letter: C
Question
C++ code
A company keeps the database of its employees in the following format LastName FirstName ID Salary Assume there are no more than 20 employees in the company. Declare four(4) arrays in your main function to store the values for the four parameters th database file and read the appropriate parameter from the file to fill the array. Now write a function to sort the records by salary in descending order. Display the sorted database including first and last names, ID and salary on the screen Use Final Probl.txt available on Blackboard for reading in this program.Explanation / Answer
#include<iostream>
#include<fstream>
using namespace std;
void sort(string firstName[] ,string lastName[] ,int id[] ,double salary[] , int n){
double tempD = 0;
int tempI = 0;
string tempF;
string tempL;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(salary[j-1] < salary[j]){
//swap the elements!
tempD = salary[j-1];
salary[j-1] = salary[j];
salary[j] = tempD;
tempI = id[j-1];
id[j-1] = id[j];
id[j] = tempI;
tempF = firstName[j-1];
firstName[j-1] = firstName[j];
firstName[j] = tempF;
tempL = lastName[j-1];
lastName[j-1] = lastName[j];
lastName[j] = tempL;
}
}
}
}
int main() {
string firstName[20];
string lastName[20];
double salary[20];
int id[20];
int records = 0;
ifstream inputFile;
inputFile.open("FinalProb1.txt");
if (inputFile.is_open()) {
while (!inputFile.eof()) {
inputFile >> firstName[records]>>lastName[records]>>id[records]>>salary[records];
records++;
}
}
sort(firstName, lastName, id, salary, records);
cout<<"After sorting, employee details are: "<<endl;
for(int i=0; i<records; i++){
cout<<firstName[i]<<" "<<lastName[i]<<" "<<id[i]<<" "<<salary[i]<<endl;
}
inputFile.close();
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
After sorting, employee details are:
Anshu Murapaka 3333 50000
Revathi Kella 1111 30000
Sekhar Murapaka 2222 20000
Suresh Murapaka 1111 10000
FinalProb1.txt
Suresh Murapaka 1111 10000
Sekhar Murapaka 2222 20000
Anshu Murapaka 3333 50000
Revathi Kella 1111 30000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.