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...
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 and String key consisting of integers only, such that int rows=(int)Math.ceil(text.length()/key.length()); // or int rows=(int)Math.ceil(text.length()/key.length()); ? int columns= key.length(); int remainder= text.length() % key.length(); // such that the last row avoids taking an index beyond the string text by making columns - remainder The method fills the 2d array with letters a String entered by the use (row by row)....
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...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest...
IN JAVA Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. Your program should output all the values in the array and then output the average high and the average low. im trying to...
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for...
Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. Prompt the user for 12 months of highest and lowest.   Write two methods : one to calculate and return the average high and one to calculate and return the average low of the year. These methods MUST be your original code.   Your program should output all the values in the array and then output the average high and the...
Write a program that uses a two dimensional array to store the highest and lowest temperatures...
Write a program that uses a two dimensional array to store the highest and lowest temperatures for each month of the calendar year. The temperatures will be entered at the keyboard. This program must output the average high, average low, and highest and lowest temperatures of the year. The results will be printed on the console. The program must include the following methods: A method named inputTempForMonth whose purpose is to input a high and a low temperature for a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT