Results 1 to 7 of 7
Thread: java Handling matrices
 02282014, 12:42 PM #1Member
 Join Date
 Feb 2014
 Posts
 16
 Rep Power
 0
java Handling matrices
Hello,
Can any one help me why my code is not printing the matrix and sum?
thank you!
Assignment
The Javaprogram Matrix below first asks the user for the number of rows and columns in a matrix. After this the program asks for the values of the elements. Finally, program prints the elements of the matrix and their sum on screen. Your task is to create the missing methods. Check the example print to see how to modify the print. When printing the matrix, values on the same row are separated using tabulator.
Program to complete:
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
int rows, columns;
Scanner reader = new Scanner(System.in);
System.out.print("Type in the number of rows: ");
rows = reader.nextInt();
System.out.print("Type in the number of columns: ");
columns = reader.nextInt();
int matrix [][];
matrix = askInfo(rows, columns);
printMatrix(matrix);
countSum(matrix);
}
// Write the missing methods here
// Methods are written in the text box below.
}
Example output
Type in the number of rows: 3
Type in the number of columns: 4
Type in the element 1 of the row 1: 1
Type in the element 2 of the row 1: 2
Type in the element 3 of the row 1: 3
Type in the element 4 of the row 1: 4
Type in the element 1 of the row 2: 5
Type in the element 2 of the row 2: 6
Type in the element 3 of the row 2: 7
Type in the element 4 of the row 2: 8
Type in the element 1 of the row 3: 9
Type in the element 2 of the row 3: 10
Type in the element 3 of the row 3: 11
Type in the element 4 of the row 3: 12
Matrix:
1 2 3 4
5 6 7 8
9 10 11 12
Sum of the elements of the matrix: 78
my code
Java Code:import java.util.Scanner; public class apples { public static void main(String[] args) { int rows, columns; Scanner reader = new Scanner(System.in); System.out.print("Type in the number of rows: "); rows = reader.nextInt(); System.out.print("Type in the number of columns: "); columns = reader.nextInt(); int matrix [][]; matrix = askInfo(rows, columns); printMatrix(matrix); countSum(matrix); } // method private static int[][] askInfo(int rows, int columns) { Scanner reader = new Scanner(System.in); int[][] matrix = new int[rows][columns]; int sum=0; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { System.out.print("Type in the element "+(j+1)+" of the row "+(i+1)+": "); matrix[i][j] = reader.nextInt(); sum += matrix[i][j]; } }return matrix; } private static void printMatrix(int[][] matrix) { int rows=0, columns=0; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { System.out.print(matrix[i][j] + " "); } System.out.print("\n"); } } private static void countSum(int[][] matrix) { int sum=0; System.out.println("Sum of the elements of the matrix: "+sum); } }
 02282014, 12:45 PM #2
Re: java Handling matrices
On line 51 you set int rows=0, columns=0 so there are no rows and columns to loop over. I would suggest you take the length of the matrix array to loop over instead of a hardcoded row or column.
"It's not fixed until you stop calling the problem weird and you understand what was wrong."  gimbal2™ © 2013
 03012014, 02:39 AM #3Member
 Join Date
 Feb 2014
 Posts
 16
 Rep Power
 0
Re: java Handling matrices
Hello,
Thank you for the suggestions. As per the assignment my task is only to create the methods.
I am not supposed to touch the main method, that is what it makes hard for me. When I modify
int rows=0; int columns=0; to int rows, columns; it says I have to initialize. So generally I am trying
to create three methods which fits the main. Is it possible to use length the matrix array inside a private
method with out modifying the given code in the assignment? thank you for your help!
 03012014, 02:56 AM #4
Re: java Handling matrices
Use the array's .length property to control looping. For example for the num array:
Java Code:for(int i = 0 ; i < num.length ; i++)
If you don't understand my response, don't ignore it, ask a question.
 03012014, 03:17 AM #5Member
 Join Date
 Feb 2014
 Posts
 16
 Rep Power
 0
Re: java Handling matrices
Hi Norm,
Thanks, when I use the array's length, like for example
for (int i = 0; i < rows.length; i++) {
And I can't modify any thing on the main, my task is only to create the
missing 3 methods.
Thanks
 03012014, 03:43 AM #6
Re: java Handling matrices
Is rows an array? .length is for arrays.
If you don't understand my response, don't ignore it, ask a question.
 03012014, 08:08 AM #7Senior Member
 Join Date
 Jan 2013
 Posts
 168
 Rep Power
 4
Similar Threads

Help with arrays and matrices!
By chwex in forum New To JavaReplies: 2Last Post: 02152013, 01:34 AM 
Help with arrays and matrices!
By chwex in forum New To JavaReplies: 3Last Post: 02132013, 12:46 PM 
Arrays and Matrices
By Shaeman111 in forum New To JavaReplies: 2Last Post: 11102012, 08:24 AM 
how to add columns from Matrices(2D array)
By John_shok in forum New To JavaReplies: 7Last Post: 10042010, 04:33 PM 
How to multiply two matrices
By Java Tip in forum java.langReplies: 0Last Post: 04142008, 08:50 PM
Bookmarks