// *******************************************************************

// ArrayOperations16.java By: Aiman Hanna (C) 1993 - 2016

//

// This program illustrates more on two-dimensional arrays.

// The program shows how, for example, they can be passed as

// parameters to functions. You should notice that this is

// done in the fashion for passing one-dimension array.

// See ArrayOperations9.java for comparisons.

//

//Key Points: 1) Passing Two-Dimensional Arrays as Method Parameters

// *******************************************************************

import java.util.Scanner;

import java.util.Random;// Needed to generate random values

class ArrayModifier

{

publicstaticint intArrModifier(int[][] ar, int ov, int nv)

{

// This method will expect a two-dimensional array of integers,

// as well as two integer values.

// The method will search the array for every occurrence

// of the first passed value; the is ov, and if found replace

// it with a new value, that is nv.

// Notice that when an array is passed as a parameter, the

// lengths of the array are NOT to be specified.

int ctr = 0, i, j;

for (i = 0; i < ar.length; i++)

{

for (j = 0; j < ar[i].length; j++)

{

if (ar[i][j] == ov)

{

// if the value is found, change it and increase the counter

// to trace how many changes were performed at the end

ar[i][j] = nv;

System.out.println("A replacement has been made at index [" + i + "][" + j +"]");

ctr++;

}

}

}

return ctr;

}

}

publicclass ArrayOperations16

{

publicstaticvoid main (String[] args)

{

int i, j, rSize, cSize, counter =0, val1, val2;

Scanner kb = new Scanner(System.in);

// Create a Random object

Random rand = new Random();

// Allow the user to enter the dimensions of the array to be created

System.out.print("Please enter the row and column sizes of the array, respectively:");

rSize = kb.nextInt();

cSize = kb.nextInt();

// Create a two-dimensional array of integers

int[][] arr = newint[rSize][cSize];

// Initialize the array with random values between 0 and 99

// Now you must use "length" for both rows and columns

for (i = 0; i < arr.length; i++)// This will give the row length;

{// in other words how many rows

for (j = 0; j < arr[i].length; j++)// Notice the difference;

{// that is length of each column;

arr[i][j] = rand.nextInt(100);// 100 will force the generator

// to get a value between 0 & 99

}

}

// Show the contents of the array

System.out.println("Here are the contents of the array");

System.out.println("======");

for (i = 0; i < arr.length; i++)

{

for (j = 0; j < arr[i].length; j++)

{

System.out.printf("%4d", arr[i][j]);

}

System.out.println();

}

// Now ask the user to enter the value that he/she wishes to replace

System.out.println();

System.out.print("\nPlease enter two integers to search and replace respectively: ");

val1 = kb.nextInt();

val2 = kb.nextInt();

System.out.print("\nAttempting modifications on array arr \n");

counter = ArrayModifier.intArrModifier(arr, val1, val2);

if (counter > 0)

{

System.out.println(counter + " changes were made on arr1");

System.out.println("\nHere are the contents of arr1 after modifications: ");

System.out.println("======");

for (i = 0; i < arr.length; i++)

{

for (j = 0; j < arr[i].length; j++)

{

System.out.printf("%4d", arr[i][j]);

}

System.out.println();

}

}

else

System.out.println("Value " + val1 + " was not found in the array; no changes were made.");

kb.close();

}

}

/* The output

Please enter the row and column sizes of the array, respectively:6 10

Here are the contents of the array

======

53 94 61 70 81 91 32 84 52 20

34 66 50 32 15 42 4 27 87 4

98 15 40 23 48 42 31 27 6 54

32 91 15 74 35 49 75 3 11 5

10 44 58 51 93 14 84 34 11 3

77 37 29 73 94 0 3 72 60 65

Please enter two integers to search and replace respectively: 91 88

Attempting modifications on array arr

A replacement has been made at index [0][5]

A replacement has been made at index [3][1]

2 changes were made on arr1

Here are the contents of arr1 after modifications:

======

53 94 61 70 81 88 32 84 52 20

34 66 50 32 15 42 4 27 87 4

98 15 40 23 48 42 31 27 6 54

32 88 15 74 35 49 75 3 11 5

10 44 58 51 93 14 84 34 11 3

77 37 29 73 94 0 3 72 60 65

*/

/* Run Again

Please enter the row and column sizes of the array, respectively:6 12

Here are the contents of the array

======

56 67 80 39 6 75 4 98 35 36 68 79

22 67 12 57 54 67 25 97 11 78 81 85

88 52 9 81 81 64 65 58 88 27 41 32

24 62 36 52 62 64 59 47 74 15 73 6

26 25 30 79 26 59 7 13 98 45 3 57

25 35 12 73 64 97 50 61 44 54 97 44

Please enter two integers to search and replace respectively: 77 14

Attempting modifications on array arr

Value 77 was not found in the array; no changes were made.

*/