210k views
0 votes
C++

Convert totalOunces to pints, cups, and ounces, finding the maximum number of pints, then cups, then ounces.

Ex: If the input is 63, then the output is:

Pints: 3
Cups: 1
Ounces: 7
Note: A pint is 16 ounces. A cup is 8 ounces.

More:
First, the number of pints is found using integer division on the total ounces. Then, the remaining amount is found using modulo.

Next, the number of cups is found using integer division on the remaining ounces. The remaining amount is updated using modulo.

Finally, the number of ounces is assigned with the remaining ounces.

User Migz
by
7.7k points

1 Answer

4 votes

#include <iostream>

#include <cmath>

void totalOunches(int n) {

int count[3];

for(int i=0; i<3; i++) {

int general_term = (std::pow(i+1,2) - (19*(i+1)) + 50) / 2;

count[i] = n / general_term;

n -= general_term * count[i];

}

std::cout << "\\Pints: " << count[0]

<< "\\Cups: " << count[1]

<< "\\Ounches: " << count[2]

<< std::endl;

}

int main(int argc, char* argv[]) {

int idx;

std::cin >> idx;

totalOunches(idx);

return 0;

}

C++ Convert totalOunces to pints, cups, and ounces, finding the maximum number of-example-1
User Bazza
by
7.7k points