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

NEED HELP PLEASE! Using C++, A company keeps the database of its employees in th

ID: 3581838 • Letter: N

Question

NEED HELP PLEASE!

Using C++, 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 the database file and read the appropriate parameter from the file to fill the array.

Now write a fuction to sort the records by salary in descending order. Display the sorted database including first and last names, ID and salary on the scree.

Use FinalProb1.txt

Here is the information for 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

Explanation / Answer

#define N 20
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

float convertToFloat(string s) {
   int i;
   for(i=0; i<s.length(); ++i)
       if (s[i]=='.')
           break;
  
   int startWithPower = i-1;
   float answer = 0;
  
   for(i=0; i<s.length(); ++i, startWithPower--) {
       if (s[i]=='.') {
           startWithPower++;
           continue;
           }
       answer += (s[i]-'0') * pow(10, startWithPower);
   }
   return answer;
          
}

void sortAndDisplay(string lastName[], string firstName[], string ID[], float salary[], int max) {
   int i, j;
   float temp;
   string tempString;
   for(i=0; i<max; ++i)
       for(j=0; j<max-i; ++j)
           if (salary[j]<salary[j+1]) {
               temp = salary[j];
               salary[j] = salary[j+1];
               salary[j+1] = temp;
              
               tempString = lastName[j];
               lastName[j] = lastName[j+1];
               lastName[j+1] = tempString;
              
               tempString = firstName[j];
               firstName[j] = firstName[j+1];
               firstName[j+1] = tempString;

               tempString = ID[j];
               ID[j] = ID[j+1];
               ID[j+1] = tempString;
               }
              
   for(i=0; i<max; ++i)
       cout<<lastName[i]<<" "<<firstName[i]<<" "<<ID[i]<<" "<<salary[i]<<" ";
}

int main() {
   string lastName[N], firstName[N], ID[N];
   float salary[N];
  
   int count = 0, posCount = 0;
   string s;
  
   ifstream infile("FinalProb1.txt");
   while(infile>>s) {
       switch (count%4) {
       case 0:
           lastName[posCount] = s;
           break;
       case 1:
           firstName[posCount] = s;
           break;
       case 2:
           ID[posCount] = s;
           break;
       case 3:
           salary[posCount++] = convertToFloat(s);
           break;
       default:break;
       }
       count++;
   }
  
   sortAndDisplay(lastName, firstName, ID, salary, posCount);
  
   return 0;
   }