CS100a Sections

Oct 27-28

Topics

2D array problems

checkers assignment

For each of these problems, write a segment of Java code that solves it. You do not need to write complete programs or functions.

For all of the problems assume the following data definitions are to be used.

final int A_Size = 5;

final int B_Size = 5;

int [][] A = new int[A_Size][A_Size];

int [][] B = new int[B_Size][B_Size];

int r, c; // row and column numbers

Assume also that A, B, r, and c have been initialized.

 

1. Write a segment of code that prints the maximum value in row number r

and also prints the number of the column in which it occurs.

2. Write a segment of code that prints the maximum value in column number c

and also prints the number of the row in which it occurs. (no solution included)

3. Write a segment of code that prints the maximum value in all of A and

the postion in which it occurs.

 

4. Write a segment of code that changes A to its transpose. This means

that the rows and columns of A are interchanged, which, for a square matrix

is the same as flipping the values over the main diagonal.

Example: If A is now

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

It should be changed to

1 6 11 16 21

2 7 8 9 10

3 8 13 18 23

4 9 14 19 24

5 10 15 20 25

 

5. Assign B values so that it looks like the first picture of A in question 4 above. Hint: Go through B in row major order counting as you go.

6. Assign B values so that it looks like the second picture of A in question 4 above. Hint: Go through B in column major order counting as you go.

 

7. Search the major diagonal of A to see if it contains any zeros and report your answer. Your search should stop as soon as you discover a zero if there are any. The major diagonal is the one that starts in the upper left corner and runs down to the lower right corner.

8. Do the same as in question 7 but for the minor diagonal (which goes from the upper right corner to the lower left one).

 

 

 

 

//Question 1

int max = A[r][0]; // maximum value in row r

int maxcol = 0; // col containing

// Check the rest of row r for a larger value

for (int j = 1; j< A_Size; j++) {

if (A[r][j] > max) {

max = A[r][j];

maxcol = j;

}

}

System.out.println("The max val in row r is: "

+ " + max);

System.out.println( "It occurs in column " +

maxcol);

 

 

 

//Question 3

max = A[0][0]; // max value in the array

maxrow = 0; // row position of max

maxcol = 0; // col position of max

for (int i = 0; i <= A_Size -1; i++) {

// Go through row i

for (int j = 0; j< A_Size; j++) {

if (A[i][j] > max) {

max = A[i][j];

maxrow = i;

maxcol = j;

}

}

}

System.out.println ("Max val in A is: "

+ max);

System.out.println ("It occurs in row "

+ maxrow + " column " + maxcol);

 

 

// Question 4

int temp;

// Go through the triangle in the lower // left, swapping each value with the // corresponding element in the upper

// right triangle.

for (int i = 1; i < A_Size; i++)

for (int j = 0; j < i-1; j++) {

temp = A[i][j];

A[i][j] = A[j][i];

A[j][i] = temp;

}

 

// Question 5

int count=1;

for (int i = 0; i < B_Size; i++)

// Go across row i

for (int j = 0; j < B_Size; j++) {

B[i][j] = count;

count++;

}

// Question 6

count=1;

for (int j = 0; j < B_Size; j++)

// Go down column j

for (int i = 0; i < B_Size; i++) {

B[i][j] = count;

count++;

}

 

// Question 7

boolean found = false; // true when

// a zero has been found

// Check the diagonal for zeros

int i = 0;

while (i < A_Size && !found) {

found = (A[i][i] == 0);

if (!found) i++;

}

if (found)

System.out.println ("There is a zero

in row " + i + " column " + i);

else

System.out.println ("There are no

zeros");

 

// Question 8

found = false; //true when a zero has

//been found

// Check the minor diagonal for zeros

i = 0;

while (i < A_Size && !found) {

found = (A[i][(A_Size - 1) - i] == 0);

if (!found) i++;

}

if (found)

System.out.println (" There is a zero

in row " + i + " column "

+ A_Size - 1 – I);

else

System.out.println ("There are no

zeros");