• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested Loops Pattern

 
Melissa Ann
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to develop and run a program that generates the following output

1
234
56789
234
1

I can't figure out how to make it anything other than
1
12
123
1234
12345
123456
1234567
12345678
123456789

Help please )
 
Campbell Ritchie
Sheriff
Pie
Posts: 49364
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That looks like a recursive problem to me, but you probably don’t know about recursion yet.
Get a sheet of paper, a pencil and an eraser; the last is the most important piece of equipment at this stage. Start by writing down how you intend to write
1
12
123
1234
12345
123456
Since you say you have got that to work, it shouldn’t be too difficult. Then work out the differences. Do it bit by bit. And don’t try to write Java nor pseudocode at this stage. Write plain English.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12143
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you start off by saying "I need to use nested loops to solve this problem", you're doing it wrong. You start off with a blank page, and say "What do I need to do?".

I think starting off as you did is like saying "I need to use a hammer to drill a hole". Nope. that's not gonna work. You start by saying "I need a 3/8" hole in a wall approx. 2 inches deep. It may have to go through drywall and some lumber. I will not have access to electricity, but can carry stuff where I am working. How should I accomplish this?"

THEN you look in your toolbox for something that can do that - like a cordless screw-drill.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1158
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:If you start off by saying "I need to use nested loops to solve this problem", you're doing it wrong. You start off with a blank page, and say "What do I need to do?".

I think starting off as you did is like saying "I need to use a hammer to drill a hole". Nope. that's not gonna work. You start by saying "I need a 3/8" hole in a wall approx. 2 inches deep. It may have to go through drywall and some lumber. I will not have access to electricity, but can carry stuff where I am working. How should I accomplish this?"

THEN you look in your toolbox for something that can do that - like a cordless screw-drill.


What is lowercase baba Fred?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic