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

1. Please help! :) Simple C++ Programming. Please make sure it compiles. FinalPr

ID: 3581824 • Letter: 1

Question

1. Please help! :) Simple C++ Programming. Please make sure it compiles.

FinalProb1.txt:

John Harris 1000 50000.00
Lisa Smith 1002 75000.00
Adam Johnson 1007 96000.00
Sheila Smith 1009 39580.00
Tristen Major 1012 115000.00
Yannic Lennart 1015 96000.00
Lorena Emil 1018 69000.00
Tereza Santeri 1020 85000.00

A company keeps the database of its employees in the following format Last Name EinstName 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

// C++ code
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string> // std::string, std::to_string
#include <math.h>
#include <fstream>

using namespace std;

void sort(string lastName[], string firstName[], int id[], double salary[], int size)
{
   for (int i = 0; i < size; ++i)
   {
       for (int j = 0; j < size; ++j)
       {
           if(salary[i] < salary[j])
           {
               int t = id[i];
               id[i] = id[j];
               id[j] = t;

               string s = firstName[i];
               firstName[i] = firstName[j];
               firstName[j] = s;

               s = lastName[i];
               lastName[i] = lastName[j];
               lastName[j] = s;

               double r = salary[i];
               salary[i] = salary[j];
               salary[j] = r;
           }
       }
   }
}

int main()
{
   string lastName[20];
   string firstName[20];
   int id[20];
   double salary[20];
int size = 0;

ifstream inFile ("FinalProb1.txt");
if (inFile.is_open())
{
while(true)
{
if(inFile.eof())
break;

inFile >> firstName[size] >> lastName[size] >> id[size] >> salary[size];
size++;
}
inFile.close();
}
else cout << "Unable to open file";

sort(lastName, firstName, id, salary, size);

cout << "Sorted Employee details ";
for (int i = 0; i < size; ++i)
{
cout << id[i] << " " << firstName[i] << " " << lastName[i] << " " << salary[i] << endl;
}

return 0;
}


/*

FinalProb1.txt:
1000 John Harris 256314765 80000
1002 Lisa Smith 225365256 50000
1007 Adam Johnson 215236645 65800
1009 Sheila Smith 256214856 75000
1012 Tristen Major 236456425 58000
1015 Yannic Lennart 256452256 99000
1018 Lorena Emil 225453664 125000
1020 Tereza Santeri 245123655 38000


output:

Sorted Employee details
1009   Sheila Smith   39580
1000   John Harris   50000
1018   Lorena Emil   69000
1002   Lisa Smith   75000
1020   Tereza Santeri   85000
1007   Adam Johnson   96000
1015   Yannic Lennart   96000
1012   Tristen Major   115000

*/