A. Port
A. Port

Reputation: 43

can't find how to program this number pattern

Number Pattern

I am asked to enter a number rc, and based on rc construct this pattern. I am able to initialize the table but without the highlighted numbers:

int [][] num2 = new int [rc][rc];
counter = 1;

    for(int i = 0; i < rc; i++){
        if(i!=0)
        counter--;
        for(int j =0; j < rc; j++){
            num2 [i] [j] = counter;
            counter ++;
        }
    }

Any hints or ideas?

Upvotes: 2

Views: 90

Answers (3)

Frakcool
Frakcool

Reputation: 11153

The logic behind my solution is:

  • First fill an array from 1 - N, where N is the user input (or rc in this case):
  • Then, we check if it's not the first line, if it is, we simply print the numbers in order.
  • Now, we have to know which numbers go first:

    • In the line 1 (remember it starts from 0), it must print the number at [1][4] in [1][0], so our loop substracts rc - i + j, this gives: 5 - 1 + 0, which in fact is index [4].
    • We know that after we've printed the last numbers first, we must continue the sequence, so we print index: [1][0] at [1][1] (Why 1, 2? Because otherwise we would get something like the example below, that's why we need to substract 1 to it

      1    2    3    4    5
      10   7    8    9    10
      

And that's it:

public class StrangePattern {

    public static void main(String[] args) {
        int rc = 5;

        int number = 1;
        int spaces = 0;

        int[][] numbers = new int[rc][rc];

        for (int i = 0; i < rc; i++) {
            for (int j = 0; j < rc; j++) {
                numbers[i][j] = number;
                number++;
            }
        }

        for (int i = 0; i < rc; i++) {
            for (int j = 0; j < rc; j++) {
                if (i != 0) {
                    if (j < i) {
                        System.out.print(numbers[i][rc - i + j] + "\t");
                    } else {
                        System.out.print(numbers[i][j - spaces] + "\t");
                    }
                } else {
                    System.out.print(numbers[i][j] + "\t");
                }
            }
            spaces++;
            System.out.println();
        }
    }
}

Which provides this output:

1   2   3   4   5   
10  6   7   8   9   
14  15  11  12  13  
18  19  20  16  17  
22  23  24  25  21  

And this one for rc = 3:

1   2   3   
6   4   5   
8   9   7   

Upvotes: 1

MubtadaNaqvi
MubtadaNaqvi

Reputation: 197

The following code is working fine for your problem.

int rc=5;
        int [][] num2 = new int [rc][rc];
        int counter = 1;
            for(int i = 0; i < rc; i++){
                for(int j =i; j < rc; j++){
                    num2 [i] [j] = counter;
                    counter ++;
                }
                for(int k =0; k < rc; k++){
                    if(num2[i][k]==0){
                        num2 [i] [k] = counter;
                        counter++;
                    }
                    System.out.print(num2[i][k]+"\t");
                }
                System.out.println();
            }

Upvotes: 1

Peyman Mahdian
Peyman Mahdian

Reputation: 522

You got it partially right. The numbers printed on each row are the same but the start point is incremented by 1 each time. Thus, you can use variable i again to shift it.

int [][] num2 = new int [rc][rc];
int counter = 1;

for (int i = 0; i < rc; i++) {
    for (int j = 0; j < rc; j++) {
        num2[i][(j + i) % rc] = counter++;
    }
}

Upvotes: 1

Related Questions