Question

In: Computer Science

Write two methods. The first method is randFill2DArray. This method will fill a two-dimensional array with...

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.

Solutions

Expert Solution

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].


Related Solutions

Write a java method that creates a two dimensional char array after asking the user to...
Write a java method that creates a two dimensional char array after asking the user to input a String text (for example, "Sara" which is entered by the user)  and String key consisting of integers (for example, 2314) only, such that int rows=(int)Math.ceil(text.length()/key.length())+1; int columns= key.length(); The method fills the 2d array with letters a String entered by the use (column by column). The method then shifts the columns of the array based on key. For example, if the user enter...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we...
Create a “Main” method that contains two 2-Dimensional arrays of characters. The first array, which we will call our visible field, needs to be 5 by 5 and should initially hold the underscore character “_”.  This will be used in our game of minesweeper to represent the possible locations the player can check. The second array, which we will call our hidden field, should also be 5 by 5 but filled with the capital letter "S” which means safety. However, we...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to fill the 2-dimensional array with (random numbers, range 0 - 30). The array has rows (ROW) and columns (COL), where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to...
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5....
Write a Java program that will use a two-dimensional array and modularity to solve the following...
Write a Java program that will use a two-dimensional array and modularity to solve the following tasks: Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. Create a method to print the array. Create a method to find the largest element in the array Create a method to find the smallest element in the array Create a method to find...
Q1: Write a method called testArray that accepts a one-dimensional array of any size and prints...
Q1: Write a method called testArray that accepts a one-dimensional array of any size and prints the following summary information: a) all array elements at an even index b) every fifth element c) all elements but the first and last Write a program that demonstrates this method by using an array of 20 random integers from 100 to 200. Q2: Write a method called displayGrid that accepts a two-dimensional array of integer values from 0 to 99 and prints this...
Write a Java program that will use a two-dimensional array to solve the following tasks: 1....
Write a Java program that will use a two-dimensional array to solve the following tasks: 1. Create a method to generate a 2-dimensional array (random numbers, range 0 - 500). The array has ROW rows and COL columns, where ROW and COL are class constants. 2. Create a method to print the array. 3. Create a method to find the largest element in the array 4. Create a method to find the smallest element in the array 5. Create a...
Write a C function that finds and displays the maximum value ina two-dimensional array of...
Write a C function that finds and displays the maximum value in a two-dimensional array of integers. The array should be declared as a 10-row-by-20-column array of integers in main (), and the starting the address of the array should be passed to the function. Modify the function so that it also displays the rows and columns number of the element with the maximum value
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and...
Write a C++ function that takes a two dimensional dynamic array (matrix) and its sizes and returns true if the matrix is an upper matrix and returns false otherwise. Remark: A matrix M is an upper matrix if it is a square matrix (row size = column size) and every element M[i][j] = 0 for all i > j. That is all the elements of the matrix below the main diagonal are zero.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT