noobuser1234
noobuser1234

Reputation: 11

Need to print rows of multidimensional array as columns

This is my program output:

Enter the size of 2D array: 
4
1   2   3   4   

5   6   7   8   

9   10  11  12  

13  14  15  16

and I need this instead:

1    12    5    16
2    11    6    15
3    10    7    14
4    9     8    13

I want the 2d array to be of size NxN where n is the integer inputted by the user. I want the first consecutive values to be stored in the even indexed columns from top to bottom and the next consecutive values to be stored in the odd indexed columns from bottom to top.

import java.util.Scanner;

public class Lab6 {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of 2D array: ");
    System.out.println();
    int n = input.nextInt();

    int arr[][] = new int[n][n];

    int inc=1;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
        arr[i][j]=inc;
        inc++;
        }
    }

    transpose(arr);

    // now let's print a two dimensional array in Java
    for (int[] a : arr) {
        for (int i : a) {
            System.out.print(i + "\t");
        }
        System.out.println("\n");

    }
}
public static int[][] transpose (int[][] array) {
      if (array == null || array.length == 0)//empty or unset array, nothing do to here
        return array;

      int width = array.length;
      int height = array[0].length;

      int[][] array_new = new int[height][width];

      for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
          array_new[y][x] = array[x][y];
        }
      }
      return array_new;
    }
}

Upvotes: 0

Views: 525

Answers (2)

theawesometrey
theawesometrey

Reputation: 428

Try this out. It should do what you have described in the question.

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.print("Enter the size of 2D array: ");
    System.out.println();
    int n = input.nextInt();
    int arr[][] = new int[n][n];

    int inc=1;
    for(int j = 0; j < n; j += 2) {
        for(int i = 0; i < n; i++) {
            arr[i][j]=inc++;
        }
    }
    for(int j = 1; j < n; j += 2) {
        for(int i = n - 1; i >= 0; i--) {
            arr[i][j]=inc++;
        }
    }

    // now let's print a two dimensional array in Java
    for (int[] a : arr) {
        for (int i : a) {
            System.out.print(i + "\t");
        }
        System.out.println();
    }
}

Upvotes: 1

Idle_Mind
Idle_Mind

Reputation: 39152

As Sean pointed out in the comments, your transpose() function returns a new array but you are not capturing that and using it. The original array remains unchanged, which is what you are displaying at the end.

Change:

transpose(arr);

// now let's print a two dimensional array in Java
for (int[] a : arr) {

To:

int[][] newArr = transpose(arr);

// now let's print a two dimensional array in Java
for (int[] a : newArr) {

Upvotes: 2

Related Questions