Thread: Stuck

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    81

    Stuck

    Well, you regular posters have already helped out people with this problem but I am having a LOT of trouble trying to satisfy a case in this problem. It's the wedding invitation problem that has been posted like twice on this forum already. Basically we have to find the cheapest way of ordering wedding invitations, given a box of 50 invites and 200 invites. There were multiple cases we had to account for. I have 2 of the three cases accounted for ("In some cases you might go with all of one package. In other cases, you might need to go with only the other package.") I've been messing around with if and while statements trying to satisfy the third case ("a mixture of the packages is necessary") but I have spent countless hours on this and I can't find a way that gives me the result I want. I understand what it's asking and I know how to do it logically, I just cannot seem to transfer it into code >.

    Here is my code:

    Code:
    #include <stdio.h>
    int main()
    
    #define     small_invites   50
    #define     large_invites   200
    
    {
        double cost_small, invites_cost, cost_large;
        int num_invites, order_small, order_large;
    
        printf("How much does a small package cost (in dollars)?\n");
        scanf("%lf", &cost_small);
    
        printf("How much does a large package cost (in dollars)?\n");
        scanf("%lf", &cost_large);
    
        printf("How many invitations are you sending out?\n");
        scanf("%d", &num_invites);
    
        if(cost_small < (cost_large / 4))
        {
            order_small = num_invites / small_invites;
            order_large = 0;
    
            if((num_invites % small_invites) != 0)
                order_small = order_small + 1;
        }
    
        else if(cost_large < cost_small)
        {
            order_small = 0;
            order_large = num_invites / large_invites;
    
            if((num_invites % large_invites) != 0)
               order_large = order_large + 1;
        }
    
        else
        {
            int remainder1 = num_invites - large_invites;
            int remainder2 = num_invites - small_invites;
    
            if((remainder1 * cost_large) < (remainder2 * cost_small))
            {
                // AGH IM SO CONFUSED
            }
        }
    
        printf("You should order %d small packages.\n", order_small);
        printf("You should order %d large packages.\n", order_large);
    
        printf("Your cost for invitations will be $%.2lf.", (order_small *cost_small) + (order_large * cost_large));
    
        return 0;
    }
    As you can see, I accounted for when you need to use all small or all large packages, I just am having trouble finding the combination. That last section of code was my final effort before posting here, because nothing I have tried has worked...

  2. #2
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    Basically I just don't know how to create a statement that allows the value of order_large and order_small to change based upon the amount of invitations I have left each time I buy a package..

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What exactly is the number of total invitations and the cost of each box (small and large), where your current program fails?

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    Well I know already that my statements for the combination of boxes isn't correct, or complete. As the code is in its current state, if you run the test values of ($50.99 - small, $150.99 - large, and 240 invitations needed) the result is 42000560 small packages and 7289064 large packages, resulting in a huge cost.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Right after you declare the variables add the initialization code like this

    Code:
    order_small = 0;
    order_large = 0;
    Note: This above will not fix the problem; but, might help you to see the cause of it.

    Tim S.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by shukiren View Post
    Well I know already that my statements for the combination of boxes isn't correct, or complete. As the code is in its current state, if you run the test values of ($50.99 - small, $150.99 - large, and 240 invitations needed) the result is 42000560 small packages and 7289064 large packages, resulting in a huge cost.
    You need to know the correct answer to the above input; before you can write code to do the operation correctly.

    How do you solve it with pencil and paper?

    Tim S.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Trace your code by hand. You'll find that for your example, you skip the if because cost_small is not less than 1/4th cost_large. You also skip the else if because cost_large is not less than cost_small. So you end up in the else clause, but you never set order_small or order_large in there, so you're printing them out uninitialized.

    I think you need to re-evaluate your if/else conditions. Think about the following:

    If the small package is cheaper per invitation, only buy small packages. Hint: repeated subtraction -- loop.
    If the large package is cheaper per invitation, and you need more than 200 invitations, keep using the large package until you have less than 200 invitations left to buy. More repeated subtraction.
    If the large package is cheaper per invitation, and you need, say, 130 invitations, that would require 3 small packages, or 1 large one. You then have to figure out which of those options is cheaper.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by shukiren View Post
    Well I know already that my statements for the combination of boxes isn't correct, or complete. As the code is in its current state, if you run the test values of ($50.99 - small, $150.99 - large, and 240 invitations needed) the result is 42000560 small packages and 7289064 large packages, resulting in a huge cost.
    I'd delete the arithmetic portion of your code, entirely - dude!

    First, figure out the cost per invitation of each pkg:

    lrgcostper1 = about .75 each (150.99/200)
    smcostper1 = about 1.00 each (50.99/50)

    Now, since .75 < 1.00, and we want to minimize the costs, we know we'll want to use the lrg boxes, as much as possible. For sure, up to 200, but you can't say what's best for the remainder yet.

    For 40 invitations still to figure, we need to just calculate the cost of getting 40 or more invitations, from each of the two pkg sizes:

    lrgpkg = 150.99
    smpkg = 50.99

    For 40 invitations, the small package is cheaper, So we need 1 small pkg for the 40 invitations portion, and 1 large pkg for the 200 invitations.

    Try thinking along those lines, and see if that helps.
    Last edited by Adak; 09-26-2011 at 12:07 PM.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    I feel ridiculous because I understand what you are suggesting, but I honestly do not know how to code loops well (I've tried coding your above suggestions and pretty much am just writing jibberish in codeblocks) >.<. We haven't had much practice with loops at all yet and my professor moves extremely quickly and does not explain much. I would have gone to his office hours for this but he was away all weekend, and his office hours are on Tuesday.

    This was in response to anduril462 btw.
    Last edited by shukiren; 09-26-2011 at 12:13 PM.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    I'll work on it some more after this class and then i'll post up what I've done.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Start off by declaring the variables: lrgcostper1 and smcostper1, and doing the arithmetic for each of those.

    Then use an if statement to compare the two and maybe use a cheapercostper1 variable to set equal to the result, that way the various names won't be so complicated. Simple is good!

    Now a while loop could be good:
    Code:
    if(lrgCostPer1 < smCostPer1) {
      cheaperPkgSize = large_invites;
    else
      cheaperPkgSize = small_invites;
    
    cheaperPkgNum = 0;
    while(cheaperPkgNum < invitationsNeeded)
        cheaperPkgNum += cheaperPkgSize;
    and that's the number of cheaper boxes you need to get (cheaper being the aforementioned cheaper per invitation size of box)

    I believe this is a difficult problem because we can solve it intuitively, based on practical experience and years of arithmetic. But the PC has none of that intuition, and so every single little thing has to have a variable for it, and get calculated and compared, etc. It's hard for us to go back to thinking in such a non-intuitive way.
    Last edited by Adak; 09-26-2011 at 12:35 PM.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Also, take a look at our tutorial here for some loop help: For, While and Do While Loops in C - Cprogramming.com.

  13. #13
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    I'm still just really not understanding the problem apparently. I understand what you are getting at when you find the number of cheapest boxes... but after that I'm still lost on what to code ... What's frustrating is that it's not a hard concept to grasp, I just obviously can't translate it. Sorry if I seem incompetent :/

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm pretty sure we covered all of this in the other thread on this:
    Code:
    if price per 50 < price per 200 / 4
        use only price per 50
    else
        use as many 200 boxes as you can
        use the remainder as 50

    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Here we go again...
    Join Date
    Sep 2011
    Location
    San Diego
    Posts
    102
    Code:
    #include <stdio.h>
    int main()
    
    #define     small_invites   50
    #define     large_invites   200
    
    {
        double cost_small, invites_cost, cost_large;
        int num_invites, order_small, order_large;
    
        printf("How much does a small package cost (in dollars)?\n");
        scanf("%lf", &cost_small);
    
        printf("How much does a large package cost (in dollars)?\n");
        scanf("%lf", &cost_large);
    
        printf("How many invitations are you sending out?\n");
        scanf("%d", &num_invites);
    
    //start by getting the price per invite
    // smallPricePerInvite = cost_small/small_invites;
    // largePricePerInvite = cost_large/large_invites;
    
    //next figure out which per per invite is cheaper
    //if the small pack is cheaper than or equal to the large
    //pack, find out how many small packs you'll need to get as close as //you can to the number of invites but not over.
    //HINT: use a while loop
    
    //if the large pack is cheaper, find out how many large
    //packs you'll need to get as close as you can to
    //the number of invites but not over.
    //HINT: use a while loop
    
    //calculate the remaining invites.
    //remainingInvites = num_invites - total;
    //total is found in one of the above while loops
    
    //Now you want to figure out if the remaining invites
    //would be cheaper to order a small packs or large
    //packs.
    
    //Then print!
    
        printf("You should order %d small packages.\n", order_small);
        printf("You should order %d large packages.\n", order_large);
    
        printf("Your cost for invitations will be $%.2lf.", (order_small *cost_small) + (order_large * cost_large));
    
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I'm stuck..please help me out..
    By kim_cam in forum C++ Programming
    Replies: 7
    Last Post: 04-02-2007, 04:57 PM
  2. im stuck, cant fix..HELP
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 08:59 AM
  3. Im stuck....
    By dAzed in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2004, 04:50 PM
  4. Stuck again.
    By campermama in forum C++ Programming
    Replies: 1
    Last Post: 06-17-2004, 10:19 PM
  5. Stuck
    By goosematt in forum C Programming
    Replies: 1
    Last Post: 11-10-2003, 07:44 AM