Quarkphysics.ca



2D Array Algorithms ExcercisesExercise 1 — Sum of All Array ElementsComplete the following program so that it computes the sum of all the elements of the array. Write the program so that it works even if the dimensions of the rows and columns are changed. In other words, use length rather than hard-coded numbers.) import java.io.* ;class ArraySum{ public static void main ( String[] args ) { int[][] data = { {3, 2, 5}, {1, 4, 4, 8, 13}, {9, 1, 0, 2}, {0, 2, 6, 3, -1, -8} }; // declare the sum ??? // compute the sum for ( int row=0; row < data.length; row++) { for ( int col=0; col < ???; col++) { ??? } } // write out the sum System.out.println( ); }} Exercise 2 — Sum of Each RowComplete the following program so that it computes the sum of the elements in each row. import java.io.* ;class RowSums{ public static void main ( String[] args ) { int[][] data = { {3, 2, 5}, {1, 4, 4, 8, 13}, {9, 1, 0, 2}, {0, 2, 6, 3, -1, -8} }; // declare the sum ??? // compute the sums for each row for ( int row=0; row < data.length; row++) { // initialize the sum ??? // compute the sum for this row for ( int col=0; col < ???; col++) { ??? } // write the sum for this row System.out.println( ); } }} Exercise 3 — Find an elementWrite a program so that it finds the the first occurence of a given number in the array, e.g.?'13'. Print out the array address (row and column) of that element. After doing this, exit the loop and continue with the program ( just print “rest of program” to indicate that the program would continue here – ie. Do not use System.exit() ). ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download