9 Replies - 701 Views - Last Post: 24 May 2011 - 06:12 PM Rate Topic: -----

#1 maniku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9044
  • View blog
  • Posts: 33,550
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 maniku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.

View Postmacosxnerd101, 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.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2696
  • View blog
  • Posts: 10,556
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5439
  • View blog
  • Posts: 8,755
  • 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...)
Was This Post Helpful? 0
  • +
  • -

#6 maniku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 nick2price  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#8 maniku  Icon User is offline

  • New D.I.C Head

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

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

Posted 24 May 2011 - 01:44 PM

View Postnick2price, 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

Was This Post Helpful? 0
  • +
  • -

#9 nick2price  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 559
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#10 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon

Reputation: 5439
  • View blog
  • Posts: 8,755
  • 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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1