Answers to Chapter 11 Review Questions

   


Chapter 11

1:
  1. How many dimensions does the array warpedWorld have if accessing one of its elements requires a line of code like the following:

     warpedWorld[2,4,1,6,4] 
  2. How many nested for loops would be required to access all elements in the warpedWorld array?

A:
  1. 5 dimensions

  2. 5 nested for loops

2:

You are considering writing a chess-playing program. How many dimensions would the array representing the chessboard need to have?

A:

2 dimensions.

3:
  1. Declare a rectangular three-dimensional array called observations with base type uint. Assign to it a reference to a new array object with the following numbers of elements in each dimension 5, 10, 20.

  2. Write a statement that assigns the value 100 to the observations element with indexes 3, 2, 10.

  3. What happens if you attempt to access the element with indexes 1, 12, 14?

  4. Write nested for loops to access and print onscreen the value of each element in the observations array.

  5. Use the foreach loop to perform the same operation as in question d.

A:
  1. uint [ , , ] observations = new uint [5,10,20];.

  2. observations[3,2,10] = 100;.

  3. The second index (12) is out of range and will trigger the runtime to generate an IndexOutOfRangeException.

  4.  for(int i = 0; i < 5; i++) {     for(int j = 0; j < 10; j++)     {         for(int k = 0; k < 20; k++)         {             Console.WriteLine(observations(i, j, k));         }     } } 
  5.  foreach(uint temp in observations) {     Console.WriteLine(temp); } 
4:
  1. Declare a jagged two-dimensional array called numbers of base type int, in which the first dimension contains 7 elements.

  2. Declare the third element of the first dimension to contain a one-dimensional array of length 20.

  3. Assign the value 100 to the fourth element in the array contained in the third element of the first dimension.

A:
  1. int [][] numbers = new int [7][];

  2. numbers [2] = new int [20];

  3. numbers[2][3] = 100;

5:

Suppose an array called numbers is declared as follows:

 int [] numbers = {2, 4, 6, 8, 10, 12, 6} ; 

Which values do the following method calls return?

  1. System.Array.IndexOf(numbers, 6)

  2. System.Array.IndexOf(numbers, 7)

  3. System.Array.IndexOf(numbers, 2, 3)

  4. System.Array.LastIndexOf(numbers, 6)

  5. System.Array.Reverse(numbers) followed by System.Array.IndexOf(numbers, 12)

A:
  1. 2

  2. 1

  3. 1

  4. 6

  5. 1

6:

Suppose that your program is searching an array containing one million data items by using the binary search algorithm. How many data items does it discard (and need not to look at again) from the search during the first search loop?

A:

Half of one million: 500000

7:

Your program needs to search for a value in an array of length 2048. What is the maximum number of loops required to find the value by using a

  1. Sequential search

  2. Binary search

A:
  1. If the sequential search begins at the beginning of the array and the key value is located at the end of the array, a maximum number of loops is required that amounts to 2048.

  2. After each loop of the binary search, half of the previous array segment is eliminated. Consequently, a maximum of 11 searches are needed because 2048 divided by 2 11 times is equal to 1. In other words, 211 = 2048.

Answers to Chapter 11 Programming Exercises

1:

Sometimes, the number of cars passing a certain point on a highway needs to be counted. Suppose a person needs to count the cars passing by every hour for one full week. Write a program that can assist this person in storing, retrieving, and analyzing this information. In particular the program must be able to

  • Store car count entries on an hourly basis for 24 hours a day and for 7 days.

  • Let the user enter a specific count by specifying the day, hour, and count.

  • Let the user retrieve a specific count by specifying the day and hour.

  • Calculate and display the total number of counts for the whole week.

  • Determine the number of hours it takes before a given number of cars have passed by. For example, if the first five hours contain the following counts 30, 40, 10, 50, and 100, it takes 3 hours to reach 75 or more cars, and 5 hours to reach 226 or more cars.

    Notice that the user expects the day indexes to be 1 7 (this requires an adjustment to the zero-base array access) and the hour indexes to be 0 23.

A:

Exercise 1:

 using System;  //From user's point of view the first day has number 1 because  //its index has been adjusted for the zero based index and the  //the first hour has number 0. So days has indexes from 1-7,  //hours has indexes from 0-23 class CarCounter {     private int [,] hourCarCounts = new int [7,24];     public CarCounter()     {          //Initialize all hourCarCounts elements to zero         for(int i = 0; i < 7; i++)         {             for(int j = 0; j < 24; j++)             {                 hourCarCounts[i,j] = 0;             }         }     }      //Display a menu,      //let the user enter a command and respond accordingly     public void Run()     {         string answer;         Console.WriteLine("I Input another value");         Console.WriteLine("G Get count for specific hour");         Console.WriteLine("S Calculate the total cars counted");         Console.WriteLine("H Calculate number of hours to reach a given car count");         Console.WriteLine("T Terminate");         do         {             Console.Write("\nPlease choose an option: ");             answer = Console.ReadLine().ToUpper();             switch(answer)             {                 case "I":                     InputCarCount();                 break;                 case "G":                     GetHourCount();                 break;                 case "S":                     Console.WriteLine("The total number of cars counted {0}",  graphics/ccc.gifTotalCarsCounted());                 break;                 case "H":                     HoursToReachCount();                 break;                 case "T":                     Console.WriteLine("Bye Bye!");                 break;                 default:                     Console.WriteLine("Invalid reply. Please try again");                 break;             }         } while (answer != "T");     }      //Let the user input a car count into a given day and hour     public void InputCarCount()     {         int day;         int hour;         int carCount;         Console.Write("Input day: ");         day = Convert.ToInt32(Console.ReadLine());         Console.Write("Input hour: ");         hour = Convert.ToInt32(Console.ReadLine());         Console.Write("Input car count: ");         carCount = Convert.ToInt32(Console.ReadLine());          // 1 is deducted from day in the next line to adjust          // for zero based index         hourCarCounts[day - 1,hour] = carCount;     }      //Finds the car count of a specific day and hour     public void GetHourCount()     {         int day;         int hour;         Console.Write("Enter day: ");         day = Convert.ToInt32(Console.ReadLine());         Console.Write("Enter hour: ");         hour = Convert.ToInt32(Console.ReadLine());          // 1 is deducted from day in the next line to adjust          // for zero based index         Console.WriteLine("Car count: {0}", hourCarCounts[day - 1, hour]);     }      //Finds the sum of all hourly car counts in hourCarCounts     public int TotalCarsCounted()     {         int sum = 0;         foreach(int temp in hourCarCounts)         {             sum += temp;         }         return sum;     }      //Calculates the hours it takes to reach a given car count     public void HoursToReachCount()     {         int reachCarCounts = 0;         int hourCount = 0;         int sumCarCounts = 0;         Console.Write("Input number of cars to be reached: ");         reachCarCounts = Convert.ToInt32(Console.ReadLine());         for(int i = 0; i < 7; i++)         {             for(int j = 0; j < 24; j++)             {                 hourCount++;                 sumCarCounts += hourCarCounts[i,j];                 if(sumCarCounts > reachCarCounts)                 {                     Console.WriteLine("Number of hours: {0}", hourCount);                     return;                 }             }         }         Console.WriteLine("The given count: {0} was never reached", reachCarCounts);     } } class Tester {     public static void Main()     {         CarCounter myCarCounter = new CarCounter();         myCarCounter.Run();     } } 
2:

Write a program containing a three-dimensional array of type int called numbers with the following dimensions: 5, 10, and 8. The user must be able to enter and retrieve individual values from this array by entering a chosen set of indexes. Further, the program must be able to calculate the sum of all numbers stored in all elements of the numbers array.

A:

Exercise 2:

 using System; class BoxOfNumbers {     private int [,,] numbers = new int [5,10,8];     public BoxOfNumbers()     {          //Initialize all numbers         for(int i = 0; i < 5; i++)         {             for(int j = 0; j < 10; j++)             {                 for(int k = 0; k < 8; k++)                 {                     numbers[i,j,k] = 0;                 }             }         }     }      //Display a menu,      //let the user enter a command and respond accordingly     public void Run()     {         string answer;         Console.WriteLine("I Input another number");         Console.WriteLine("G Get number for specific indexes");         Console.WriteLine("S Calculate the sum of the numbers");         Console.WriteLine("T Terminate");         do         {             Console.Write("\nPlease choose an option: ");             answer = Console.ReadLine().ToUpper();             switch(answer)             {                 case "I":                     InputNumber();                 break;                 case "G":                     GetNumber();                 break;                 case "S":                     Console.WriteLine("Sum of all numbers {0}", NumbersSum());                 break;                 case "T":                     Console.WriteLine("Bye Bye!");                 break;                 default:                     Console.WriteLine("Invalid reply. Please try again");                 break;             }         }  while (answer != "T");     }      //Lets the user input a number for a given set of indexes     public void InputNumber()     {         int indexDimension1;         int indexDimension2;         int indexDimension3;         int number;         Console.Write("Enter index for 1st dimension: ");         indexDimension1 = Convert.ToInt32(Console.ReadLine());         Console.Write("Enter index for 2nd dimension: ");         indexDimension2 = Convert.ToInt32(Console.ReadLine());         Console.Write("Enter index for 3rd dimension ");         indexDimension3 = Convert.ToInt32(Console.ReadLine());         Console.Write("Enter number: ");         number = Convert.ToInt32(Console.ReadLine());         numbers[indexDimension1, indexDimension2, indexDimension3] = number;     }      //Finds and displays the number for a given set of indexes     public void GetNumber()     {         int indexDimension1;         int indexDimension2;         int indexDimension3;         Console.Write("Enter index dimension 1: ");         indexDimension1 = Convert.ToInt32(Console.ReadLine());         Console.Write("Enter index dimension 2: ");         indexDimension2 = Convert.ToInt32(Console.ReadLine());         Console.Write("Enter index dimension 3: ");         indexDimension3 = Convert.ToInt32(Console.ReadLine());         Console.WriteLine("Number: {0}",             numbers[indexDimension1, indexDimension2, indexDimension3]);     }      //Finds the sum of all numbers     public int NumbersSum()     {         int sum = 0;         foreach(int temp in numbers)         {             sum += temp;         }         return sum;     } } class Tester {     public static void Main()     {         BoxOfNumbers myNumberBox = new BoxOfNumbers();         myNumberBox.Run();     } } 


   


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