Answers to Chapter 23 Review Questions

    next disabled - end of section


Chapter 23

1:

What is the output from the following method if you call it with the argument 1?

 01: public static void Recurs(int number) 02: { 03:     if(number > 8) 04:         return; 05:     else 06:     { 07:         Console.WriteLine("Number: { 0} ", number); 08:         Recurs(number * 2); 09:     } 10: } 
A:

Number: 1

Number: 2

Number: 4

Number: 8

2:

Identify in the Recurs method of question 1 the main ingredients found in most successful recursive methods.

A:

A branching statement (if-else) spanning lines 3 9. A base case in line 4, a recursive call (or recursive step) in line 8. Each recursive call moves towards the base case.

3:

Rewrite the Recurs method in question 1 so that it (by still using recursion) provides the following output if called with the argument 16.

 Number: 16 Number: 8 Number: 4 Number: 2 Number: 1 
A:

Two simple changes are needed in lines 3 and 8:

 01: public static void Recurs(int number) 02: { 03:     if(number < 1) 04:         return; 05:     else 06:     { 07:         Console.WriteLine("Number: {0}", number); 08:         Recurs(number / 2); 09:     } 10: } 
4:

Which technique provides for the most efficient solution recursion or iteration? Why?

A:

Iteration is more efficient than recursion because it does not require the generation of pending method instances (as opposed to recursion), which involves much overhead to store and manage by the runtime.

5:

The following Sum method is supposed to calculate the sum of a series of numbers starting at the argument passed to Sum and decrementing until 1 is reached. For example, Sum of 4 is supposed to be 4 + 3 + 2 + 1 = 10, and Sum of 1 is 1. However, Sum has a couple of missing parts. Make the corrections.

 public static int Sum(int number) {     return number + Sum(number 1); } 
A:

Sum is missing a branch statement and a base case. The following is the correct Sum method:

 public static int Sum(int number) {     if(number == 1)         return 1;     else     {         return number + Sum(number   1);     } } 
6:

Consider the ruler in Figure 23.4. Describe the fundamental logic behind a recursive method that can draw this ruler.

A:

If line height is lower than the shortest lines shown in the figure return (base case). Otherwise draw a line in the middle of the part of the ruler you are drawing. Draw a slightly lower line in the part to the left of the line just drawn (recursive call) and a line in the part to the right of the line (recursive call). The process begins by setting the line height to the longest of the vertical lines.

Answers to Chapter 23 Programming Exercises

1:

Write a recursive method call Count that takes a positive argument of type int. The Count method must be able to count the number of digits in the provided argument. For example, 2319 has four digits. Hint: If the number is less than 10, we have a base case (one digit); if the number of digits is 10 or greater, we can express the number of digits as 1+ the count of (number/10).

A:

Exercise 1:

 public static int Count(int number) {     if(number < 10)         return 1;     else         return 1 + Count(number / 10); } 
2:

Write a recursive method that prints out the name (with text) of each digit in a number provided. For example, if the number 2319 is provided, the method should print: two three one nine. hint: To print 2319, we can print 231 followed by the last digit 9. We can isolate 231 with the calculation 2319/10, and we can isolate 9 with 2319 % 10. A seperate method can be used to print a single digit as a word.

A:

Exercise 2:

 using System; class NumberProcessor {     public static void NumberToWord(int number)     {         if(number >= 10)             NumberToWord(number / 10);         Console.Write(DigitToText(number % 10) + " ");     }     public static string DigitToText(int digit)     {         switch (digit)         {             case 0:                 return "zero";             case 1:                 return "one";             case 2:                 return "two";             case 3:                 return "three";             case 4:                 return "four";             case 5:                 return "five";             case 6:                 return "six";             case 7:                 return "seven";             case 8:                 return "eight";             case 9:                 return "nine";             default:                 Console.WriteLine("Can only process single digits");                 return "Error";         }     } } class Tester {     public static void Main()     {         NumberProcessor.NumberToWord(3426);     } } 


    next disabled - end of section


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

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