Answers to Chapter 8 Review Questions

   


Chapter 8

1:

Which general type of statement would you use to implement each of the following logical descriptions:

  1. If number is greater than 100, write "greater than 100" onscreen; otherwise, write "less than or equal to 100."

  2. Repeatedly check each letter of a string until no more letters are left in the string.

A:
  1. The branching statement if-else.

  2. An iteration statement.

2:

Suppose that your program contains three variables called rainfall, wind, and temperature, all of type double. For each of the following points, write a few lines of code that implement the described logic (you don't need to write the whole program):

  1. If rainfall is greater than 100, write "Heavy rainfall" onscreen.

  2. If rainfall is greater than 100 or wind is greater than 120, write "Bad weather" onscreen.

  3. If rainfall is equal to 0 and wind is less than 10 and temperature is between 23 and 27 (Celsius), write "Nice weather."

  4. If rainfall is equal to 0, write "It is not raining"; otherwise, write "It is raining."

A:
  1.  if(rainfall > 100)     Console.WriteLine("Heavy rainfall"); 
  2.  if((rainfall > 100) || (wind > 120))     Console.WriteLine("Bad weather"); 
  3.  if((rainfall == 0) && (wind < 10) && (temperature >= 23) && (temperature <= 27))     Console.WriteLine("Nice weather"); 
  4.  if(rainfall == 0)     Console.WriteLine("It is not raining"); else     Console.WriteLine("It is raining"); 
3:

No matter which value rainfall has, the following lines will always write "It's raining" onscreen. Why?

 if(rainfall > 10);     Console.WriteLine("It's raining"); 
A:

The semicolon after the first line represents an empty statement, so the if statement could also be written as follows. If rainfall is greater than 0, the empty statement is executed; otherwise not. The last line is always executed.

 if(rainfall > 0) ; Console.WriteLine("It's raining"); 
4:

What is the purpose of nested if statements?

A:

It allows a program to choose among more than two alternative branches.

5:

Write logical expressions (using C#'s comparison operators and logical operators) that represent the following conditions:

  1. rainfall is between 100 and 150.

  2. number is odd and not equal to 23.

  3. number is even and smaller than 100, or weight is less than 100.

A:
  1. ((rainfall > 100) && (rainfall < 150))

  2. ((number % 2 != 0) && (number != 23))

  3. ((number % 2 == 0) || (weight < 100))

6:

A method with the header bool IsEven(int number) returns true if the argument passed to its parameter number is even; otherwise, it returns false. Write a Boolean expression containing a call to IsEven that is true if the number passed along with the call is odd.

A:

!(IsEven(someNumber)).

7:

To what does the term scope refer?

A:

Scope refers to the section of the source code where a particular variable identifier can be used. The scope of a variable begins at the point where it is declared and ends where the block in which it is declared terminates. The scope includes the blocks that might be nested inside this block.

8:

What is one of the few correct uses of the goto statement?

A:

To direct control to another switch section in a switch statement.

9:

Rewrite the following if-else multibranch statement using a switch statement.

 if(timeOfDay == "morning")     Console.WriteLine("Good Morning"); else if(timeOfDay == "midday")     Console.WriteLine("Good Day"); else if(timeOfDay == "evening")     Console.WriteLine("Good evening"); else     Console.WriteLine("Invalid time"); 
A:
 switch(timeOfDay) {     case "morning":         Console.WriteLine("Good Morning");     break;     case "midday":         Console.WriteLine("Good Day");     break;     case "evening":         Console.WriteLine("Good Evening");     break;     default:         Console.WriteLine("Invalid time");     break; } 
10:

To what does "falling through" in relation to the switch statement refer? How can "falling through" be prevented?

A:

Falling through is when the flow of execution moves from one switch section directly to the next switch section. This can be prevented by either a break or a goto statement.

11:

Your program contains variables called cost1, cost2, and minimumCost. Write a statement involving the conditional operator that assigns the smaller of the values in cost1 and cost2 to minimumCost.

A:

minimumCost = (cost1 < cost2) ? cost1 : cost2;

Answers to Chapter 8 Programming Exercises

1:

The currency on planet Blipos is called sopilbs. The aliens on Blipos pay the following amount of tax depending on their income:

  • 0% of income less than or equal to 10000 sopilbs

  • 5% of income that is greater than 10000 and less than or equal to 25000 sopilbs

  • 10% of income that is greater than 25000 and less than or equal to 50000 sopilbs

  • 15% of income that is greater than 50000 and less than or equal to 100000 sopilbs

  • 20% of income that is greater than 100000 sopilbs

So if an alien earns 60000 sopilbs, it will have to pay the following amount in tax: 0 * 10000 + 0.05 * 15000 + 0.1 * 25000 + 0.15 * 10000 = 4750.

Write a program that accepts a certain income as input and from that amount calculates and outputs the tax payable.

A:

Exercise 1:

 using System; class TaxCalculator {     public static void Main()     {         double income;         double tax;         Console.Write("Enter income: ");         income = Convert.ToDouble(Console.ReadLine());         if(income <= 10000)             tax = 0;         else if((income > 10000) && (income <= 25000))             tax = (income - 10000) * 0.05;         else if((income > 25000) && (income <= 50000))             tax = (15000 * 0.05) + ((income - 25000) * 0.1);         else if((income > 50000) && (income <= 100000))             tax = (15000 * 0.05) + (25000 * 0.1) + ((income - 50000) * 0.15);         else             tax = (15000 * 0.05) + (25000 * 0.1) + (50000 * 0.15)+ ((income - 100000) *  graphics/ccc.gif0.2);         Console.WriteLine("The tax is: {0}", tax);     } } 
2:

Write a program (using a switch statement) that accepts one of the exam scores A, B, C, D, and E. In response, the program will output the corresponding percentage score, which is A: 90 100; B: 80 89; C: 70 79; D: 60 69; E: 0 59.

A:

Exercise 2:

 using System; class ExamScoreConverter {     public static void Main()     {         string score;         Console.WriteLine("Enter score you wish to convert");         score = Console.ReadLine().ToUpper();         switch(score)         {             case "A":                 Console.WriteLine("90-100 percent score");             break;             case "B":                 Console.WriteLine("80-89 percent score");             break;             case "C":                 Console.WriteLine("70-79 percent score");             break;             case "D":                 Console.WriteLine("60-69 percent score");             break;             case "E":                 Console.WriteLine("0-59 percent score");             break;             default:                 Console.WriteLine("Invalid exam result provided");             break;         }     } } 
3:

Write a program that can find a number between 0 and 100. The user decides on a number and the program repeatedly makes a guess and constantly narrows in on the number until the number is found. Hint: Let the computer guess on the number in the middle of the possible maximum number and the possible minimum number. For example, the first guess should be 50. If the user indicates the number to be greater, the next guess should be 75, and so on.

A:

Exercise 3:

 using System; class NumberFinder {     public static void Main()     {         int maxNumber = 100;         int minNumber = 0;         bool found = false;         int guessCounter = 0;         string reply;         do         {             guessCounter++;             Console.WriteLine("My guess is: {0}", ((maxNumber + minNumber) / 2));             Console.WriteLine("Enter H for H)igher or L for L)ower C for C)orrect?");             reply = Console.ReadLine().ToUpper();             if(reply == "H")                 minNumber = (maxNumber + minNumber) / 2;             else if (reply == "L")                 maxNumber = (maxNumber + minNumber) / 2;             else                 found = true;         }  while (!found);         Console.WriteLine("I found the number in {0} guesses", guessCounter);     } } 


   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net