0

How to multiply a matrix already created with random numbers to itself ?

Hello everyone. I created a matrix with random integers and I have to multiply this matrix to itself and print it. And I have to do the multiplication in an another method. Here is my code : public class Deneme21 { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here int [][]a = new int [10][10]; gunesF(a); } /** * * @param sayi * @return */ public static boolean isPrime(int sayi){ boolean sonuc = true; for (int i = 2; i < sayi; i++){ if (sayi % i == 0){ sonuc = false; break; } } return sonuc; } public static void gunesF(int [][] a) { Random randomNum = new Random(); for (int x = 0; x < 10; x++){ for (int y = 0; y < 10; y++){ int s; s = randomNum.nextInt(100); a[x][y] = s; } } for (int x = 0; x < 10; x++){ for (int y = 0; y < 10; y++){ if (isPrime(a[x][y])){ System.out.print(a[x][y] + "* "); } else { System.out.print(a[x][y] + " "); } } System.out.println(" "); } } }

6th Nov 2021, 10:48 PM
Güneş Özkan
Güneş Özkan - avatar
5 Antworten
+ 4
What is the isPrime() method for though? are you only multiplying when a certain element's value was a prime number?
7th Nov 2021, 12:44 AM
Ipang
+ 3
Martin Taylor Does a[x][y] = a[x][y] * a[x][y] ; works for matrix multiplication? This will multiply the numbers with itself but I think Güneş Özkan want the matrix multiplication.
7th Nov 2021, 5:16 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 1
Ipang No man it is for checking the random integer if it is prime number or not, when it is prime, the number has got an asteriks over it.
7th Nov 2021, 12:48 AM
Güneş Özkan
Güneş Özkan - avatar
+ 1
Okay then, Martin's suggestion is good. I'd go his way for this case also 👌
7th Nov 2021, 1:02 AM
Ipang