# Java homework: interlocking for (?) loops of numbers

Page 1 of 1

## 9 Replies - 712 Views - Last Post: 24 May 2011 - 06:12 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=232985&amp;s=a2e43d43d19fc56c58797d32a211147a&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 maniku

Reputation: 0
• Posts: 15
• Joined: 19-May 11

# Java homework: interlocking for (?) loops of numbers

Posted 22 May 2011 - 07:36 AM

Hi all,

Once again stuck with a Java homework: write a program which asks the user for upper limit (integer), then print the numbers in the following way (e.g.):

```limit: 8

1 8 2 7 3 6 4 5
```

I'm thinking, first, two if loops:

```if (limit % 2 == 0) { // limit is an even number.
```

and
``` if (limit % 2 != 0) { // limit is an odd number.
```

Then, under the first if loop, two for loops (e.g. limit = 8):

```for (i = 1; i < limit / 2; i++) { // ascending from beginning, 1 2 3 4.
```

and
```for (i = limit; i > limit / 2; i--) { // descending from end, 8 7 6 5.
```

And similarly, under the second if loop, two for loops (e.g. limit = 9):

``` for (i = 1; i < (limit + 1) / 2; i++) { // ascending from beginning, 1 2 3 4 5.
```

and
``` for (i = limit; i > (limit - 1) / 2; i--) { // descending from end, 9 8 7 6.
```

This is where I get stuck. It's easy to get the two sequences of numbers to print separately, but I can't figure out how to combine the for loops so that the numbers are printed alternatingly from the first and second sequence, as the homework tells me to do. Can someone give me hints on this? Thanks!

Is This A Good Question/Topic? 0

## Replies To: Java homework: interlocking for (?) loops of numbers

### #2 macosxnerd101

• Self-Trained Economist

Reputation: 9156
• Posts: 33,973
• Joined: 27-December 08

## Re: Java homework: interlocking for (?) loops of numbers

Posted 22 May 2011 - 07:48 AM

I would just use two counters in your loop. The first starts at 1, the other starts at the entered number. Continue as long as i < j, and increment i++ and j-- on each iteration. Then it simply comes down to printing i and j in the loop.

### #3 maniku

Reputation: 0
• Posts: 15
• Joined: 19-May 11

## Re: Java homework: interlocking for (?) loops of numbers

Posted 22 May 2011 - 07:51 AM

Sorry, in first if loop, first for loop, second condition should be <= and not <. Same in second if loop, first for loop, second condition.

macosxnerd101, on 22 May 2011 - 07:48 AM, said:

I would just use two counters in your loop. The first starts at 1, the other starts at the entered number. Continue as long as i < j, and increment i++ and j-- on each iteration. Then it simply comes down to printing i and j in the loop.

Ah, of course, that is much simpler. Thank you.

### #4 Dogstopper

Reputation: 2707
• Posts: 10,579
• Joined: 15-July 08

## Re: Java homework: interlocking for (?) loops of numbers

Posted 22 May 2011 - 07:58 AM

I used two for loops to do this.
```               int[] arr = new int[8];
int num = 8;
int i; // Keep it here so we can use it later

// Print out the first numbers....acsending
for (i = 1; i < arr.length; i += 2) {
arr[i] = num;
num--;
System.out.println(i);
}
i -= 2; // The for loop adds two onto it, so remove it

```

So that code put the 8, 7, 6, 5 numbers ascending skipping each array value
```		// This section causes it to offset by 1
if (i != arr.length-1)
i++;
else
i--;

```

Now, you need to start with an offset of one, so that your numbers do not overlap. You need to make sure it's in bounds after the fact.
```		// Print the descending numbers
for (; i >= 0; i -= 2) {
arr[i] = num;
num--;
}

```

Now, you can just descend

This is essentially the algorithm you can up with, but instead of messing with the condition, worry about the step.

### #5 jon.kiparsky

• Pancakes!

Reputation: 5602
• Posts: 9,040
• Joined: 19-March 11

## Re: Java homework: interlocking for (?) loops of numbers

Posted 22 May 2011 - 08:19 AM

There's More Than One Way To Do It, as the perl hackers say.

You could also keep one counter and subtract it from (limit+1).

Try coding that as an exercise - you'll see that you have to deal with the final number, which in a naive implementation will be printed twice if the initial limit is odd. Can you think of a way to do this without putting an if statement in the body of your loop?

ie, your basic loop is going to be
```{
print counter;
print limit-counter+1;
}
```

for 9, the last iteration of that will print 5,5 - we don't want that. See if you can come up with a good way to avoid that.
(no solutions, please! let this be an exercise...)

### #6 maniku

Reputation: 0
• Posts: 15
• Joined: 19-May 11

## Re: Java homework: interlocking for (?) loops of numbers

Posted 24 May 2011 - 01:32 PM

As the course I'm doing is Java basics, I went with what we have learned so far. I am ALMOST getting there:

```import java.util.*;

public class T59_Vuorotellen {

private static Scanner input = new Scanner(System.in);

public static void main(String[] args) {
System.out.print("Give number ");
int num = Integer.parseInt(input.nextLine());

for (int i = 1; i < num; i++) {
int a = num--;
int b = i;
System.out.print(b + " ");
System.out.print(a + " ");
}
}
}
```

This works if num is even, e.g. if num is 8, it prints: 1 8 2 7 6 5. However, if num is odd, it leaves the last number to be printed out. E.g. if num is 9, it prints 1 9 2 8 3 7 6 when it SHOULD print 1 9 2 8 3 7 6 5. I don't understand how I should alter my code so that it works with even and odd nums?

This post has been edited by maniku: 24 May 2011 - 01:41 PM

### #7 nick2price

• D.I.C Lover

Reputation: 559
• Posts: 2,826
• Joined: 23-November 07

## Re: Java homework: interlocking for (?) loops of numbers

Posted 24 May 2011 - 01:41 PM

I am terrible at maths so I may be completely wrong. Infact I am so bad at maths I am struggling to see how your code will print out what you say it does. Just going through it slowly to get my maths brain in gear!!! In the for loop, try adding +1 after num for (int i = 1; i < num+1; i++) Or maybe -1

### #8 maniku

Reputation: 0
• Posts: 15
• Joined: 19-May 11

## Re: Java homework: interlocking for (?) loops of numbers

Posted 24 May 2011 - 01:44 PM

nick2price, on 24 May 2011 - 01:41 PM, said:

I am terrible at maths so I may be completely wrong. Infact I am so bad at maths I am struggling to see how your code will print out what you say it does. Just going through it slowly to get my maths brain in gear!!! In the for loop, try adding +1 after num for (int i = 1; i < num+1; i++) Or maybe -1

Actually, corrected myself: it works with even nums but leaves the last number to be printed if the num is odd. I'm NOT a mathematician either, I just tested a code from another assignment and it worked, almost.

Tried adding +1 works, sort of: with even numbers, the result stays the same; with odd numbers, it prints the last number but twice.

This post has been edited by maniku: 24 May 2011 - 01:49 PM

### #9 nick2price

• D.I.C Lover

Reputation: 559
• Posts: 2,826
• Joined: 23-November 07

## Re: Java homework: interlocking for (?) loops of numbers

Posted 24 May 2011 - 01:48 PM

Damn, and I thought I was going to be able to call myself a mathematician after that advise!!! Wait a couple of minutes and someone who knows this stuff will show you how to do it.
good luck

### #10 jon.kiparsky

• Pancakes!

Reputation: 5602
• Posts: 9,040
• Joined: 19-March 11

## Re: Java homework: interlocking for (?) loops of numbers

Posted 24 May 2011 - 06:12 PM

Quote

This works if num is even, e.g. if num is 8, it prints: 1 8 2 7 6 5. However, if num is odd, it leaves the last number to be printed out. E.g. if num is 9, it prints 1 9 2 8 3 7 6 when it SHOULD print 1 9 2 8 3 7 6 5. I don't understand how I should alter my code so that it works with even and odd nums?

Read my initial statement of the problem again, carefully. There's a clue in there.

What is the nature of the problem, exactly? What is it not doing that it ought to be doing? When is it not doing that?