Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎notes‎ > ‎

2017-11-06

posted Nov 6, 2017, 9:59 AM by Konstantinovich Samuel   [ updated Nov 6, 2017, 11:25 AM ]

Goal: Arrays in 2D!

int[][]a = new int[4][3];

Remember row vs col:
                 Col 0     Col 1   Col 2
Row 0        a[0][0]   a[0][1] a[0][2]
Row 1        a[1][0]   a[1][1] a[1][2]
Row 2        a[2][0]   a[2][1] a[2][2]
Row 3        a[3][0]   a[3][1] a[3][2]



Practice problems/ Homework, due Monday. Sorry for late post!

2D Array Practice: 


Assume the inputs to your methods are valid 2d arrays. They do NOT have to be rectangular. 
Any method that tries to sum a row/column of a non-existant part of an array should count that value as a 0.
e.g.

[
 [1 2 3]
 [3 3]
 [4 1 1 9]
]

-summing column 2 would be (3 + 1) or just 4. 
-summing column 3 would be 9.
-summing row 1 would be (3+ 3) or 6,
-summing row 3 would be 0 since there are no values.

public class ArrayMethods{

   //PART 1 (these two methods are almost the same! -- I count them as 1 problem)
   public static int rowSum(int[][] AR, int x){}
    //returns the sum of the elements in Row x of AR. 

   public static int columnSum(int[][] AR, int x){}
    //returns the sum of the elements in Column x of AR (careful with rows of different lengths!).

   //PART 2
   public static int[] allRowSums(int[][] AR){}
    //calculates the row sum for every row and returns each of the values in an array. 
    //Index i of the return array contains the sum of elements in row i.

    public static int[] allColSums(int[][] AR){}
    //calculates the column sum for every column and returns each of the values in an array. 
    //Index i of the return array contains the sum of elements in column i, ignoring any rows that are too short.
    //The length of the returned array should be the length of the LONGEST array in AR.

   //PART 3 (just use your part 2 answer... look at the difference in the readability)
   public static boolean isRowMagic(int[][] AR){}
     //checks if the array is row-magic (this means that every row has the same row sum).

   public static boolean isColumnMagic(int[][] AR){}
    //checks if the array is column-magic (this means that every column has the same column sum).

}



Coming up next: You are writing a word search generator. 

The program should be able to:
1. generate an N by M puzzle 
    (A puzzle includes a list of words, and a grid of letters with those words hidden.)
2. generate a solution if requested.

Nice features we can add:
3. Use a plain text file as a list of which which words can be hidden.
4. Be able to reproduce the same puzzle/solution at a later date. This is a VERY useful feature.


IN CLASS:

 Brainstorm with your neighbors on the following points:
 - How to design a data structure to hold the puzzle, words, solution, etc.
 - How the overall program would place the words and how you could view a solution.
 - What tools in java do you need but not have yet?
Comments