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

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact