Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Class exercise 7 5/9 Refer to TwoDArray in files (InClassEx, CH7) // Fill a 2

ID: 3847621 • Letter: I

Question

In Class exercise 7 5/9 Refer to TwoDArray in files (InClassEx, CH7)

// Fill a 2D (5x10) array with random values(0-50)

// Determine the max. integer value in this array

// First output the array (5 rows, 10 columns)

// Then output the max value found

// and the last place (row, column) this max value

// was found

// Remember to output your name to console

   public class TwoDArrayMax {

//Up load one file to canvas:  TwoDArrayMax.java

here is example:

// In Class Example: 2 D Arrays
import java.util.Arrays;

// In class example of 2 dimensional array
public class TwoDArray {

   public static void main(String[] args) {
       // Initialize 2d array
       int [][] array= new int[4][5];
       array=new int[][] { {2,3,4,7,9}, //row 1
           {0,7,7,9,3}, //row 2
           {8,9,3,3,1}, //row 3
           {1,1,1,1,1} }; //row 4
System.out.println("array address="+array);  
//Each array[i] points to a row address which is
// the location of the four "1" Dimension arrays
// all four of which make up the 2D array
System.out.println("array[]row address= "+array[0]+" " +array[1]+" "+array[2]+" "+array[3]);  
// Print out array, the rows' values
   //
       for (int i=0;i<4 ;i++){
           System.out.println(Arrays.toString(array[i]));
}
   // Print out a single element
       System.out.println("single element, row 3, column 2 = "+array[2][1]);
   //
   // Square every elment of the array
   System.out.println("array[i][j] elements squared");  
       for (int i=0;i<4 ;i++){
           for (int j=0;j<5;j++) {
       array[i][j]= array[i][j]*array[i][j];
           } //end for [i]
           System.out.println(Arrays.toString(array[i]));
       } //end for[j]
   } //End Main
   } // End class

Explanation / Answer

Java code:

TwoDArrayMax.java

package twodarraymax;
import java.util.Random;
import java.util.Arrays;
public class TwoDArrayMax {
public static void main(String[] args)
{
Random rand = new Random();
int [][] array= new int[5][10];
for(int i = 0; i<5; i++)
{
for(int j =0; j<10;j++)
{
array[i][j] = rand.nextInt(51);
}
}
int maxx = Integer.MIN_VALUE ;
int tmp_r = 0; int tmp_c = 0;
for(int i = 0; i<5; i++)
{
for(int j =0; j<10;j++)
{
if(array[i][j] >= maxx)
{
maxx = array[i][j];
tmp_r = i; tmp_c = j;
}
System.out.print(array[i][j] + " ");
}
System.out.println();
  
}
System.out.println("Maximum value is " + maxx);
System.out.println("Maximum value is at (x,y) = " + "(" + tmp_r + "," + tmp_c + ")");
  
}
  
}

Sample Output:

16 22 25 21 27 39 32 4 18 36
7 28 27 13 43 9 30 24 4 2
7 48 18 1 44 41 44 4 29 25
49 47 3 7 27 46 49 33 30 47
42 42 40 11 25 37 15 18 41 17
Maximum value is 49
Maximum value is at (x,y) = (3,6)