// Arup Guha
// 7/26/2011
// Written in class for BHCSI - Introduction to 2D arrays.
import java.util.*;

public class arrays2d {


	public static void main(String[] args) {

		int[][] table = new int[6][7];
		
		// Fill in subtraction table.
		for (int row=0; row<table.length; row++)
			for (int col=0; col<table[0].length; col++)
				table[row][col] = row - col;

		// Print out the results.
		print(table);
		
		// Try two searches.
		if (search(table, 5))
			System.out.println("Found 5.");
			
		if (search(table, 6))
			System.out.println("Found 6.");
			
		// Run count.
		System.out.println("Found 0 "+count(table, 0)+" times.");

	}

	// Prints out all the values in the table, tab separated by row.
	public static void print(int[][] table) {

		// Print out the table.
		for (int row=0; row<table.length; row++) {

			// Prints one row.
			for (int col=0; col<table[0].length; col++) {
				System.out.print(table[row][col] + "\t");
			}

			// Go to the next line.
			System.out.println();
		}

	}

	// Returns true iff searchVal exists in table.
	public static boolean search(int[][] table, int searchVal) {

		// Go through each row.
		for (int row=0; row<table.length; row++) {

			// Go through each item in the row.
			for (int col=0; col<table[0].length; col++) {

				// We found it!
				if (table[row][col] == searchVal)
					return true;
			}
		}

		// Completed search and didn't find it.
		return false;
	}

	// Returns the number of times searchVal appears in table.
	public static int count(int[][] table, int searchVal) {

		int timesfound=0;
		
		// Go through each row.
		for (int row=0; row<table.length; row++) {

			// Look at each item in the row.
			for (int col=0; col<table[0].length; col++) {

				// Add one to our counter since we found it.
				if (table[row][col] == searchVal)
					timesfound++;

			}
		}
		
		// The value to return
		return timesfound;
	}
}