137k views
4 votes
Write a Java program in jGRASP that creates a 2D integer array of 5 rows and 20 columns, fills it with increasing integer values ranging from 0 to 59 and then prints the array

1 Answer

6 votes

Answer:

As per the question we need to make 5 rowa and 2o columns, in this we have total element 100 so range should be 0 to 99, but we are suggested to keep range 0 to 59, so i have kept the elementns in the range, but if it was a typo in the question for range you do not need to reset the k with zero.

Step-by-step explanation:

//create a new class TestArray

public class TestArray {

//define main method

public static void main(String[] args) {

//declare a 2D array of given size i.e. 5 rows and 20 columns

int arr[][] = new int[5][20];

//declare an integer variable k and initialize it with zero, this will used to initialize the array from 0 to 99

int k = 0;

//for loop for rows

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

//for loop for columns

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

//initialize arr with the current value of k

arr[i][j] = k;

//once k is 59 set it zero again as we are not going beyond 59

if(k == 59)

k = 0;

//increment value of k with 1

k++;

}

}

//print the element from the array one by one

//for loop for rows

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

//for loop for columns

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

//print the current element of array and an space with it

System.out.print(arr[i][j]+" ");

}

// move the cursor to new line

System.out.println();

}

}

}

User Ohdroid
by
8.4k points