-1

So, I'm trying to create a program that captures 9 numbers in a 2D array, being of size 3 x 3. After capturing all the numbers, they are re-arranged, reversed, and displayed.

So if a user entered:

1 2 3 
4 5 6
7 8 9

It should be reversed as this:

9 8 7 
6 5 4
3 2 1

So far I have this:

public class Reversenumber {


    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);
        int[][] array1 = new int[3][3];
        int val1 = 0;
        int val2 = 0;
        int[][] array2 = new int[3][3];
        int val3 = 0;
        int val4 = 0;

        for (val1 = 0;val1<3; val1++){
            for (val2 = 0;val2<3; val2++){
            System.out.println("Please type number for column "+(val1+1)+" and row "+(val2+1));
            array1[val1][val2] = kb.nextInt();
            }
        }


        System.out.println("\nThe inverse of this array is:");
        for(val1 = 0;val1<3; val1+=1){
           for (val2 = 0;val2<3; val2+=1){
               System.out.print(array1[val1][val2]+" ");
           }
        System.out.println("");
        }
    }
}

The problem I'm having is outputing the inverse of the array. The first for loop captures the information perfectly, but the second set of for loop displays that same information in a 3x3 array, but not inverted. Help anyone?

David H
  • 1
  • 3
  • 1
    Could you be more specific to exactly what you mean by "invert" the array? Do you mean finding the inverse of a 3x3 matrix? Or is it something more simple than that? – Arc676 Dec 10 '15 at 01:28
  • Can you show an example on what the input and input is like ? – sean Dec 10 '15 at 01:29
  • I should have probably used the word reversed instead of invert Instead of going 1 2 3 4 5 6 7 8 9 in a 3 x 3 it should revers going 9 8 7 6 5 4 3 2 1 in a 3 x 3 – David H Dec 10 '15 at 01:42

2 Answers2

1

I hope you don't mind, I changed the variables around completely since they are confusing. also changed the way you input stuff.

    Scanner kb = new Scanner(System.in);
    int[][] array1 = new int[3][3];
    int[][] array2 = new int[3][3];

    for (int y = 0; y < array1.length; y++) {
        System.out.println("input 3 numbers for row " + (y + 1));
        for (int x = 0; x < array1[y].length; x++) {
            array1[y][x] = kb.nextInt();
        }
    }


    System.out.println("\nThe inverse of this array is:");
    for (int y = array1.length; --y >= 0;) {
        for (int x = array1[y].length; --x >= 0;) {
            System.out.printf("%3d", array1[y][x]);
        }
        System.out.println();
    }

output

input 3 numbers for row 1
1 2 3
input 3 numbers for row 2
4 5 6
input 3 numbers for row 3
7 8 9

The inverse of this array is:
  9  8  7
  6  5  4
  3  2  1

So instead of inputting each separately you put 3 on the same line and put a space between them.

WalterM
  • 2,686
  • 1
  • 19
  • 26
-1

Assuming you mean invert like so:

1 2 3
4 5 6
7 8 9

to

1 4 7
2 5 8
3 6 9

It's as simple as keeping the loop the same, but swapping the coordinates between the arrays:

public static void main(String[] args) {

    Scanner kb = new Scanner(System.in);
    int[][] array1 = new int[3][3];
    int val1 = 0;
    int val2 = 0;
    int[][] array2 = new int[3][3];
    int val3 = 0;
    int val4 = 0;

    for (val1 = 0; val1 < 3; val1++) {
        for (val2 = 0; val2 < 3; val2++) {
            System.out.println("Please type number for column " + (val1 + 1) + " and row " + (val2 + 1));
            array1[val1][val2] = kb.nextInt();
        }
    }

    System.out.println("\nOriginal array is:");
    for (val1 = 0; val1 < 3; val1 += 1) {
        for (val2 = 0; val2 < 3; val2 += 1) {
            System.out.print(array1[val1][val2] + " ");
        }
        System.out.println("");
    }

    System.out.println("\nInverted array is:");
    for (int x = 0; x < 3; x += 1) {
        for (int y = 0; y < 3; y += 1) {
            System.out.print(array1[y][x] + " ");
        }
        System.out.println("");
    }

    System.out.println("\nThe Reversed array is:");
    for (int x = 2; x >= 0; x -= 1) {
        for (int y = 2; y >= 0; y -= 1) {
            System.out.print(array1[x][y] + " ");
        }
        System.out.println("");
    }
}

Edit: I added the reversed scenario you asked about in the comment below. You actually expected output like this:

9 8 7
6 5 4
3 2 1
Mike
  • 522
  • 2
  • 14