Could you please help me with this problem.i keep on getting on error when i run
ID: 3738550 • Letter: C
Question
Could you please help me with this problem.i keep on getting on error when i run it.
The error that i keep on getting when i run is :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at matrix.main(matrix.java:81)
import java.util.Random;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class MatrixMult implements Runnable {
//create matrixes
private static float[][]matrix_1;
private static float[][]matrix_2;
private static float[][]matrix_3;
private static int size;
private int start_time;
private int end_time;
private static int[] number;
public MatrixMult(int start_time, int end_time){
this.start_time = start_time;
this.end_time = end_time;
}
// multiplying the matrices
public void multiply_matrix(){
for (int r = start_time; r < end_time;r++) {
for (int k =0; k < size; k++) {
float sumMatrix = 0;
for (int j = 0; j < size; j++) {
sumMatrix = (sumMatrix + matrix_1[r][j] * matrix_2[j][k]);
}
matrix_3[r][k] = sumMatrix;
}
}
}
public void run(){
multiply_matrix();
}
// Setting the threeads
public static void checkThread(int mat_l){
number = new int[mat_l];
int count = (int)size/mat_l;
int rem = size % mat_l;
for(int r = 0; r < mat_l ; r++){
number[r] = count;
if(rem > 0) {
number[r] = number[r] + 1;
rem--;
}
}
}
//print matrix
public static void print_matrix(float[][] MatrixMult){
for(int r = 0; r < size; r++){
for(int k = 0; k < size; k++){
System.out.print(MatrixMult[r][k] + " ");
}
}System.out.println();
}
// Creates the Matrix method
public static void matrix_calc(){
matrix_1 = new float[size][size];
matrix_2 = new float[size][size];
matrix_3 = new float[size][size];
for (int r = 0; r < size; r++) {
for (int k = 0; k < size; k++) {
matrix_1[r][k] = 2.0f;
matrix_2[r][k] = 2.0f;
}
}
}
// Main method
public static void main(String[] args){
long startMatrix = System.currentTimeMillis();
size = Integer.parseInt(args[0]);
matrix_calc();
checkThread(Integer.parseInt(args[1]));
int temp = 0;
System.out.print("Main: ");
Thread[] arrayM = new Thread[Integer.parseInt(args[1])];
for(int r = 0; r < Integer.parseInt(args[1]); r++){
Runnable run = new MatrixMult(temp, number[r] + temp);
temp += number[r];
Thread my_matrix = new Thread(run);
arrayM[r] = my_matrix;
my_matrix.start();
}
for(int r = 0; r < Integer.parseInt(args[1]); r++){
try {
arrayM[r].join();
}catch(InterruptedException exceptional){
return;
}
}
long endMatrix = System.currentTimeMillis();
long time_matrix = endMatrix - startMatrix;
print_matrix(matrix_3);
System.out.println("Took " + time_matrix + " ms.");
}
}
Explanation / Answer
The problem can be solved by giving two command line arguments as the input to the program. I will give you sample run
java MatrixMult argument1 argument2
java MatrixMult 4 3
if you give
java MatrixMult 4
then definitely you will get ArrayIndexOutOfBoundsException error according to the given problem so give two command line arguments as the input