Alpha
Alpha

Reputation: 1

c++ sorting array function

      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); }

Upvotes: 0

Views: 113

Answers (1)

John Zwinck
John Zwinck

Reputation: 249592

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);

Upvotes: 1

Related Questions