Thread: Please help me figure out what is wrong with my program!

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    6

    Please help me figure out what is wrong with my program!

    Code:
    Problem A: Wedding Invitations (invite.c)
    Though Arup wants to just send a free E-vitation to all of his guests for his wedding, his fiancee will have none of it. She has put her foot down and has insisted that they mail out regular invitations. After doing some research, Arup found out that depending on the size of the packages of invitations ordered, there are different prices. He wants you to figure out how many of each package to order, in order to get enough invitations to mail out at the cheapest price. For the purposes of this problem, there are only two packages:
     
    1) One with 50 invitations
    2) One with 200 invitations
     
    Each vendor has a different price for the two packages. You’ll ask the user to input these two prices, as well as the total number of invitations that need to be sent out. Your job will be to calculate how many of each package to buy for the best deal, and how much will be spent on invitations.
     
    Input Specification
    1. The cost of both packages (in dollars) will be positive real numbers less than 500.
    2. The number of invitations needed will be a positive integer less than 10000.
     
    Output Specification
    The first line of output will specify the number of small packages to buy to minimize the cost of the order of invitations using the following format:
     
    You should order X small package(s).
     
    where X is the number of small packages to order.
     
    The second line of output will specify the number of large packages to buy to minimize the cost of the order of invitations using the following format:
    You should order Y small package(s).
     
    where Y is the number of small packages to order.
    The last line of output should include the total cost of the packages using the following format:
     
    Your cost for invitations will be $Z.
     
    where Z is the minimum cost in question outputted to two decimal places.
     
    Output Sample
    Below is one sample output of running the program. Note that this sample is NOT a comprehensive test. You should test your program with different data than is shown here based on the specifications given above. In the sample run below, for clarity and ease of reading, the user input is given in italics while the program output is in bold. (Note: When you actually run your program no bold or italics should appear at all. These are simply used in this description for clarity’s sake.)
     
    Sample Run #1
    What is the cost of a small package (in dollars)?
    50.99
    What is the cost of a large package (in dollars)?
    150.99
    How many invitations are you sending out?
    240
     
    You should order 1 small package(s).
    You should order 1 small package(s).
    Your cost for invitations will be $201.98.
     
    Note: There are actually many somewhat tricky test cases for this problem. Keep in mind that the cost of the two packages might be anything. In some cases you might go with all of one package. In other cases, you might need to go with only the other package. Finally, in cases like this sample, a mixture of the packages is necessary.
    
    
    
    #include <stdio.h>
    #include <math.h>
    
    #define SM_PACK 50
    #define LG_PACK 200
    
    int main() {
    
        int num_invite;
        int indv_sm, indv_lg;
        double num_sm, num_lg;
        double remain;
        double price_sm, price_lg;
        double total_sm, total_lg;
        double total_price;
    
    
        printf("What is the cost of a small package (in dollars)?\n");
        scanf("%lf", &price_sm);
        printf("What is the cost of a large package (in dollars)?\n");
        scanf("%lf", &price_lg);
        printf("How many invitations are you sending out?\n");
        scanf("%d", &num_invite);
    
        if (4*price_sm > price_lg) {
    
            if (num_invite%LG_PACK == 0) {
    
                num_lg = num_invite/LG_PACK;
                total_lg = price_lg*num_lg;
                total_price = total_lg;
            }
    
             else if (num_invite%LG_PACK != 0) {
    
                    if (3*price_sm <= price_lg && num_invite%LG_PACK <= 150) {
    
                        num_lg = num_invite/LG_PACK;
                        total_lg = price_lg*num_lg;
                        indv_lg = LG_PACK*num_lg;
                        remain = num_invite - indv_lg;
                        num_sm = ceil(remain/SM_PACK);
                        total_sm = num_sm*price_sm;
                        total_price = total_sm + total_lg;
    
                    }
                    else {
                        num_lg = (num_invite/LG_PACK) + 1;
                        total_lg = price_lg*num_lg;
                        total_price = total_lg;
                    }
    
             }
        }
    
        else if (4*price_sm <= price_lg) {
    
            if (num_invite%SM_PACK == 0) {
                num_sm = num_invite/SM_PACK;
                total_sm = price_sm*num_sm;
                total_price = total_sm;
            }
    
            else {
                total_sm = price_sm*num_sm;
                total_price = total_sm;
            }
    
        }
    
        printf("You should order %.0lf small package(s).\n", num_sm);
        printf("You should order %.0lf large package(s).\n", num_lg);
        printf("Your cost for invitations will be $%.2lf.\n", total_price);
    
      return 0;
    
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Do some searches... i think you'll find this program --almost exactly your program, in fact-- were recently torn apart in a cople of other threads on these forums. Might be some help there.

    Otherwise you need to ask a specific question about your own code...
    and please don't put whole messages in code tags... just source code.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I doubt if you can order a part of a package, large or small. Those should be integers. The money should stay double.

    We've gone over this two ways - dynamically, and statically, so there are several posts in those threads - and yes, it gets a bit tricky. I wouldn't use 4 * price of the small being less than the price of the large. There's more to it than that.

    1) get the number of invitations needed, and the prices of each package
    2) compute the cost per invitation, from both package sizes
    3) Use the lesser price per invitation for as many as you need, in whole packages only
    4) for the left over amount of invitations needed, calculate the number of smaller packages, and that price per invitation, to at least get the amount up to the amount you need, in total.

    5) and compare that price, with the price of one more (larger) package. Don't bother with what's left over, you don't care about that, for this problem.
    6) add that final package purchase cost to the sum of the earlier invitation package(s) cost.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help? I can't figure out what's wrong with this?
    By kaiser2614955 in forum C Programming
    Replies: 24
    Last Post: 07-29-2011, 07:15 PM
  2. Trying to figure what I'm doing wrong in this program
    By Magmadragoon in forum C Programming
    Replies: 6
    Last Post: 03-29-2011, 07:17 AM
  3. Help! I cannot figure out what I'm doing wrong here!
    By chsindian595 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 03:18 AM
  4. please help, i can't figure out what's wrong
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2002, 09:13 PM
  5. Replies: 0
    Last Post: 11-11-2001, 01:24 PM