//illustrates several common algorithms that use 2D arrays.  
//EXERCISE: write a tester that uses these methods on simple arrays:
//use 2D initializer lists, as in A3
class Arrays2D {
    //method1: given a 2D array of doubles, prints all of its elements
    //NOTE: cannot assume arr is a rectangular array--rows may be variable-length
    //Exercise: re-implement this method using either a while loop or a do-while loop
    public static void printArray(double [][] arr) {
	for(int i=0; i<arr.length; ++i) {
	    for(int j=0; j<arr[i].length; ++j) {
		System.out.print(arr[i][j] + " "); //challenge: what if you put ' ' instead?
	    }
	    System.out.println();
	}
    }//printArray


    //method2: given an integer n, prints the following pattern:
    /*

     *
     **
     *** (n=3 here)

    */
    //this uses a 2D array, but we've gone over alternate implementations in class
    //challenge Q: is it possible to implement this method using initializer lists?
    //why or why not?
    //TODO: fix a bug in this method.  It is revealed by one of the tests in the tester
    public static void printPattern(int n) {
	//create a 2D array of characters, and fill it w/ the desired pattern
	char [][] pattern = new char[n][]; //why does this work? [does it? what happens in memory?]
	for(int i=0; i<n; ++i) {
	    pattern[i] = new char[i+1];
	    for(int j=0; j<pattern[i].length; ++j) {
		pattern[i][j] = '*'; //note this is of type char
	    }
	}//end initialize

	//now can just print the pattern--same as printArray but for chars
	for(int i=0; i<pattern.length; ++i) {
	    for(int j=0; j<pattern[i].length; ++j) {
		System.out.print(pattern[i][j]);
	    }
	    System.out.println();
	}	
    }
}

