Interleaved matrix

Asked

Viewed 239 times

0

Hello, I’m doing this exercise, but I’m having a hard time. Follow the statement:

22) Write a program to generate one matrix (N x M) (<=20) and another (N x P) (<=20). Generate a third matrix (N x (M+P)) (<=20 x <=40), where for each interleave row their respective columns and present the three matrices.

Here’s the code I already have:

import java.util.Scanner;

public class Ex22 {
    public static void main(String args[]) {
        Scanner leia = new Scanner(System.in);

        int n, a = 0, b = 0;

        do {
            System.out.printf("Entre com o tamanho da matriz: ");
            n = leia.nextInt();
        } while (n > 20);

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

        System.out.printf("\nMatriz M:\n");
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                m[i][j] = (int)(Math.random() * 9+1);
                System.out.printf("%d ", m[i][j]);
            }
            System.out.printf("\n");
        }

        do {
            System.out.printf("\nEntre com o tamanho da matriz: ");
            n = leia.nextInt();
        } while (n > 20);

        int [][] p = new int [n][n];

        System.out.printf("\nMatriz P:\n");
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                p[i][j] = (int)(Math.random() * 9+1);
                System.out.printf("%d ", p[i][j]);
            }
            System.out.printf("\n");
        }

        int [][] q = new int [n][n+n];

        System.out.printf("\nMatriz Q:\n");
        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++;                
            }
        }

        for(int i=0; i<n; i++){
            for(int j=0; j<(n+n); j++){
                System.out.printf("%d ", q[i][j]);
            }
            System.out.printf("\n");
        }

    }
}

Does anyone know why it’s popping?

  • Anybody know why it’s popping? This is the error that returns: Matrix Q: Exception in thread "main" java.lang.Arrayindexoutofboundsexception: 2 At Ex22.main(Ex22.java:53) But in C it compiles without errors.

  • You don’t reset a and b no for matrix q, I did not analyze the rest of the code, but you are considering that n = m = p and that is not what the question asks too...

  • Michael Martins, compiling it compiles normally, but between compiling and executing there is a huge difference.

1 answer

1

First of all, you’re reading n twice, but you should read n, m and p independently.

Then the most problematic section 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++;                
        }
    }

Note the a++; and the b++;, will make each one go from 0 until 2n² and I don’t think that’s what you want, once you access m[i][a] or p[i][b] you will have a column input outside the valid range, and therefore the exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
        at Ex22.main(Ex22.java:46)

Probably missing a a = 0; and a b = 0; somewhere.

  • 1

    Exactamante. a=b=0 Thank you all ^^

Browser other questions tagged

You are not signed in. Login or sign up in order to post.