Passing two-dimensional arrays into methods


Write an application that ask the user to fill a 4x3 matrix array. Include a method that calculate the sum, and use it.

Output:










import java.util.Scanner;

public class twoDimentionalArrays
{
  
public static int sum(int[][] list)
{
  int sum=0;
  for (int i =0;i<list.length;i++)
  {
    for (int j=0;j<list[i].length;j++)
    {
      sum+= list[i][j];
    }
  }
  return sum;
}
public static void main (String[] args)
{
  Scanner scan = new Scanner (System.in);
  int[][] list = new int[4][3];
  System.out.println("Fill is this 4x3 matrix array :) " );
  System.out.println("By the way a matrix array is a two-dimensional one,");
  for (int i =0;i<list.length;i++)
  {
    for (int j=0;j<list[i].length;j++)
    {
      list[i][j= scan.nextInt();
    }
  }
  System.out.println("The sum of the values entered is " + sum(list));
}
}

No comments:

Post a Comment