0
      double gross_pay(double pay[7]){
int i;
double add;

add = 0;
for(i = 0; i < 7; i++){
    add += pay[i];
}

return add;
}

      int find_lowest(double pay[7]){
double lowest;
int index, i;

index = 0;
lowest = pay[0];

for(i = 1; i < 7; i++){
    if(pay[i] < lowest){
        index = i;
        lowest = pay[i];
    }
}

return index;

}

int find_highest(double pay[7]){
double highest;
int index, i;

index = 0;
highest = pay[0];

for(i = 1; i < 7; i++){
    if(pay[i] > highest){
        index = i;
        highest = pay[i];
    }
}

return index;

}



  int main()
{

string dayweek[7];
double dailypay[7];
int i;
double pay, add;
int lowest, highest;

dayweek[0] = "Monday";
dayweek[1] = "Tuesday";
dayweek[2] = "Wednesday";
dayweek[3] = "Thursday";
dayweek[4] = "Friday";
dayweek[5] = "Saturday";
dayweek[6] = "Sunday";


 for(i = 0; i < 7; i++){
    cout << "Please enter the gross sales ecieved on" << endl;
    cout << dayweek[i] << ": ";
    cin >> pay;
    dailypay[i] = pay;
    cout << endl << endl;
}

add = gross_pay(dailypay);
lowest = find_lowest(dailypay);
highest = find_highest(dailypay);


cout << endl;
cout << "Total sales for the week: " << "$" << add << endl << endl;
cout << "Total taxes withheld: $" << add*.0975 << "\n\n";
cout << "Net profit: $" << add*.9025 <<"\n\n";
cout << "Day with the highest sales amount was" << endl;
cout <<  dayweek[highest].c_str() <<" with a sales total of $" << dailypay[highest];
cout << "\n\nDay with the lowest sales amount was" << endl;
cout << dayweek[lowest].c_str() << " with a sales total of $" << dailypay[lowest];
  return 0;
    }

cant seem to figure out the code that will sort the array in order from lowest to highest for example Sales sorted from lowest to highest:

Monday had gross sales of $101.00

Tuesday had gross sales of $135.64

Sunday had gross sales of $245.55

Wednesday had gross sales of $533.44

Thursday had gross sales of $922.42

Saturday had gross sales of $1555.22

Friday had gross sales of $2242.63

those are also the input values for gross sales,

this is my first time asking a question on this site, if i did anything wrong please let me know,thanks for all the help !

could you write one using this

     void sortArray(double arySales[], int size, string aryDays[])
   {
     bool swap;
     double tempSales;
     string tempDays;

   do
  {
  swap = false;
  for (int count = 0; count < (size - 1); count++)
  {
     if (arySales[count] > arySales[count + 1])
     {
        tempSales = arySales[count];
        arySales[count] = arySales[count + 1];
        arySales[count + 1] = tempSales;

        tempDays = aryDays[count];
        aryDays[count] = aryDays[count + 1];
        aryDays[count + 1] = tempDays;

        swap = true;
     }
  }

} while (swap); }

1 Answer 1

1

You should put the day-of-week index (0 to 6) plus the value for that day into a struct, and then populate a std::vector of these structs. Then you can sort them easily:

struct PayForDay {
    int day; // 0..6
    double pay;
};

bool PayLess(const PayForDay& lhs, const PayForDay& rhs) {
    return lhs.pay < rhs.pay;
}

std::vector<PayForDay> payments;

std::sort(payments.begin(), payments.end(), PayLess);
Sign up to request clarification or add additional context in comments.

4 Comments

should have added im still in beginner stages, in my c++ book im learning off of im in linear searches, could you write the same code in a bubble sort ? thanks
No, but I'm sure you can implement your own bubble sort once you have the struct in place. The struct in a vector helps you keep the data organized, so you don't lose track when sorting of which pay amount goes with which day.
i added a sort function , off ex in my book, but im still not gettin it . Thanks
to display Sales sorted from lowest to highest:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.