View Answers

February 19, 2010 at 12:06 PM

Hi Friend,

Try the following code:

import java.io.*;

import java.util.*;

public class matrix {

public static void main(String[] args) throws IOException {

int rows, cols;

int sum=0;

int[][] matrix;

Scanner input = new Scanner(System.in);

System.out.print("Enter number of rows: ");

rows = input.nextInt();

System.out.print("Enter number of columns: ");

cols = input.nextInt();

matrix = new int[rows][cols];

System.out.println("Enter elements for Matrix");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

matrix[i][j] = input.nextInt();

}

}

System.out.println("Matrix is: ");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

sum+=matrix[i][j];

System.out.print(matrix[i][j]+" ");

}

System.out.println();

}

System.out.println("Sum of all elements is: "+sum);

}

}

Thanks

Try the following code:

import java.io.*;

import java.util.*;

public class matrix {

public static void main(String[] args) throws IOException {

int rows, cols;

int sum=0;

int[][] matrix;

Scanner input = new Scanner(System.in);

System.out.print("Enter number of rows: ");

rows = input.nextInt();

System.out.print("Enter number of columns: ");

cols = input.nextInt();

matrix = new int[rows][cols];

System.out.println("Enter elements for Matrix");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

matrix[i][j] = input.nextInt();

}

}

System.out.println("Matrix is: ");

for (int i = 0; i < rows; i++) {

for (int j = 0; j < cols; j++) {

sum+=matrix[i][j];

System.out.print(matrix[i][j]+" ");

}

System.out.println();

}

System.out.println("Sum of all elements is: "+sum);

}

}

Thanks

Ads

Related Tutorials/Questions & Answers:

two dimensional array how tow dimensional array works.How those loopes get incremented .and how every time the value of k changes

two dimensional write a program to create a 3*3 array and print the sum of all the numbers stored in it. Hi Friend, Try the following code: import java.io.*; import java.util.*; public class matrix

Advertisements

Two Dimensional Array Program Using Nested for loop...; program . In this session we will teach how to use of a two dimensional array... are going to make two dimensional array having three row and three columns.

Two- Dimensional Array I am new in java programming. I am creating a two-dimensional array. This is my code ** class BinaryNumbers { public static void main(String[] args) { //create a two-dimensional array int ROWS = 21

how do i begin a two dimensional array? I'm new to java programming and need to create a two dimensional array that enters exactly what is entered in the first dimension and then the first non-white space character of what

Two dimensional array in java. In this section you will learn about two-dimensional array in java with an example. As we know that array is a collection.... int arr[][]; Here arr is a two-dimensional array of type int. To create an array

Need coding help - two dimensional matrix and it returns back a boolean. I need to write a code method that accepts a two dimensional integer arrays as the argument and returns true if the array or matrix is left or right

need help with two dimensional array that takes input from user and bubble sorts and selections sorts I can get both of the sorts figured out but i cannot figure out how i can declare a two dimensional array without knowing how

A single dimensional array is called What is a single dimensional array called? A single dimensional array is called LIST

one dimensional array program Design and implement a java program that will read a file containing numbers and compute the following statistics: the rannge( low, high), the average and the median(middle number). Notes

comparing arraylist of an multi dimensional arraylist can anyone help me in solving the following issue: actually i have an arraylist called dany... araylist.if we find the content of the two arraylist similar then it shuld be stored

one dimensional array using java design and implement a java program that will read a file containing numbers and compute the following statistics: the range(low,high) the average and the median

Display tow dimensional array using one for loop in java Display tow dimensional array using one for loop in java

Display tow dimensional array by matrix form using one for loop in java Display tow dimensional array by matrix form using one for loop in java

multiplication of two matrices. In java this is a simple program to multiply two matrices, we have to take two-dimensional array and the result should be saved in third two-dimensional array. Here we are going to develop a Java code for matrices

Two - dimension arraylist how to create two - dimension arraylist in jsp-servlet

intersection of two files hi!could you please provide me with the java program for displaying intersection of two files

ModuleNotFoundError: No module named 'two' Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'two' How to remove the ModuleNotFoundError: No module named 'two' error

ModuleNotFoundError: No module named 'two' Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'two' How to remove the ModuleNotFoundError: No module named 'two' error

Join the two tables in sql How to join the two tables in SQL ..and return the value from common column in SQL Database

compare two strings in java How to compare two strings in java...) { System.out.println("The two strings are the same."); } } } Output: The two strings are the same. Description:-Here is an example of comparing two

two linked combobox give jsp example of two combo box when i select state in one combobox in second combo box cities will display according to state which i select

parralel connection in two resisitence R1=r1+r2(series) R2=r1.r2/r1+r2(parralel

plz try to clear my doubt for shuffling multi-dimensional array hi, if v r using Arrays.asList() means, it may shuffle the row wise list only... v want to shuffle the ful entire multi-simensional array means wat v want to do

Creation Time Comparison of Multi Dimensional Array In this section, we will compare the creation time between the different size/dimension of array. As you know that multidimensional array is the array of arrays. Practically

. Different types of array used in Java are One-dimensional, Two-dimensional and multi-dimensional. One-dimensional arrays: int[] i; int[] i = new int[5]; int[] = {50,60,70,80,90}; Two-dimensional arrays: Two-dimensional arrays

Two Pagination in one page hai friends any one help me. how do u make two pagination script in same page i'm used some ajax coding one pagination script is working but another pagination is not working please help me urgent