# [Help] C++ Problem

Printable View

• 10-09-2004
Oxide
[Help] C++ Problem
I need help with an exercise in my C++ book that I cannot figure out, hopefully someone here can and can help me out.

Quote:

Daur's Flowers ships flowers in pots around the world, and orders are placed via the Internet.

a) Write a program that asks the user for the number of flower pots to be shipped, and then displays the number of boxes required. A big box can hold four pots, and a small box only one. Have the program calculate the smallest number of boxes required. The program output should look similar to:

Enter the number of flower pots to ship: <their value, e.g. 25>
We will ship:
6 big box(s)
1 small box(s)

b) Modify the program to allow for a very big box that can hold nine pots. The program output should look similar to:

Enter the number of flower pots to ship: <their value, e.g. 25>
We will ship:
2 very big box(s)
1 big box(s)
3 small box(s)
I have managed to get part A with the following code

Code:

```#include <iostream.h> int main()     {             int pots, big, small, remainder;             cout<<"Enter the number of flower pots to ship: ";             cin>>pots;             big = 4;                 remainder = pots%4;                 small = remainder;                 cout<<endl;                 cout<<(pots/big)<<" big box(s)"<<endl;                 cout<<(small)<<" small box(s)";         return 0;         }```
however, I cannot figure out how to incorporate 'very big boxes' into the mix and get a correct output.
• 10-09-2004
Epo
If you want to know the number of very big boxes needed to take a certain amount of pots, then you want to go with Integer Division, instead of the Mod (Remainder).

I.e. 25 Pots: Most Very Big Boxes = 2
25 / 9 = 2.7777777 , but, since C++ uses Integer Division (make sure to typecast any floats as integers, and if your variables are already integers, then you're set) 25 / 9 = 2.

9*2 = 18
25 - 18 = 7

That's the remaining number of pots that you'll need to fit in big boxes, and small boxes. Now use the same method to figure out the rest.

Hope that helps some.
• 10-09-2004
Hunter2
The problem is very similar to the change-calculating problem (if item costs x, and payment is y, how to give change with least number of coins). Someone had a thread about it before, at this link:
http://cboard.cprogramming.com/showt...ight=tendollar

If you're having a lot of trouble, you can look through the code there and see if you can get some ideas on how to do it. The code doesn't work 100% as is, but it can help if you've got a mental block at the moment.
• 10-09-2004
exluddite
Look at the logic of the thing. You're going to divide by 9, and possibly have a remainder. With that possible remainder you'll divide by 4, and again possibly have a remainder.