CS 1713 Practice Problems #2

Students are asked to code the following table:
 Number Of passengers Yearly fee < 2 \$100 2 - 10 \$300 11 - 20 \$500 > 20 \$1000

All of the solutions are wrong! In each case, draw a flow chart of the solution and find at least one error in each proposed solution. Fill in the following table:

 passengers Sol #1 fee Sol #2 fee Sol #3 fee Sol #4 fee Sol #5 fee Sol #6 fee 1 2 5 10 11 15 20 21

• Proposed solution 1:
`     fee = 100;     if (passengers > 20)        fee = 1000;     if (passengers > 10)        fee = 500;     if (passengers > 2)        fee = 300;`

• Proposed solution 2:
`     fee = 100;     if (passengers > 2)        fee = fee + 200;     if (passengers > 10)        fee = fee + 400;     if (passengers > 20)        fee = fee + 900;`

• Proposed solution 3:
`     fee = 100;     if (passengers >= 2)        fee = fee + 200;     else if (passengers > 10)        fee = fee + 400;     else if (passengers > 20)        fee = fee + 900;`

• Proposed solution 4:
`     fee = 100;     if (passengers >= 2)        if (passengers >= 10)            if (passengers >= 20)                fee = fee + 900;            else                fee = fee + 400;         else            fee = fee + 200;`

• Proposed solution 5:
`     fee = 1000;     if (passengers < 20)        fee = 500;     if (passengers < 10)        fee = 300;     if (passengers < 2)        fee = 100;`

• Proposed solution 6:
`     fee = 100;     if (2 <= passengers && passengers <= 10)        fee = 300;     else if (10 < passengers && passengers <= 20)        fee = 500;     else        fee = 1000;`

Last revision: August 19, 2002 at 4:15 pm