I wanted to sort nuggets by unit price, so I made a class Nugget with the variables quant, and price, then made a double unit, which is price/quant. The nuggets come in 4, 6 and 9 packs
When I input 10, 10, and 10, for the price of each, I should get a sorted array of 9pack, 6pack and then a 4pack, because 10/9 is less than 10/6 and 10/4. But thats not the case.
#include <iostream>
using namespace std;
class Nugget {
public:
int price;
int quant;
double unit;
Nugget(int price, int quant) {
this->price = price;
this->quant = quant;
this->unit = price/quant;
}
};
int main(){
int n4,n6, n9;
cin >> n4 >> n6 >> n9;
Nugget* nuggetArr[3] = {new Nugget(n4,4), new Nugget(n6,6), new Nugget(n9,9)};
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (nuggetArr[j]->unit > nuggetArr[i]->unit) {
Nugget* temp = nuggetArr[i];
nuggetArr[i] = nuggetArr[j];
nuggetArr[j] = temp;
}
}
for (int j = 0; j < 3; j++)
cout << nuggetArr[j]->quant << ' ';
cout << endl << endl;
}
for (int i = 0; i<3; ++i)
cout << nuggetArr[i]->quant << ' ';
return 0;
};