Results 1 to 2 of 2
Thread: HELP please
- 10-10-2012, 07:40 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 1
- Rep Power
- 0
HELP please
im trying to do a matrix multiplication and adding here.. yet i cant.. because this error occurs : "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at one.main(one.java:104) "
HELP ME please?
here's my code:
import java.util.Scanner;
public class one
{
public static void main(String [] args)
{
int m, n, c, d, m2,n2, mADD, nADD, x, k, prod= 0,x3,x4,x5,Ascalar, Bscalar ;
String x2;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of rows and columns of matrix A");
m = in.nextInt();
n = in.nextInt();
int first[][] = new int[m][n];
System.out.println("Enter the elements of first matrix");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
first[c][d] = in.nextInt();
System.out.println("Enter the number of rows and columns of matrix B");
m2 = in.nextInt();
n2 = in.nextInt();
int second[][] = new int[m2][n2];
int sum[][] = new int[m][n];
System.out.println("Enter the elements of second matrix");
for ( c = 0 ; c < m2 ; c++ )
for ( d = 0 ; d < n2 ; d++ )
second[c][d] = in.nextInt();
System.out.println(" _________________");
System.out.println("| MENU |");
System.out.println("|-----------------|");
System.out.println("|1.Addition |");
System.out.println("|2.Multiplication |");
System.out.println("|3.Scalar |");
System.out.println("|4.Transverse |");
System.out.println("|5.Exit |");
System.out.println("|_________________|");
System.out.println("");
System.out.println("Enter no. to be performed:");
x = in.nextInt();
if (x == 1)
{
if ( n != m2 )
{
System.out.println("Matrices with entered orders can't be multiplied with each other.");
}
else
{
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d]+ second[c][d];
System.out.println("Sum of entered matrices:");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(sum[c][d]+"\t");
System.out.println();
}
}
}
if ( x==2)
{
if ( n != m2 )
{
System.out.println("Matrices with entered orders can't be multiplied with each other.");
}
else
{
int multiply[][] = new int[m][n2];
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n2 ; d++ )
{
for ( k = 0 ; k < m2 ; k++ )
{
prod = prod + first[c][k]*second[k][d];
}
multiply[c][d] = prod;
prod = 0;
}
}
System.out.println("Product of entered matrices:");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
System.out.print(multiply[c][d]+"\t");
System.out.println();
}
}
}
if (x==3)
{
System.out.println(" _________________");
System.out.println("| MENU |");
System.out.println("|-----------------|");
System.out.println("|1.Matrix A |");
System.out.println("|2.Matrix B |");
System.out.println("|3.Both |");
System.out.println("|_________________|");
System.out.println("");
System.out.println("Enter what matrix do you want to multiply?");
x4 = in.nextInt();
if (x4==1)
{
System.out.println("You selected Matrix A, where do you want to multiply it?");
x5 = in.nextInt();
}
}
if (x==4)
{
int transposeA[][] = new int[n][m];
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
transposeA[d][c] = first[c][d];
}
System.out.println("Transpose of entered matrix A:");
for ( c = 0 ; c < n ; c++ )
{
for ( d = 0 ; d < m ; d++ )
System.out.print(transposeA[c][d]+"\t");
System.out.print("\n");
}
int transposeB[][] = new int[n2][m2];
for ( c = 0 ; c < m2 ; c++ )
{
for ( d = 0 ; d < n2 ; d++ )
transposeB[d][c] = second[c][d];
}
System.out.println("Transpose of entered matrix B:");
for ( c = 0 ; c < n2 ; c++ )
{
for ( d = 0 ; d < m2 ; d++ )
System.out.print(transposeB[c][d]+"\t");
System.out.print("\n");
}
}
}
}
- 10-11-2012, 04:09 AM #2
Member
- Join Date
- Oct 2012
- Location
- Tempe, Arizona
- Posts
- 77
- Blog Entries
- 12
- Rep Power
- 0


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks