1: |
|
A: |
|
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: |
|
A: |
|
4: |
|
A: |
|
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?
|
A: |
|
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
|
A: |
|
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
|
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}", TotalCarsCounted()); 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(); } } |