Count down

• 03-14-2011, 03:56 PM
tj_wolf2
Count down
How can I get my code to count down like this
6 5 4 3 2 1
__5 4 3 2 1
___4 3 2 1
___ 3 2 1
_____ 2 1
______ 1
I am getting this
654321
_65432
__6543
___654
____65
_____6

Here is my code.

import java.util.Scanner;
public class genTower
{
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
int n=0;
do {
System.out.print("Enter a positive integer : "); n = input.nextInt();
} while( n <= 0);
System.out.println();
for(int i = 1; i <=n; i++)
{
for(int j = 1; j <= i; j++)
{
System.out.print(" ");
}
for(int k = n; k >= i; k--)
{
System.out.print( k );
}
System.out.println();
}
}
}
• 03-14-2011, 04:06 PM
berkeleybross
Im unsure of the exact maths required - you need to work it out - but instead of
Code:

```for(int j = 1; j <= i; j++) { System.out.print(" "); }```
you should use
Code:

```for(int j = 1; j <= i / 2; j++) { System.out.print(" "); }```
In future please use [code] [/code] tags - it preserves spaces and makes code much more readable. It would have come in handy when describing your output, as its been horribly mangled!
• 03-14-2011, 04:13 PM
tj_wolf2
My main concern is how to get each row to drop the highest number e.i. 321, 21, 1 instead of dropping the lowest 321, 32, 3. And Ill remember to use the . Thanks
• 03-14-2011, 04:21 PM
berkeleybross
Apologies, the formatting threw me off the obvious error!

Your problem is obviously in this loop:
Code:

```for(int k = n; k >= i; k--) { System.out.print( k ); }```
Think about what you are counting from and to. 6 - 1, 5 -1, 4-1 etc. So k needs to be greater than 0 (or greater or equal to 1).
The value you are starting from changes each time, ill let you think about what to set it to.

Code:

```for (int k = ???; k > 0; k--){     System.out.print( k ); }```
PS use the [noparse] [/noparse] tags to display tags ;-)
• 03-14-2011, 04:40 PM
tj_wolf2
Ok so ??? is n--, right?
Now I get
Code:

```654321  54321   4321```
what am I messing up on to have it finish out the 321, 21, 1?
• 03-14-2011, 04:48 PM
berkeleybross
Close, but not quite. n should not be changed.
You are only getting three outputs because on the fourth run through, i is four and n is three, so the outer for loop terminates.

k needs to be initiated at 6, then 5, then 4 etc. Or we can call it n - 1, n -2, n - 3 etc.
So we need some variable which counts how many times we have initialised k. Personally, I'd call it i, wouldnt you?

k = n - i;

btw, what did you type when trying to do the tags? How did you get [] and [/]?
• 03-14-2011, 04:59 PM
tj_wolf2
Alright getting close. With k = n - i it takes 1 from n right away so if I type 6 it starts 5 right? I need it to start with 6.
I first put [] [/] and that didnt work then I changed to [noparse] [/noparse] then switched to [code] [/code] to align it right.
• 03-14-2011, 05:50 PM
berkeleybross
You've got two options to rectify that.
First: add one to the result
Second: Deduct one from i
Personally, I recommend the second option - its slightly more efficient, and is "the C way". Most things in C are 0 based - for instance, the first position in the array is located at [0].

So, to correct this you would change the for loop:
Code:

`for(int i = 0; i < n; i++)`
Note i've changed the first two statements in the for loop.

The first way is just as simple, but is less efficient (one or two more clock ticks (gasp))
Code:

`k = n - i + 1;`
• 03-14-2011, 06:07 PM
tj_wolf2
Ok that works out. Thanks for all the help the code and how to post.