# Answers to Chapter 10 Review Questions

### Chapter 10

 1: Declare an array variable called distances with elements of type double. Assign an array object with 5 elements to distances. Assign an array object to distances with the values 20.1, 30.7, 45.8, 19.1, 12.4, and 34.5. A: private double[] distances; distances = new double[5]; private double[] distances = {20.1, 30.7, 45.8, 19.1, 12.4, 34.5} ; 2: Consider the following array: ` private int[] numbers = { 10,5,3,15} ; ` What is the value of the following array element? numbers[1] What is the output from the following loop construct: ` for(int i = 3; i >= 0; i--) { Console.Write(" { 0} ", numbers[i]) } ` Write a loop using the foreach construct to traverse and print onscreen each element in numbers. This should result in the following output: 10 5 3 15. What is wrong with the following code that attempts to traverse the numbers array? What will happen when it is executed? ` for(int i = 0; i <= 4; i++) { Console.WriteLine(numbers[i]); } ` A: The value of numbers[1] is 5 15 3 5 10 ` foreach(int temp in numbers) { Console.Write(" {0}", temp); } ` The for loop repeats itself one time too many, so i becomes larger than 3 in the last loop and causes an IndexOutOfBoundsException. The loop condition should be i < 4 instead of i <= 4. 3: A class contains the following array declaration: ` private int[] heights = new int[3]; ` What is the value of each array element just after this array has been implicitly initialized? A line in your class looks like the following ` heights[4] = 10; ` Is this a valid statement? What will happen when this line is executed? A: Array elements of type int are implicitly initialized to 0. No, it is not a valid statement. The index 4 is out of range. When this line is executed, an IndexOutOfRangeException will be generated. 4: Why is it not a good idea to use an array to represent accounts of a real world bank? A: A bank has a constant need to delete and add new accounts. This calls for an array-like structure that dynamically can have new elements added and deleted. Arrays are not suited for this task because they have a fixed length once they are created. 5: Consider the following array declaration (same as in question 2): ` private int[] numbers = { 10,5,3,15} ; ` A fellow programmer has written the following method for retrieving values from the array: ` int GetNumber(int index) { return numbers[index]; } ` Adjust for the zero-based array index so that GetNumber(1) will return the first element value of numbers instead of the second element value, as is the case with the currently shown method. A: ` int GetNumber(int index) { return numbers[index - 1]; } ` 6: What is wrong with the following array declaration? ` byte [] ages = new byte [4] { 10, 34, 12, 19, 21, 56} ; ` A: [4] (positioned after byte) states that the length of the array is 4. However, the same line is attempting to assign six values at the same time; this difference is invalid. 7: Write a method called DisplayArray that accepts an array reference of base type int. The method must be able to print the values of the array object referenced by the argument onscreen, regardless of the array length. A: ` void DisplayArray(int[] tempArray) { for(int i = 0; i < tempArray.Length; i++) { Console.WriteLine(tempArray[i]); } } ` 8: Write a method with the following header: ` int [] AddNumber(int [] tempArray, int num) ` that will add num to every element of tempArray and return this array back to the caller. A: ` int [] AddNumber(int [] tempArray, int num) { for(int i = 0; i < tempArray.Length; i++) { tempArray[i] += num; } return tempArray; } ` 9: Consider the following two declarations: ` int [] myNumbers = { 2,4,6,8} ; int [] yourNumbers; ` Suppose that we assign myNumbers to yourNumbers and add 10 to the first element of yourNumbers, as in the following lines ` yourNumbers = myNumbers; yourNumbers[0] += 10; ` What is the value of myNumbers[0] after these statements have been executed? Explain what is going on. A: myNumbers and yourNumbers are referencing the same array object, so adding 10 to yourNumbers[0] is the same as adding 10 to myNumbers[0]. As a result, myNumbers[0] is equal to 12. 10: What is the fundamental difference between cloning an array with the array's Clone method and simply assigning the array variable value to another array variable? A: The Clone method creates an entire new array object with copies of array element values that are separate from those of the array for which the Clone method is called. In contrast, a simple assignment from one array variable to another only passes a reference along, which will cause the two array variables to reference the same array object. 11: Consider the two arrays from question 9, myNumbers and yourNumbers. Suppose each array variable references a different array object. Both these array objects contain exactly the same number of array elements and each pair of corresponding array elements have the same value. Will the following comparison be true or false? Explain why. ` (MyNumbers == YourNumbers) ` A: It is false because the comparison operator (==) tests whether the compared array variables are referencing the same object (reference-based comparison). 12: Your program contains a class called Planet. You are writing another class called SolarSystem, which in this case must consist of 10 planets. You want to represent the 10 planets in an array of 10 planets. Write the declaration you must insert into the SolarSystem class to enable the representation of the 10 planets. A: ` Planet [] planets = new Planet[10]; `

#### Answers to Chapter 10 Programming Exercises

 1: Implement a class called ArrayMath, containing the following methods to perform calculations on arrays: A method called ArrayAverage that, as an argument, accepts an array of base type double (of any length) and returns the average of this array. A method called ArraySum that accepts two arrays of base type int. The two arrays must have identical length. The method will add together each corresponding pair of array elements from the two arrays. Each sum is assigned to a corresponding element of a third array, which finally is returned to the caller. A method called ArrayMax that finds and returns the maximum value in an array of base type int. The array argument can be of any length. Write the code to test this class and its methods. A: Exercise 1: ` using System; class ArrayMath { public static double ArrayAverage(double [] tempArray) { double sum = 0; foreach(double temp in tempArray) { sum += temp; } return sum / tempArray.Length; } public static int [] ArraySum(int [] tempArray1, int [] tempArray2) { int [] sumArray = new int [tempArray1.Length]; for(int i = 0; i < tempArray1.Length; i++) { sumArray[i] = tempArray1[i] + tempArray2[i]; } return sumArray; } public static int ArrayMax(int [] tempArray) { int maxValue = -2147483648; foreach(int temp in tempArray) { if(temp > maxValue) maxValue = temp; } return maxValue; } } class Tester { public static void Main() { double [] distances = {100, 200, 300}; int [] agesTeam1 = {10, 20, 30}; int [] agesTeam2 = {34, 38, 31}; int [] sumArray; Console.WriteLine("Average distance of distances array: {0}", ArrayMath.ArrayAverage(distances)); Console.WriteLine("Max age in agesTeam1 array: {0}", ArrayMath.ArrayMax(agesTeam1)); sumArray = ArrayMath.ArraySum(agesTeam1, agesTeam2); Console.WriteLine("sumArray's element values: {0} {1} {2}", sumArray[0], sumArray[1], sumArray[2]); } } ` 2: Write the basic parts of a car game program. The program must include a Car class with the following members: An instance variable called position of type int A method with the header public void MoveForward(int distance) that adds distance to the position instance variable A method with the header public void Reverse(int distance) that deducts distance from position A method called GetPosition that simply returns the value of position to the caller Furthermore, the program must contain a class called CarGame that (by using an array) contains 5 objects of type Car. It must be possible to move each car (forward and reverse) and to get the position of each of the cars by providing an array index of the corresponding car. Write a small test program to ensure that the two classes function correctly. A: Exercise 2: ` using System; class Car { private int position = 0; public void MoveForward(int distance) { position += distance; } public void Reverse(int distance) { position -= distance; } public int GetPosition() { return position; } } class CarGame { private Car [] cars; public CarGame() { cars = new Car [5]; for(int i = 0; i < cars.Length; i++) { cars[i] = new Car(); } } public int GetCarPosition(int carIndex) { return cars[carIndex].GetPosition(); } public void MoveCarForward(int carIndex, int distance) { cars[carIndex].MoveForward(distance); } public void ReverseCar(int carIndex, int distance) { cars[carIndex].Reverse(distance); } } class CarGameTester { public static void Main() { CarGame testGame = new CarGame(); testGame.MoveCarForward(0, 100); testGame.MoveCarForward(1, 40); Console.WriteLine("Position of car 0: {0}", testGame.GetCarPosition(0)); Console.WriteLine("Position of car 1: {0}", testGame.GetCarPosition(1)); } } `

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

Similar book on Amazon