Thread: [Help] C++ Problem

  1. #1
    Registered User
    Join Date
    Oct 2004

    [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.

    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

    #include <iostream.h>
    int main()
        	int pots, big, small, remainder;
        	cout<<"Enter the number of flower pots to ship: ";
        	big = 4; 
    		remainder = pots%4;
    		small = remainder;
    		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.

  2. #2
    Registered User
    Join Date
    Jun 2003
    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.
    Last edited by Epo; 10-09-2004 at 07:35 PM.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    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:

    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.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Apr 2004
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM