Hello, I'm doing this exercise, but I'm having a hard time. The following is the statement:
22) Write a program to generate an array (N x M) (
Hello, I'm doing this exercise, but I'm having a hard time. The following is the statement:
22) Write a program to generate an array (N x M) (
First, you are reading n
twice, but you should read n
, m
and p
independently.
Then the most problematic part is this:
for(int i=0; i<n; i++){
for(int j=0; j<(n+n); ){
q[i][j] = m[i][a];
a++;
j++;
q[i][j] = p[i][b];
b++;
j++;
}
}
Look at a++;
and b++;
, they will each go from 0
to 2n²
and do not think that's what you want, since when accessing m[i][a]
or p[i][b]
you will have a column index outside the valid range, and therefore the exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Ex22.main(Ex22.java:46)
There is probably a lack of a = 0;
and a b = 0;
somewhere.