1. Member
Join Date
Oct 2015
Posts
1
Rep Power
0

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!

3. Re: Need Help

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

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

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

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

Posting Permissions

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