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

Allocate storage to store a 3 row by 3 column two dimensional array of integers.

ID: 3841654 • Letter: A

Question

Allocate storage to store a 3 row by 3 column two dimensional array of integers. Call the array A. Initialize the array with each element’s value as follows: A[0][0] = 1 A[0][1] = 2 A[0][2] = 3 A[1][0] = 4 A[1][1] = 5 A[1][2] = 6 A[2][0] = 7 A[2][1] = 8 A[2][2] = 9 Then, have the program use a loop to continually ask the user to input a pair of integers. The first integer is the row number (row) and the second integer is the column number (column) of an element in the array A. One of these three actions will be performed depending on the value of the row number and column number inputted: 1. If the row number is -1 and the column number is -1 then terminate the loop and the program. 2. Or, check to make sure the row number and column number are valid indices for the array A. A valid index for array A is 0, 1 or 2. If either the row number or the column number (or both) are invalid then do nothing and return to the beginning of the loop to ask for a new pair of numbers. 3. If the row number and the column number are valid indices then load the value of element A[row][column] from memory and print out the value. Then, return to the beginning of the loop to ask for a new pair of numbers. You must use subroutines in your program as much as possible, especially when the same code appears in your program in two or more places. Program Input and Output Program input: Type -1 for the row number and -1 for the column number to terminate the program. Otherwise type 0, 1, or 2 for the row number and 0, 1, or 2 for the column number. Enter a row number: Enter a column number: Program output: Only if the row number and column number are valid will the program print. For example, if the row number inputted is 1 and column number inputted is 2 then the program output for this input will be: The value for element A[1][2] is 6.

Explanation / Answer

Note : You didn't specify which language to write in, so I chose Java.

import java.util.Scanner;

class TestClass {
  
   public static void main(String args[]) {
      
       Scanner inputScanner = new Scanner(System.in);
       int[][] A = new int[3][3];
       initializeArray(A);
      
       while (true) {
           /*
           * Get a pair of numbers from user
           */
           System.out.print(" Enter First Number : ");
           int row = inputScanner.nextInt();
          
           System.out.print("Enter Second Number : ");
           int col = inputScanner.nextInt();

           if (row == -1 && col == -1) {
               return;
           }  
          
           if (isValid(row, col)) {
               System.out.println("The element is : " + A[row][col]);
           }  
       }
   }

   public static boolean isValid(int row, int col) {
       if (row<0 || row>=3 || col<0 || col>=3) {
           return false;
       }
       return true;
   }
  
   public static void initializeArray(int A[][]) {
       /*
       * Assigning A[0][0]=1, A[0][1]=2, .....
       */
       for (int i=0; i<3; i++) {
           for (int j=0; j<3; j++) {
               A[i][j] = i+1;
           }
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote