Using Mod Operator for problem

I have to make a program that goes as follows:

A company manufactures doodles which are then shipped to customers. Four sizes of shipping boxes are used: Huge, Large, Medium, and Small holding 50, 25, 10, and 1 doodle(s) respectively. Use named constants as needed for the number of doodles each size container can hold. Note: Small boxes will never hold more than 1 doodle.

Write a program that prompts the user to enter the number of doodles to be shipped then calculates the number of each size container needed for the shipment.


I have to use the mod operator but I just don't know how. I understand how the mod works but I don't understand how to use it in this problem. I thought the mod only gives you the remainder? So how do I use it to calculate the number of each size box?
I thought the mod only gives you the remainder?


Since you want to ship the doodles using the least number of containers, this is where the mod function comes in. It will give you the number of doodles remaining to be packaged by maximizing resources.

ex. Customer wants 128 doodles.

50 is closest to 128 so you do

128 % 50 == 2 r 28
So you need 2 Huge boxes

28 % 25 == 1 r 3
So you need 1 large box

3 % 1 == 3 r 0
So you need 3 small boxes

In total, you need 2 Huge boxes, 1 large and 3 small for an order of size 128.
That I understand.

But how will I know how many boxes to use if the mod function only outputs the remainder?
That is what you are trying to find out...

Write a program that prompts the user to enter the number of doodles to be shipped then calculates the number of each size container needed for the shipment.


ex. Customer wants 128 doodles.

50 is closest to 128 so you do

128 % 50 == 2 r 28 (mod returns 28)
So you need 2 Huge boxes

28 % 25 == 1 r 3 (mod returns 3)
So you need 1 large box

3 % 1 == 3 r 0 (mod returns 0 - so you stop here)
So you need 3 small boxes

In total, you need 2 Huge boxes, 1 large box and 3 small boxes for an order of size 128.


6 boxes

The question you should be asking is what box to use first and depending on what mod returned, what box to use next; and for this you will need to find a way to determine which of the capacities of the boxes is closest to the value you have without going over. Follow what I did above
Last edited on
Let me rephrase my question: When a customer wants 128 doodles, how am I supposed to know to use 2 huge boxes if the mod function only gives me the remainder?
You use the mod function to pass on the remainder, just like in a binary counter. Well, if you play minecraft you'll understand it better. GO MINECRAFT!

Analyze my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main(int nNumberofArgs,char* pszArgs[])
{
    int h,l,m,s,r = 0;
    int input;

    cout << "How many doodles do you want to be shipped?: ";
    cin >> input;
    if(cin.fail())
    {
        cout << "Please enter a number" << endl;
        system("PAUSE");
        return 0;
    }

    h = input / 50;
    r = input % 50;

    l = r / 25;
    r = r % 25;

    m = r / 10;
    r = r % 10;

    s = r / 1;

    cout << "You will need " << h << " huge boxes, " << l << " large boxes, " << m << " medium boxes, and " << s << " small boxes" << endl;
    system("PAUSE");
    return 0;
}
exactly what greenleaf800073 did. You get number of each box by dividing the order left by the capacity of that box. And as mentioned by greenleaf800073 and shown by my post, you use the remainder from the previous to get the amount left to be filled
Last edited on
Topic archived. No new replies allowed.