In: Computer Science
Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with random integers between a given min and max value. It must accept for parameters, min and max values for the creation of random integers, and rows and columns for the number of rows and columns of the array. The method should return an array of rows by columns size, filled with random integers between min and max values. The second method is called printRA, and this method will accept a 2d array of integers and display this two-dimensional array to the screen (showing the rows and columns of the arrray). Write test code within your main method that will demonstrate the use of these methods.
1)
Dear Student I am trying to populate a 2D array with random numbers before I add the rows and columns.
public static void main(String[] args) { //create the grid final int row = 9; final int col = 9; int [][] grid = new int [row][col]; //fill the grid for (int i=0; i<grid.length; i++) grid[i][grid[i].length] = (int)(Math.random()*10); //display output for(int i=0;i<grid.length; i++) { for(int j=0; j<grid[i].length; j++) System.out.print(grid[i][j]+""); System.out.println(); } int sum = 0; for (int i = 0; i < grid.length; i++) { System.out.println("This row sums up to: " + sum); for (int j = 0; j < grid[i].length; j++) { sum += grid[j][i]; } System.out.println("This column sums up to: " + sum); } }
2)
An array of arrays is known as 2nd array. the 2 dimensional (2nd) array in programming is likewise referred to as matrix.a way to initialize a two dimensional array, we will discuss that part later. This programdemonstrates a way to save the elements entered through consumer in a 2nd array and the way to displaythe elements of a two dimensional array.
#include<stdio.h> int main(){ /* 2D array declaration*/ int disp[2][3]; /*Counter variables for the loop*/ int i, j; for(i=0; i<2; i++) { for(j=0;j<3;j++) { printf("Enter value for disp[%d][%d]:", i, j); scanf("%d", &disp[i][j]); } } //Displaying array elements printf("Two Dimensional array elements:\n"); for(i=0; i<2; i++) { for(j=0;j<3;j++) { printf("%d ", disp[i][j]); if(j==2){ printf("\n"); } } } return 0; }
Output:
Two Dimensional array elements: 1 2 3 4 5 6
Storage of Data:
#include<stdio.h> int main(){ /* 2D array declaration*/ int abc[5][4]; /*Counter variables for the loop*/ int i, j; for(i=0; i<5; i++) { for(j=0;j<4;j++) { printf("Enter value for abc[%d][%d]:", i, j); scanf("%d", &abc[i][j]); } } return 0; }
#include <stdio.h> int main() { int abc[5][4] ={ {0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15}, {16,17,18,19} }; for (int i=0; i<=4; i++) { /* The correct way of displaying an address would be * printf("%p ",abc[i]); but for the demonstration * purpose I am displaying the address in int so that * you can relate the output with the diagram above that * shows how many bytes an int element uses and how they * are stored in contiguous memory locations. * */ printf("%d ",abc[i]); } return 0; }
The addresses proven inside the output belongs to the first element of each row abc[0][0], abc[1][0], abc[2][0], abc[3][0] and abc[4][0].