I need help sorting the item that was sold the most using this function: void so
ID: 670372 • Letter: I
Question
I need help sorting the item that was sold the most using this function:
void sort ( int listLength, int amount_sold[ ], float price[ ], string item_name[],bool (*compare) (int, int))
{ ……
if ((*compare) (amount_sold [i], amount_sold [j]) ) …. /*since they are parallel arrays, it is important that all arrays are sorted at the same time*/
}
here is the txt file of the items.
February 12, 2015
17$1.80 Premium Milk
10 $2.00 Berrytale
10 $3.50 Boroh Danish
15 $1.89 Marble Taro
12 $2.54 Chocolate Chip Bowl
15 $1.45 Mango Bread
Explanation / Answer
void sort ( int listLength, int amount_sold[ ], float price[ ], string item_name[],bool (*compare) (int, int))
{
int temp;
string temp_s;
for(int i = 0 ; i<listLength - 1 ; i++){
for(int j = i +1 ; j < listLength ; j++){
if(amount_sold[i] < amount_sold[j]){
//swap amount
temp = amount_sold[i];
amoun_sold[i] = amount_sold[j];
amount_sold[j] = temp;
//swap price
temp = price[i];
price[i] = price[j];
price[j] = temp;
//swap item name
temp_s = item_name[i];
item_name[i] = item_name[j];
item_name[j] = temp_s;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.