Problem Description For this lab you will create a 2D Array

Problem Description: For this lab, you will create a 2D Array where you will be storing values of type integer, fill in the elements of that array by prompting the user for input then display the elements ofthe array. Then you will need to find the sum of each row of array and display it. Step 1: Getting Started: Create a new java file named \"Lab11java\". Import the following libraries java util Scanner; java util Arrays; Lab11 is the name of the class that contains the main function: public class Lab11 public static void main(String0 args) Step 2: Declaring variables: For this section of the lab, you will need to declare four local variables: one Scanner to read input from the user, one integer for the mumber of rows, one integer for the number of columns, and one string that will be used to sum of the elements of each row. Declare these variable immediately after the function definition: ll A scanner object for requesting input from the user An integer for the number of rows. ll An integer for the number of columns. llan integer for sum of each row\'s elements

Solution

import java.util.Scanner;
import java.util.Arrays;

class Lab11
{
   public static void main (String[] args)
   {
       Scanner scan = new Scanner(System.in); //scanner object
       int rows;
       int cols;
       int rowsum;
      
       System.out.println(\"Enter the number of rows in the array\");
       rows = scan.nextInt();
      
       System.out.println(\"Enter the number of columns in the array\");
       cols = scan.nextInt();
      
      
       int[][] intArray = new int[rows][cols];
      
       for(int i=0; i<rows; i++) //input elements of array
       {
       for(int j=0; j<cols; j++)
       {
           System.out.println(\"Enter the next number\");
           intArray[i][j] = scan.nextInt();
       }
       }
      
       for(int i=0; i<rows; i++)
       {
       for(int j=0; j<cols; j++)
       {
           System.out.print(intArray[i][j] + \" \"); //display elements of array
          
       }
       System.out.println();
       }
      
      
       for(int i=0; i<rows; i++)
       {
           rowsum = 0; //initialize sum of row =0
       for(int j=0; j<cols; j++)
       {
           rowsum = rowsum + intArray[i][j]; //compute sum of elemnts of a row
          
       }
       System.out.println(\"Sum of row \"+ i +\" is : \"+rowsum);
       }
   }
}

output:

 Problem Description: For this lab, you will create a 2D Array where you will be storing values of type integer, fill in the elements of that array by prompting
 Problem Description: For this lab, you will create a 2D Array where you will be storing values of type integer, fill in the elements of that array by prompting

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site