Doha Kik
Doha Kik

Reputation: 109

Array loop program logic

I'm supposed to write a program that asks for the number of rows the user wants. For example is the user entered 4 as number it will display all numbers from 1 to 16 arranged in 4 columns and 4 rows. Something like this should be the output if 4 is entered:

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

As you can see there is a pattern. the first number to appear is 1 and then it increases until 4. Next line starts 8 then to 5. As you can see it is like a snake. But my program's not working. Although I have tried simulating my program, I cant still figure out what's wrong.

Another example is when 3 is entered:

1 2 3
6 5 4
7 8 9

Here's my current codes: please help me thanks

import java.util.*;
public class ArrayOutput2 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int number = 0;

        System.out.print("Enter number of rows: ");
        number = input.nextInt();

        int[][] num = new int[number][number];
        int k=1, i, j;

        if (number == 1)
        {
              num[0][0] = 1;
        }


            while(k< (number*number))
            {
                for(i=number; i>=1; i--)
                {
                   if (i%2==1)
                   {
                     for(j=number-1; j>=0; j--)
                         {
                             num[i-1][j]=k;
                             k++;
                         }
                   }

                   else
                         for(j=0; j<=number-1; j++)
                         {
                             num[i-1][j]=k;
                             k++;
                         }
                 }
           }
       for(i=number-1;i<0;i--)
       {
            for(j=number-1;j<0;j--)

             System.out.print(num[i][j]+"\t");
             System.out.println();   
       }

    }
}

Upvotes: 2

Views: 1252

Answers (3)

SomeJavaGuy
SomeJavaGuy

Reputation: 7347

this does produce the excepted output:

Scanner input = new Scanner( System.in );
int number = 0;
System.out.print("Enter number of rows: ");
number = input.nextInt();

int[][] num = new int[number][number];
int k=1, i=1, y = 0;
if (number == 1)
{
     num[0][0] = 1;
}

   while(y<number)
   {
       for(int x =0;x<number;++i,++x) 
            num[y][x] = i;


       ++y;
       if(y<number)
            for(int x = number-1;x>=0;++i,--x) 
                 num[y][x] = i;

       ++y;
   }
   for(i = 0;i<number;i++)
   {
        for(int j=0;j<number;++j)

         System.out.print(num[i][j]+"\t");
         System.out.println();   
   }

hope it works for you

Upvotes: 3

Peter Jaloveczki
Peter Jaloveczki

Reputation: 2089

I think using the 2D array is unnecessary in your case try this snippet:

 for (int i=0; i<number; i++) {
            if (i%2 == 0) {
                for (int j=i*number; j<(i+1)*number; j++) {
                    System.out.print((j + 1) + " ");
                }
            } else {
                for (int j=(i+1)*number; j>i*number; j--) {
                    System.out.print(j + " ");
                }
            }
            System.out.println();
        }

Upvotes: 0

cowls
cowls

Reputation: 24334

It looks like the num[][] array is set up correctly.

I think you just need to flip the < to >= in your last for loops.

As currently you are looping over i while i is less than 0, but it starts at 4 so it never enters the loops and therefore never prints anything to the console.

It should be:

 for(i=number-1;i>=0;i--)
 {
    for(j=number-1;j>=0;j--)

Upvotes: 4

Related Questions