Results 1 to 5 of 5

Thread: Need Help

  1. #1
    sushigeneral is offline Member
    Join Date
    Oct 2015
    Posts
    1
    Rep Power
    0

    Default Need Help

    I also do need help too - I'm not so sure why I'm getting some inaccurate outputs for my code. By the way, this is part of the AP computer science course in Eimacs.

    An array meals contains five elements corresponding to the meal codes 0 through 4 in Activity 6.2. For example, element meals[ 1 ] contains the number of "Chicken-based adult" meals requested, meals[ 4 ] contains the number of "Burger child" meals, and so on. Element meals[ 0 ] contains the number of requests in which none of the conditions for a particular meal type was met.

    Write code to store in the double cost the cost to the airline of providing the meals, where the cost for a meal of each type is specified in the table below. The number of meals in meals[ 0 ] should be split equally between "Chicken-based adult" and "Beef-based adult". If this number is not an even number, then the number allocated to "Chicken-based adult" should be one more than the number allocated to "Beef-based adult".

    Meal Code Meal Cost
    1 $1.75
    2 $2.08
    3 $2.14
    4 $0.98

    My code is the following:
    if (meals[0] % 2 != 0)
    cost = 1.75;
    else if (meals[1] >= 0)
    cost = 1.75;
    else if (meals[2] >= 0)
    cost = 2.08;
    else if (meals[3] >= 0)
    cost = 2.14;
    else if (meals[4] >= 0)
    cost = 0.98;
    else
    cost = 2.08;
    cost = (meals[0]*1.75) + (meals[1]*1.75) + (meals[2]*2.08) + (meals[3]*2.14) + (meals[4]*0.98);

    For test #2, the array is { 2,0,1,0,0 }. The expected output is $5.91 but I got $5.58?
    And for test #4, the array is {3,3,0,1,0}. The expected output is $12.97 but I got $12.64?

    The other two tests worked fine. Thank you for your help!

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: student need help

    This thread is six years old. Create your own thread.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Need Help

    Moved to own thread.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Need Help

    Your last compound expression where you compute the cost is the important one
    (not certain where you were going with the logical expression train).

    You did not properly calculate meal[0] costs. You need to divide meal[0] evenly for two
    types of meals, favoring one meal over the other for an odd value of meals[0].

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Need Help

    I was curious if this had been asked before so I did a web search on chicken-based-adult and
    found quite a few hits on different sites as long as two years ago. The spoon fed solutions were
    clearly posted by beginning programmers (but the diversity of the various solutions was interesting).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •