1

Ive been working on this assignment question for about 4 days now and I'm about to go crazy. We have specific directions to follow for this code;

"Your program should proceed as follows:

  1. Display a welcome message.

  2. Prompt the user for an integer which is 3. If the number entered is < 3. keep prompting the user until they enter a number 3 (use a do/while). This number will determine the size of the square array.

  3. Fill the array as per pattern 1 and display it using printf to format the array.

  4. Fill the same array as per pattern 2 and display it using printf t0 format the array.

  5. Display a closing message."

Pattern: enter image description here

I'm still stuck on pattern one. I tried to do first do a for loop which in it there's an if statement that checks if the column number is even or not, if it is, to print out the code backwards. The question also recommends using a while loop and a do/while loop...?

Also any tips on to how to go about the second patterns.

Here is my code.

import java.util.Scanner;
public class a3q33 
{

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

    int n;

    do
    {
        System.out.println("How many rows/columns do you want your array to have? (Must be at least 3)");

        n=keyboard.nextInt();

    } while(n < 3);

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

     int i, j, k=1;

    for(i=0;i<n;i++)
    {
         if(i % 2 != 0)
         {   
             for(j=n;j>0;j--) 
             {
                 k = k+n; 
                 arr[i][j]=k;
                 k--;
             }
         }

         else
         {
             for(j=0;j<n;j++) 
             {
                 arr[i][j]=k;
                 k++;
             }
         }
    }

    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++) 
        {
            System.out.printf(arr[i][j]+" ");

        }
            System.out.printf("");
            System.out.println();
    }

    }
}

Any help would be greatly appreciated!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
lelfsearner32
  • 21
  • 2
  • 4

3 Answers3

0

I assume that you meant "row" instead of "column" for the condition check given that the convention is arr[row][column]?

Also, the for loop in your code:

for(j=n;j>0;j--)

This will decrement the row/column index down to 1 and not 0. So you will miss one element at arr[0][...] or arr[...][0]. Also, j=n will be out of bound.

Instead, try using:

for(j=n-1;j>-1;j--)

It would be a good first steps to look into.

Anthony
  • 1
  • 1
0
import java.util.Scanner; 

public class a3q33{
    public static void main(String[] args){
        Scanner keyboard = new Scanner(System.in);
        int n;
        do{
            System.out.println("How many rows/columns do you want your array to have? (Must be at least 3)");
            n=keyboard.nextInt();
        } while(n < 3);

        int count = 1;
        int [][] arr = new int [n][n];
        for(int i = 0;i<n; i++){
            if(i%2==0){
                for(int j = 0;j<n; j++){
                    arr [i][j] = count;
                    count++;
                }
            }
            else{
                for(int j = n-1;j>=0; j--){
                    arr [i][j] = count;
                    count++;
                }
            }             
        }


        System.out.println("\nPattern 1 \n");
        // print the array 
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                System.out.printf("%d\t",arr[i][j]);
            }
            System.out.println();
        }
        // reset count back to 1 and fill the array in the same way only without the if/else 
        // for n = 5 for example this will produce  [1, 2,  3,  4,  5 ]
        //                                          [6, 7,  8,  9,  10]
        //                                          [11,12, 13, 14, 15]
        //                                          [16,17, 18, 19, 20]
        //                                          [21,22, 23, 24, 25]

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

        // rotate arrays using arraycopy; for array at index 1 rotate by one, at index 2 by 2 and so on see 
        //http://stackoverflow.com/questions/31174840/how-to-rotate-an-array
        //
        for (int i = 1; i<n; i++){
            int [] temp = new int [n];
            for (int j = 0; j < n; j++) {
                temp[(j + i) % n] = arr[i][j];
            }
            System.arraycopy(temp, 0, arr[i], 0, n);
            arr[i]=temp;
        }

        System.out.println("\nPattern 2 \n");
         // print the array 
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                System.out.printf("%d\t",arr[i][j]);
            }
            System.out.println();
        }
    }
}
Eritrean
  • 15,851
  • 3
  • 22
  • 28
0

You can achieve this by checking the modulo:

for (int i = 0; i < n; i++) {
    int start = i * n;
    for (int j = 0; j < n; j++) {
        arr[i][j] = i * n + ((i % 2 === 0) ? (j + 1) : (n - j));
    }
}
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175