English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

A microwave oven manufacturer recommends that when heating two items, add 50% to the heating time, and when heating three items double the heating time. Heating more than three items at once is not recommended.
Write a program that asks the user for the number of items and the single-item heating time. The program then writes out the recommended heating time.
Hint: do this with four successive single-branch if statements each of which tests for one of the four cases: 1 item, 2 items, 3 items, more than three items. Look at the sports car purchase program in the chapter to see how to do this, if you are stuck

2007-03-08 19:11:44 · 2 answers · asked by Anonymous in Computers & Internet Programming & Design

2 answers

I will just give you some hints.

In your main(), you can use cout to ask the user number of items (nItems) and heating time (nHeatingTime). Then use cin twice to get those two numbers.

Then the four if statements follow:

if (nItems == 1)
// leave the nHeatingTime as is
if (nItems == 2)
nHeatingTime = nHeatingTime + nHeatingTime / 2.0;
if (nItems == 3)
nHeatingTime = nHeatingTime * 2.0;
if (nItems > 3)
// tell the user that this is not recommended

The tricky part is using 2.0 instead of just 2. If you just use 2 to divide, you will lose the fractional part. For example, if your nHeatingTime is 7, then 7/2 = 3, when what you want is 3.5. This has to do with the fact that type int stores whole numbers only. For your case, you will want to use double for your nHeatingTime since you may have some fractional numbers. The type float will also work.

That being said, what type do you think nItems should be? You can't have a fraction of an item, only whole items. I will let you figure that one out.

2007-03-08 19:49:02 · answer #1 · answered by gradjimbo 4 · 0 0

easiest way i can think of is with 3 if/else statements

with it asking the user for the number of items before you start them. then doing each calculation in its own if/else statement and finally printing out the answer after the if/else statements. if you need more help email me or IM me and i'll give ya a hand

2007-03-08 19:23:01 · answer #2 · answered by dark hawk 2 · 0 0

fedest.com, questions and answers