0

I'm struggling to find an answer to the problem below. User inputs rows and columns. Example below is given for 4 x 4 matrix.

1 8 9 16

2 7 10 15

3 6 11 14

4 5 12 13

I cannot find how to relate the numbers when printing the array. The only obvious relation is how it goes downwards and upwards. From my perspective looks really hard. I'm just a beginner.

Not quite sure if there is any point to post the code, but it's just the basic lines:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter your array rows: ");
    int rows = scanner.nextInt();

    System.out.println("Please enter your array columns: ");
    int columns = scanner.nextInt();

    int[][] array = new int[rows][columns];

    int counter = 0;
    for (int j = 0; j < columns; j++){
        for (int i = 0; i < rows; i++) {
            counter++;
            array[i][j]=...(stuck at the beginning);
        }

Probably I'd need to use several loops , not only the above-mentioned or probably it is totally wrong ... Thank you in advance!

8
  • you should store it row-wisely, array[0][0] = 1; array[1,3] = 15; array[2][1]=3 Commented Aug 30, 2015 at 17:47
  • Yes, sorry. I noticed I reversed them. Should be rows then columns. But still need help for it. Commented Aug 30, 2015 at 17:53
  • Must be a class going on. Here are now 7 questions created in the last hour exactly like this one. Commented Aug 30, 2015 at 17:54
  • That would be funny if they were related :) Commented Aug 30, 2015 at 17:55
  • from where the number 1 8 9 16 2 7 10 15 3 6 11 14 4 5 12 13 come from? Commented Aug 30, 2015 at 17:57

2 Answers 2

1

I think this should do it.

    int counter = 0;
    boolean top_to_bottom=true;
    for (int j = 0; j < columns; j++){
        for (int i = 0; i < rows; i++) {
            counter++;
            if(top_to_bottom)
             array[i][j]=counter;
            else
             array[rows-1-i][j]=counter;
        }
        if(top_to_bottom)
         top_to_bottom=false;
        else top_to_bottom=true;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Great ! Thank you! So actually in the 'for' statement it has to be columns first, then rows ?
0

It serves the purpose

import java.util.*;

public class Seq

{

public static void main(String[] args)

{

Scanner scanner = new Scanner(System.in);

System.out.println("Please enter your array rows: ");

int rows = scanner.nextInt();

System.out.println("Please enter your array columns: ");

int columns = scanner.nextInt();

 int[][] array = new int[rows][columns];

  int counter = 0;

for (int j = 0; j < columns; j++){

if(j%2==0) {

 for (int i = 0; i < rows; i++)
     {
        counter=counter+1;
            array[i][j]=counter;

        }
 } 
else

 {
      for (int i = rows-1; i >=0; i--) 

          {
                counter=counter+1;

                array[i][j]=counter;

            }
     }

  }

  for(int i=0;i<rows;i++)

    {

        for(int j=0;j<columns;j++)
      {

        System.out.print(array[i][j]+" ");

        }
        System.out.println();
         }
   }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.