Notes - Tom Kleen

advertisement
Array algorithms
Create a new project called Day15-ArrayAlgorithms. Then do the following.
One-Dimensional Arrays
Write methods to do the following. Assume that list has been declared as an array of integers. You may
use the following code to initialize your array:
list = new int[] {10, 90, 20, 80, 30, 70, 40, 60, 50};
printList(list)
This is a void function. It prints the elements of the list, one per line to the Console (output window).
sumList(list)
This is an integer function (it returns an integer). It determines the sum of all of the elements in the list
and returns the sum.
reverseList(list)
This is a void function. It reverses the elements of the list (if there are n elements in the list, it swaps
items at locations 0 and n-1, 1 and n-2, 2 and n-3, etc., until all elements have been swapped.
max(list)
This is an integer function (it returns an integer). It determines the largest element in the list and returns
that value.
readList(ref list, fileName)
This is a void function. It reads the data from the file (whose name is passed in as the second argument)
into the array. Assume that the array is formatted as follows:
 First line has the number of remaining lines in the file
 Remaining lines have one integer per line
Read the first line and allocate the appropriate amount of memory for your array. Then read the
remaining lines into the array. Note that all input is strings, and that you must convert each string into
an integer before placing it in the array. You may copy the following into Notepad and use it as your
data file:
9
10
90
20
80
30
70
40
60
50
2/9/2016
Document1
Page 1 of 2
Two-Dimensional Arrays
Write methods to do the following. Assume that table is a 2-D array of integers. You may use the
following code to initialize your array:
table = new int[,] { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11 12} };
printRow(table, rowToPrint)
This is a void function. The table argument is the 2D array, and the rowToPrint argument is an integer
representing the row that is to be printed. Print the given row on the Console.
printColumn(table, columnToPrint)
This is a void function. The table argument is the 2D array, and the columnToPrint argument is an integer
representing the column that is to be printed. Print the given column on the Console. You may print it
horizontally; it doesn't have to be in a column.
readTable(ref table, filename)
This is a void function. It reads the data from the file (whose name is passed in as the second argument)
into the array. Assume that the array is formatted as follows:
 First line has the number of lines of data in the file (call it rows).
 Second line has the number of values (integers) on each line (call it columns).
 There are rows remaining lines in the data file with columns numbers on each line.
Read the first two lines and allocate the appropriate amount of memory for your array. Then read the
remaining lines into the array. Note that all input is strings, and that you must convert each string into
an integer before placing it in the array. You may copy the following into Notepad and save it as a text
file to use for input. You may assume that each integer on each line occupies 3 columns (allowing for
numbers up to 999).
3
4
1 2 3 4
5 6 7 8
9 10 11 12
2/9/2016
Document1
Page 2 of 2
Download