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

c++ not sure why the before sort in this problem wont display when testing it, t

ID: 3839229 • Letter: C

Question

c++

not sure why the before sort in this problem wont display when testing it, the after sort works, but their are a bunch of random string of characters that appears? Maybe its my compiler? Using Microsoft Visual Studios

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

void selection_sort(double arr[], int size) {
   int i, j;
   for (i = 0; i<size; i++)
   {
       for (j = i + 1; j<size; j++)
       {
           if (arr[i]>arr[j])
           {
               double temp = arr[i];
               arr[i] = arr[j];
               arr[j] = temp;
           }
       }
   }

}

int main()
{

   int i;
   int ID[4];
   double hWorked[10], hRate[10], oTime[10], oTimePay[10], Gross[10], tRate[10], tAmount[10], nPay[10], RegP[10];
   char mStatus;
   string firstname[10], lastname[10];
   float sum = 0, average = 0;

   int n;
   cout << "Enter no. of employees: ";
   cin >> n;

   for (i = 0; i < n; i++)
   {
       cout << "Please enter your first name " << endl;
       cin >> firstname[i];
       cout << endl;

       cout << "Please enter your last name " << endl;
       cin >> lastname[i];
       cout << endl;

       cout << "Please enter the last four of your social security number. " << endl;
       cin >> ID[i];
       cout << endl;

       cout << "How many hours did this employee work this week? " << endl;
       cin >> hWorked[i];
       cout << endl;

       cout << "What is the pay rate for this employee? " << endl;
       cin >> hRate[i];
       cout << endl;

       cout << "What is the Marital Status of this Employee? S for single and M for Married or H as the Head of the Household: " << endl;
       cin >> mStatus;
       cout << endl;

       //calculations
       oTime[i] = hWorked[i] - 40;
       oTimePay[i] = oTime[i] * hRate[i];
       oTimePay[i] = oTimePay[i] * 1.5;
       Gross[i] = (hWorked[i] * hRate[i]) + oTime[i];

       //Tax Rate solution
       if (Gross[i] > 1000)
       {
           tRate[i] = 0.30;
           cout << "Your Tax Rate is " << tRate[i] << endl;
       }
       else if (Gross[i] >= 800 && Gross[i] <= 1000)
       {
           tRate[i] = 0.20;
           cout << "Your Tax Rate is " << tRate[i] << endl;
       }
       else if (Gross[i] >= 500 && Gross[i] <= 800)
       {
           tRate[i] = 0.10;
           cout << "Your Tax Rate is " << tRate[i] << endl;
       }
       else
       {
           tRate[i] = 0.00;
           cout << "Your Tax Rate is " << tRate[i] << endl;
       }

       //Marital Status Tax Addition or Break
       if (mStatus == 'S' || mStatus == 's')
       {
           tRate[i] += 0.05;
           cout << "Your Single this means you get an additional 5 percent added to your taxes this year. ";
           cout << "Your Tax Rate is now, " << tRate[i] << endl;
       }


       if (mStatus == 'H' || mStatus == 'h')
       {
           tRate[i] -= 0.05;
           cout << "Enjoy a 5 percent tax break as the head of the house hold! ";
           cout << "Your Tax rate is now, " << tRate[i] << endl;
       }


       tAmount[i] = Gross[i] * tRate[i];
       nPay[i] = Gross[i] - tAmount[i];
       RegP[i] = Gross[i] - oTimePay[i];

       // Averaging
       sum += nPay[i];
       average = sum / n;
   }

   for (i = 0; i < n; i++)
   {
       //Display
       cout << endl;
       cout << fixed;
       cout << setprecision(2);
       cout << "Dr. Ebrahimi's Payroll Institute " << endl;
       cout << left << setw(15) << firstname[i] << lastname[i] << endl;
       cout << left << setw(15) << "SSN: " << ID[i] << endl;
       cout << left << setw(15) << "HW: " << hWorked[i] << endl;
       cout << left << setw(15) << "HR: " << hRate[i] << endl;
       cout << left << setw(15) << "OTH: " << oTime[i] << endl;
       cout << left << setw(15) << "OTP: " << oTimePay[i] << endl;
       cout << left << setw(15) << "REGP: " << RegP[i] << endl;
       cout << left << setw(15) << "Gross: " << Gross[i] << endl;
       cout << left << setw(15) << "Tax: " << tAmount[i] << endl;
       cout << left << setw(15) << "NET: " << nPay[i] << endl;
       cout << endl;
   }
   cout << left << setw(15) << "SUM: " << sum << endl;
   cout << left << setw(15) << "Average: " << average << endl;

   int scan, pass, n1 = 10, i1;
  

   // I believe this is the problem in here
   cout << " Before sort: " << endl;
   for (i1 = 0; i1<sizeof(nPay) / sizeof(nPay[0]); i1++) {
       if (nPay[i]>0)
           cout << nPay[i1] << " ";
   }
   cout << endl;

   selection_sort(nPay, sizeof(nPay) / sizeof(nPay[0]));

   cout << " After sort: " << endl;
   for (i1 = 0; i1<sizeof(nPay) / sizeof(nPay[0]); i1++) {
       if (nPay[i]>0.00)
           cout << nPay[i1] << " ";
   }
   cout << endl;
  

   system("pause");
   return 0;
}

Explanation / Answer

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
void selection_sort(double arr[], int size) {
int i, j;
for (i = 0; i<size; i++)
{
for (j = i + 1; j<size; j++)
{
if (arr[i]>arr[j])
{
double temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
int main()
{
int i;
int ID[4];
double hWorked[10], hRate[10], oTime[10], oTimePay[10], Gross[10], tRate[10], tAmount[10], nPay[10], RegP[10];
char mStatus;
string firstname[10], lastname[10];
float sum = 0, average = 0;
int n,chk;
cout << "Enter no. of employees: ";
cin >> n;
chk=n;
for (i = 0; i < n; i++)
{
cout << "Please enter your first name " << endl;
cin >> firstname[i];
cout << endl;
cout << "Please enter your last name " << endl;
cin >> lastname[i];
cout << endl;
cout << "Please enter the last four of your social security number. " << endl;
cin >> ID[i];
cout << endl;
cout << "How many hours did this employee work this week? " << endl;
cin >> hWorked[i];
cout << endl;
cout << "What is the pay rate for this employee? " << endl;
cin >> hRate[i];
cout << endl;
cout << "What is the Marital Status of this Employee? S for single and M for Married or H as the Head of the Household: " << endl;
cin >> mStatus;
cout << endl;
//calculations
oTime[i] = hWorked[i] - 40;
oTimePay[i] = oTime[i] * hRate[i];
oTimePay[i] = oTimePay[i] * 1.5;
Gross[i] = (hWorked[i] * hRate[i]) + oTime[i];
//Tax Rate solution
if (Gross[i] > 1000)
{
tRate[i] = 0.30;
cout << "Your Tax Rate is " << tRate[i] << endl;
}
else if (Gross[i] >= 800 && Gross[i] <= 1000)
{
tRate[i] = 0.20;
cout << "Your Tax Rate is " << tRate[i] << endl;
}
else if (Gross[i] >= 500 && Gross[i] <= 800)
{
tRate[i] = 0.10;
cout << "Your Tax Rate is " << tRate[i] << endl;
}
else
{
tRate[i] = 0.00;
cout << "Your Tax Rate is " << tRate[i] << endl;
}
//Marital Status Tax Addition or Break
if (mStatus == 'S' || mStatus == 's')
{
tRate[i] += 0.05;
cout << "Your Single this means you get an additional 5 percent added to your taxes this year. ";
cout << "Your Tax Rate is now, " << tRate[i] << endl;
}

if (mStatus == 'H' || mStatus == 'h')
{
tRate[i] -= 0.05;
cout << "Enjoy a 5 percent tax break as the head of the house hold! ";
cout << "Your Tax rate is now, " << tRate[i] << endl;
}

tAmount[i] = Gross[i] * tRate[i];
nPay[i] = Gross[i] - tAmount[i];
RegP[i] = Gross[i] - oTimePay[i];
// Averaging
sum += nPay[i];
average = sum / n;
}
for (i = 0; i < n; i++)
{
//Display
cout << endl;
cout << fixed;
cout << setprecision(2);
cout << "Dr. Ebrahimi's Payroll Institute " << endl;
cout << left << setw(15) << firstname[i] << lastname[i] << endl;
cout << left << setw(15) << "SSN: " << ID[i] << endl;
cout << left << setw(15) << "HW: " << hWorked[i] << endl;
cout << left << setw(15) << "HR: " << hRate[i] << endl;
cout << left << setw(15) << "OTH: " << oTime[i] << endl;
cout << left << setw(15) << "OTP: " << oTimePay[i] << endl;
cout << left << setw(15) << "REGP: " << RegP[i] << endl;
cout << left << setw(15) << "Gross: " << Gross[i] << endl;
cout << left << setw(15) << "Tax: " << tAmount[i] << endl;
cout << left << setw(15) << "NET: " << nPay[i] << endl;
cout << endl;
}
cout << left << setw(15) << "SUM: " << sum << endl;
cout << left << setw(15) << "Average: " << average << endl;
int scan, pass, n1 = 10, i1;
  
// I believe this is the problem in here
cout << " Before sort: " << endl;
for (i1 = 0; i1<chk; i1++) { // have changed here initialised chk with the total number of employees dynamically
if (nPay[i]>0)
cout << nPay[i1] << " ";
}
cout << endl;
selection_sort(nPay,chk);
cout << " After sort: " << endl;
for (i1 = 0; i1<chk; i1++) {
if (nPay[i]>0.00)
cout << nPay[i1] << " ";
}
cout << endl;
  
system("pause");
return 0;
}

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