Question

You will create a general MxN matrix where M = number of rows and N = number columns that are specified by the user. Write a

Why is my multiplication wrong when i do a matrix of 3 x 5 and 2 x 2?

code below

enter how many rows: 2 enter how many columns:2 Matrix 1 76 54 22 80 Matrix 2 92 47 47 60 Matrix 1 + Matrix 2 : 168 101 69 14

enter how many rows:3 enter how many columns:5 Matrix 1 93 50 42 36 92 23 36 28 53 69 38 90 41 60 Matrix 2 82 17 76 19 18 64

import java.util.*;

public class matrix {

public static void main(String[] args) {

int m, n, i, j;

Random rand = new Random();

Scanner scan = new Scanner(System.in);

System.out.print("enter how many rows:");

m = scan.nextInt();

System.out.print("enter how many columns:");

n=scan.nextInt();

int matrix_1[][] = new int[m][n]; //Initialize matrixes

int maritx_2[][] = new int[m][n];

int matrix_add[][] = new int[m][n];

int matrix_mul[][] = new int[m][n];

for (i = 0; i < m; i++) { //matrix 1 w/ random values 0-99 and print matrix 1

for (j = 0; j < n; j++) {

matrix_1[i][j] = rand.nextInt(99);

}

}

System.out.println("Matrix 1 ");

for (i = 0; i < m; i++) {

for (j = 0; j < n; j++) {

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

}

System.out.println();

}

for (i = 0 ; i < m ; i++) { //matrix 2 w/ random values 0-99 and print matrix 2

for (j = 0 ; j < n ; j++) {

maritx_2[i][j] =rand.nextInt(99);

}

}

System.out.println("Matrix 2 ");

for (i = 0; i < m; i++) {

for (j = 0; j < n; j++) {

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

}

System.out.println();

}

System.out.println("Matrix 1 + Matrix 2 :\n");

for (i = 0; i < m; i++) { //Adding of matrix 1 & 2

for (j = 0; j < n; j++) {

matrix_add[i][j] = matrix_1[i][j] + maritx_2[i][j];

}

}

for (i = 0; i < m; i++) {

for (j = 0; j < n; j++) {

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

}

System.out.println();

}

System.out.println("Matrix 1 x Matrix 2 : \n");

for ( i = 0; i < m; i++) {

for ( j = 0; j < n; j++) {

for (int k = 0; k < m; k++) {

matrix_mul[i][j] = matrix_1[i][k] * maritx_2[k][j];

}

}

}

//print the multiplication matrix result

for (i = 0; i < m; i++) {

for (j = 0; j < n; j++) {

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

}

System.out.println();

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

matrix noi of no. of mulh plication is possible only when columns of ist matrin ů equal to rows of 2nd matrin. If order of is

Add a comment
Know the answer?
Add Answer to:
Why is my multiplication wrong when i do a matrix of 3 x 5 and 2...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner;...

    draw a flew chart for this code // written by Alfuzan Mohammed package matreix; import java.util.Scanner; public class Matrix {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);    System.out.print("Enter number of rows: first matrix ");    int rows = scanner.nextInt();    System.out.print("Enter number of columns first matrix: ");    int columns = scanner.nextInt();    System.out.print("Enter number of rows: seconed matrix ");    int rowss = scanner.nextInt();    System.out.print("Enter number of columns seconed matrix:...

  • Can you help me with this code in Java??? import java.util.Scanner; public class Main { public...

    Can you help me with this code in Java??? import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int rows = 3; int columns = 4; int[][] arr = new int[rows][columns]; for(int i = 0; i < rows; ++i) { for(int j = 0; j < columns; ++j) { System.out.println("Enter a value: "); arr[i][j] = scan.nextInt(); } } System.out.println("The entered matrix:"); for(int i = 0; i < rows; ++i) { for(int j...

  • Look for some finshing touches java help with this program. I just two more things added...

    Look for some finshing touches java help with this program. I just two more things added to this code. A loop at the end that will ask the user if they want to quit if they do want to quit the program stops if they don't it loads a new number sequence. import java.util.Random; import java.util.ArrayList; import java.util.Scanner; import java.util.Arrays; import java.util.List; import java.util.Collections; public class main { public static void main(String[] args) { List < Sequence > list =...

  • cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /**...

    cant understand why my toString method wont work... public class Matrix { private int[][] matrix; /** * default constructor -- * Creates an matrix that has 2 rows and 2 columns */ public Matrix(int [][] m) { boolean valid = true; for(int r = 1; r < m.length && valid; r++) { if(m[r].length != m[0].length) valid = false; } if(valid) matrix = m; else matrix = null; } public String toString() { String output = "[ "; for (int i...

  • My Question is: I have to modify this program, even a small modification is fine. Can...

    My Question is: I have to modify this program, even a small modification is fine. Can anyone give any suggestion and solution? Thanks in Advanced. import java.util.*; class arrayQueue { protected int Queue[]; protected int front, rear, size, len; public arrayQueue(int n) { size = n; len = 0; Queue = new int[size]; front = -1; rear = -1; } public boolean isEmpty() { return front == -1; } public boolean isFull() { return front == 0 && rear ==size...

  • Hello everyone. I have a bankers algorithm written in java that gets inputs from a .txt...

    Hello everyone. I have a bankers algorithm written in java that gets inputs from a .txt file. The file has 7 processes and 5 resources however, when the program runs, it doesn't sum the resource columns correctly. The program runs correctly for smaller matricies (4 processes, 3 resources), not sure what the issue is and have been looking over the code for awhile so maybe another set of eyes would help...thanks BankersAlgorithm.java /** * This program implements Bankers algorithm which...

  • How do I modify this to let the user input the size of the matrix? The...

    How do I modify this to let the user input the size of the matrix? The lowest size is 4 and the largest size is 16. import java.util.*; public class Test1 {      public static void main(String[] args)      { int[][] matrix = new int[4][4];    // create 4 by 4 matrix (need user input???)           // generate 1's and 0's for each each rows and columns      // and track largest row index with the most ones         int largestRI...

  • Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections;...

    Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections; class Grid { private boolean bombGrid[][]; private int countGrid[][]; private int numRows; private int numColumns; private int numBombs; public Grid() { this(10, 10, 25); }    public Grid(int rows, int columns) { this(rows, columns, 25); }    public Grid(int rows, int columns, int numBombs) { this.numRows = rows; this.numColumns = columns; this.numBombs = numBombs; createBombGrid(); createCountGrid(); }    public int getNumRows() { return numRows;...

  • The following code is a Java code for insertion sort. I would like this code to...

    The following code is a Java code for insertion sort. I would like this code to be modified by not allowing more than 10 numbers of integer elements (if the user enters 11 or a higher number, an error should appear) and also finding the range of the elements after sorting. /* * Java Program to Implement Insertion Sort */ import java.util.Scanner; /* Class InsertionSort */ public class InsertionSortTwo { /* Insertion Sort function */ public static void sort( int...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT