Answers to Chapter 9 Review Questions

   


Chapter 9

1:

What is the major difference between the while and the do-while statements? Why is the while statement most often the preferred type of statement?

A:

The while statement is an entry condition loop statement; the do-while is an exit condition loop statement. The entry condition loop may never be executed, whereas the do-while condition will be executed at least once. The entry condition loop is better suited to generate and traverse data sequences because perhaps the expected set of data is empty or perhaps no data should be generated in a particular run of the program.

2:

What is the output from the following piece of source code? (Mentally trace the code.)

 int counter = 0; while(counter < 4) {     counter++;     Console.Write(counter + " "); } 
A:
 1 2 3 4 
3:

What is the output from the following do-while construct? (Trace the code.)

 int counter = 4; do {     Console.Write(counter + " ");     counter--; } while(counter > 0); Console.WriteLine("Value of counter: {0} ", counter); 
A:
 4 3 2 1 Value of counter: 0 
4:

What is the output from the following for loop construct? (Trace the code.)

 for(int i = 0; i < 0; i += 2) {     Console.Write(i + " "); } 
A:
 0 2 4 6 
5:

Write a for loop that generates the following output:

 12  9  6  3  0  -3 
A:
 for(int i = 12; i >= -3; i -= 3) {     Console.Write(i + "  "); } 
6:

Is the following for loop valid (disregarding that it is not shown to be part of a complete program). If so, what output will it generate?

 for(int i = 1, j = 1; i < 5; i++, j += 2) {     Console.Write("  {0} ", i * j); } 
A:

It is valid. The output is as follows:

 1  6  15  28 
7:

What is the output from the following nested pair of for loops? (Trace the source code.)

 for(int i = 1; i < 4; i++) {     for(int j = 1; j < 5; j++)     {         Console.Write(" {0} ", i + j);     } } 
A:
 2 3 4 5 3 4 5 6 4 5 6 7 
8:

Consider the following for loop:

 for(int counter = 1;  ; counter++) {     Console.WriteLine("The value of counter: {0} ", counter);     Console.WriteLine("This value of counter * 10: {0} , counter * 10); } 

Syntactically, this for loop is valid. However, it is an infinite loop as it stands now, because it lacks a loop condition. Change the for loop so that at its fourth repeated loop it terminates between the two WriteLine statements. Thus, at its fourth repeated loop, it should only write

 The value of counter: 4 

and then terminate. Hint: You can use the break statement.

A:

A break statement has been inserted between the two WriteLine statements. It is executed whenever counter is greater than 3.

 for(int counter = 1; ; counter++) {     Console.WriteLine("Value of counter: {0}", counter);     if(counter > 3)         break;     Console.WriteLine("Value of counter * 10: {0}", counter); } 
9:

Consider the following for loop shown previously in Listing 9.8:

 for(int i = 0; i <= 20; i++) {     if(i % 4 == 0)         continue;     Console.Write(i + ", "); } 

Rewrite the for loop and eliminate the continue statement while keeping the semantics of the for loop intact.

A:
 for(int i = 0; i <= 20; i++) {     if(!(i % 4 == 0))         Console.Write(i + ", "); } 
10:

Name one advantage of pseudocode over flowcharts in relation to constructing structured programs.

A:

An arrow in a flowchart represents a movement of control from one part of the program to another. It is easy to draw arrows on a chart that goes to any place. However, sometimes the only way this can be implemented is through the error-prone goto statement. On the other hand, the format of pseudocode is very similar to C# source code (you write one line at a time and lines are mostly executed sequentially) and is highly compatible with the compact structured C# constructs, such as while loops, if statements, and so on.

Answers to Chapter 9 Programming Exercises

1:

The factorial is an important mathematical function. The factorial of a number n is written n!. n! is calculated as the product of the integers from 1 to n. For example 4! = 4 x 3 x 2 x 1. Furthermore 1! = 1, and 0! = 1. Write a program that receives an integer from the user, calculates its factorial, and returns this value as output. Allow the user (through a loop construct) to perform as many calculations as he or she wants during one program. After each calculation, the program must ask the user whether he or she wants to perform another calculation.

A:

Exercise 1:

 using System; class Calculator {     public static void Main()     {         string answer;         long factorial;         int number;         do         {             Console.Write("Please enter number: ");             number = Convert.ToInt32(Console.ReadLine());             factorial = 1;             if(number >= 0)             {                 for(int i = number; i >= 1; i--)                 {                     factorial *= i;                 }             }             else             {                 factorial = -1;             }             Console.WriteLine("{0} factorial = {1}", number, factorial);             Console.WriteLine("Perform another calculation Y)es N)o");             answer = Console.ReadLine().ToUpper();         }  while(answer != "N");         Console.WriteLine("Thank you for using the factorial calculator");     } } 
2:

Write a program that generates the following output:

 * ** *** **** ***** ****** ******* ******** 
A:

Exercise 2:

 using System; class StarTriangle {     public static void Main()     {         for(int i = 1; i <= 8; i++)         {             for(int j = 1; j <= i; j++)             {                 Console.Write("*");             }             Console.WriteLine();         }     } } 
3:

Write a program that generates the following output:

 * ######## ** ####### *** ###### **** ##### ***** #### ****** ### ******* ## ******** # 
A:

Exercise 3:

 using System; class MixedTriangles {     public static void Main()     {         for(int i = 1; i <= 8; i++)         {             for(int j = 1; j <= i; j++)             {                 Console.Write("*");             }             Console.WriteLine();             for(int k = 1; k <= 9 - i; k++)             {                 Console.Write("#");             }             Console.WriteLine();         }     } } 


   


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