Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Nested Loop help

nested loop loop

  • Please log in to reply
5 replies to this topic

#1 Sarah Christine Castle

Sarah Christine Castle

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 24 February 2012 - 01:06 PM

Hello,
I'm extremely new to java, my major requires that I take a introduction to computer science class. Anyways I have a homework problem that I have not been able to solve.
This is what is asks..

"Write a segment of code involving a nested for loop that prints the
smallest odd positive integer on the first line, then the two smallest odd
positive integers (separated by a space) on the second line, then the three
smallest odd positive integers (separated by a space) on the third line, and so
on, until all odd positive integers up to (and including) 99 are printed on the
last line. In order words, your segment of code should print the following
pattern as its output:
1
1 3
1 3 5
1 3 5 7
... ... ...
... ... ... ...
1 3 5 ... ... 99
1 3 5 ... ... 97 99


I have been getting close, but i end up with
1
1
1 3
1 3
1 3 5
1 3 5
..........

and so on. it seems as though something is making the rows duplicate and i am not sure what to do from here.... any help would be much appreciated!
  • 0

#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 24 February 2012 - 01:42 PM

Please post your code so I can review what you did.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 Sarah Christine Castle

Sarah Christine Castle

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 24 February 2012 - 01:54 PM

public class nestedloop {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int number = 100;
for(int num1=1; num1 <= number; num1++)
{
for(int i=1; i<=num1 ; i++)
System.out.print(" " + i++);
System.out.println( );
}
}

}


sorry.... that would be helpful!
  • 0

#4 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 24 February 2012 - 02:28 PM

Instead of incrementing i from within the print statement, try reconstructing your loops so you more precisely control what you're wanting it to do. That way, all you have inside the inner for loop is System.out.print(" " + i);

As far as constructing your loops go, you don't have to increment the loop iterator variable by 1 each time. You can increment it by an arbitrary value. By incrementing by 1 each time, you're running your loops as 1, 2, 3, 4, and so on. This is why you end up with double output; because you have twice as many loop iterations as are needed. (Your clever use of the increment within the output statement "filters" out the even numbers.)

What you need to do is increment by 2 each time, so your loop iterator will have values of 1, 3, 5, 7, and so on. (Each successive iteration is 2 more than the last.) You can use the '+=' operator to increase your loop variable by an arbitrary amount.

Hope that makes sense. Give it another try and post your new code if you're still having trouble with it.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5 Sarah Christine Castle

Sarah Christine Castle

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 24 February 2012 - 04:09 PM

I get what you are saying about moving the i++ out because it is messing with the lines, and that I have to make it increment by 2, and have my print line like this System.out.print(" " +i); but I am not sure how the create that. Any suggestions or tips?
  • 0

#6 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 27 February 2012 - 06:31 AM

Yes, as I said in my above post, you can use the += operator to increment by any value you want. for instance:
i += 2;
increments 'i' by 2.

In your target output, both series are incrementing by 2 each time. (The max value for each series grows by 2 each subsequent line, and inside each series, the number grows by 2 with each iteration.) So just restructuring your for loops with those in mind should solve your problem.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid






Also tagged with one or more of these keywords: nested loop, loop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download