• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating patterns  RSS feed

 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the life of me, I can't figure out how to create a pattern. On a practice mid-term, we're told to write a program that prints the following:


That's supposed to be an inverted pyramid. But as if it weren't bad enough I can't do it in Java, I can't even get it to print right when I type it here!

And I can't even figure out how to do

1
21
321
4321
etc.

in Java.

I've found places with code that does this, but I can't figure out the code, & it doesn't do me any good to reproduce it if I don't understand it. Can anyone explain it to me in English or pseudo code? Please?

Thank you!

BTW, I helped a classmate on our discussion board (it's an online class), & when he didn't thank me, I posted "you're welcome"...& he got upset because I was being snarky--& still didn't post a thank-you. This is an adult we're talking about, not a college kid. No wonder my college kids don't know how to behave.

And again...thank you!
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:

And I can't even figure out how to do


etc.

Have you used nested loops? You have an outer loop that loops for each row, and an inner loop that loops for each column. At each point you have to decide if you want to print a space or a digit. Note that a typical loop counter is initialized to zero, but that need not be the case.
 
Puspender Tanwar
Ranch Hand
Posts: 500
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Karen Guffey wrote:
And I can't even figure out how to do


etc.

in Java.


hope it helps
Puspender
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender, please don't post complete solutions. DontBeACodeMill (that's a link). Help guide the OP to their own solutions. I've removed most of the code so that there is just a hint for the OP to get started with.
 
Puspender Tanwar
Ranch Hand
Posts: 500
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Puspender, please don't post complete solutions. DontBeACodeMill (that's a link). Help guide the OP to their own solutions. I've removed most of the code so that there is just a hint for the OP to get started with.

OK Knute, will remember next time
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just got back into town. Going to start the attack. Thanks--I'll let you guys know how it goes!

BTW, what does "OP" stand for? Old Person?

And printing the code wouldn't have helped me, anyway. I've found code that I can't read. Being turned in the right direction & given a little push is just right!
 
Ron McLeod
Bartender
Posts: 1603
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OP = Original Poster - the person who started the topic
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm feeling like "Old Person" works better right now. I feel like a two-floppy drive computer that someone is trying to load Windows 10 onto.

I've spent 3 hours just trying to wrap my head around this. My only real success is figuring out how the outer loop works. I found half of an inverted pyramid, & I've tried to expand on that to see if I can figure out what the heck is going on, & the only thing I've succeeded in doing is adding a couple of (correct) lines to it. But I have figured out ONE thing that's tripping me up:



I realize that the loop with k is controlling the spaces that come at the beginning, but I can't figure out how. Imagine my original inverted pyramid cut in half so that the right side of the pyramid is gone. That's what the above code prints out. When I try to add numbers to the end of each line to complete the pyramid, the numbers are screwy, but so are the spaces, so I realized that until I could figure out exactly what that loop with k is doing, it doesn't do me much good to try to add numbers.

Also, are "for" loops like "while" loops in that if there's only one statement after the close parenthesis, that statement is the end of the loop? In other words, are the second two of the "for" loops above complete in themselves? Or is the last loop part of the one before it?

Thank you very much!

 
Puspender Tanwar
Ranch Hand
Posts: 500
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:Also, are "for" loops like "while" loops in that if there's only one statement after the close parenthesis, that statement is the end of the loop? In other words, are the second two of the "for" loops above complete in themselves? Or is the last loop part of the one before it?

if you don't put the parenthesis to the for() loop, the just next statement to the for() is the first and last statement of that for() loop

is same as :

If more than one statements are there after for() and no parenthesis are there:

it is same as:


now just a little tricky question for you:

in the above code: which statement is part of which for() loop ??

Hope it helps
Puspender
 
Puspender Tanwar
Ranch Hand
Posts: 500
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:

I realize that the loop with k is controlling the spaces that come at the beginning, but I can't figure out how. Imagine my original inverted pyramid cut in half so that the right side of the pyramid is gone. That's what the above code prints out. When I try to add numbers to the end of each line to complete the pyramid, the numbers are screwy, but so are the spaces, so I realized that until I could figure out exactly what that loop with k is doing, it doesn't do me much good to try to add numbers.

//loop 1 is for controlling the number of rows
//loop 2 is for controlling the number of spaces
//loop 2 is for controlling the number of digits
now come to your question:-> just look at your code, after the //loop1 Boolean expression is true, control going into its block and firstly is encounters the //loop2.
here System.out.print(" "); is responsible for printing the number of spaces is the current row. Once //loop2 is over(means all spaces have been printed), the control goes to the //loop3 . //loop3 is responsible for printing the digits : System.out.print(j); (Note: //loop2 and //loop3 are printing in the same ROW)
once //loop3 get finished, is runs println() which takes to the new line. Then the control is out of the current iteration. And then //loop1 again perfroms all these tasks .

my suggestion to you while solving such problems :
Step1. : print only Rows(1st element) according to the questions demand
step2 : print the columns i.e. the elements in the Row
step3: print other stuffs like "spaces", "*", or digits

print step 1,2,3 one by one, then you will also understand what is going on in all the loops

Hope it helps
Puspender
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote: . . .
if you don't put the parenthesis to the for() loop, the just next statement to the for() is the first and last statement of that for() loop
. . .
You do not mean parenthesis. You mean braces.

Karen: always write braces when writing for if while try catch finally switch or do statements.
Use the code tags to make such text stand out. I Have added code tags to your expected output, changed where it says java to text, and removed some spaces; you had double the number on the left that you actually needed. Now you can see it in its full glory

Your code is getting better, but it looks as if you are trying to alter the code to get the output. Until you have written down what you want to do with pencil paper and eraser (the latter being the most important piece of hardware ‍), you will simply end up going round and round in circles.
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:Just got back into town. Going to start the attack. Thanks--I'll let you guys know how it goes!

BTW, what does "OP" stand for? Old Person?

And printing the code wouldn't have helped me, anyway. I've found code that I can't read. Being turned in the right direction & given a little push is just right!



Let me suggest a different approach:

since you want this output,




Can you write a program that gives this output ?



 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you aware of the StringBuilder class? Are you aware of what you get if you do this?Yes, the first statement after for (...) constitutes the body of the loop. So you need to look immediately after the ) to find the contents of the loop. That is why you should always avoid ; there are always use {} as well as correct indentation.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I'm too old for this.

Salvin, suggesting that I try to get the pattern I need with just a character definitely pointed me in the right direction & helped me get the inverted pyramid. Thank you!

Campbell, I wrote it out with pen & paper, & I finally got the left side of the pyramid. I never did get the right side. I couldn't make it count backwards, so to speak. I mean, I could only get 123, not 321. When I tried to count backwards, it messed up my pyramid.

And guess what: a very similar problem was on the mid-term, which I just completed. Thanks to you guys, I'm pretty sure I got at least the shape correct & the left side of the shape. We weren't allowed to run the programs. I'm seriously hoping for a D. I just don't think fast enough to be able to work out a new program in 20 minutes. Thankfully, for practice I'd done all but one, & with that one, I didn't even know where to start because it involved MATH. And not just int++ math. Probably wouldn't be hard for you guys, & I could have done it if I"d had a couple of hours, but I had only 20 minutes left for that problem.

Anyway, we're on to arrays now, & then, finally, thankfully, strings. WORDS!

Thanks for all your help.

I'll be back...
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you get to 7 you print 13 stars. You are printing 2n − 1 stars. You can print "**" n times and delete the last star by using an obscure character which nobody ever seems to use. Actually they do use it, all the time, and seem not to notice they are using it. See what happens if you print this:-
System.out.print("\b ");
Yes, everybody forgets good ol' backspace.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
YOU CAN BACKSPACE IN A PROGRAM???

So I assume you can also backspace more than once. Possibilities...
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:YOU CAN BACKSPACE IN A PROGRAM? . . .
If you have enough fingers.
 
Karen Guffey
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
9 to backspace & a thumb for the space bar?
 
salvin francis
Bartender
Posts: 1664
37
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Guffey wrote:
...
We weren't allowed to run the programs.
...


I really really hate this. Whats the purpose of writing a program on a paper ?

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!