Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashtag and Slash Pattern Using For Loops?  RSS feed

 
Jimmer Fredette
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Input your desired width: 4
Input your desired length:45





Ive been trying to figure this out for quite a while now..i am a beginner..

Would using nested for loops work?
I can print out
#
##
###
####
but cannot figure out how to do the pattern shown in the example.
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch! Could you post the code you've tried?
 
Jimmer Fredette
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



the first couple things are the parameters for the width and length..
i was thinking of adding if statements inside the for loops
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An if statement inside the second for loop sounds good. You may want a variable that keeps track of which character needs to be printed.
 
Jimmer Fredette
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would the variable keep track of the character that needs to be printed..and what would be the condition for the if statement? I'm completely lost.
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you repost the pattern, and this time use "code" tags around the pattern. Web pages tend to eat white space but code tags prevent this. I'd like to see exactly what the pattern looks like.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jimmer Fredette wrote:I'm completely lost.

Us too (certainly me).

What is it supposed to print out given the parameters you provided?

Winston
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretend you are talking to a six-year-old. First put down the "#" block, then the "/" block, then the "\" block, then another "#" block, then start over. In her mind the six-year-old needs to keep track of which block is next. She would probably just use her memory but the program needs some sort of variable to "remember" which block is next. It could be as simple as int nextChar = 0; for the first "#", 1 for the "/", and so on.

Then you would test nextBlock and increment it or reset it.
 
Jimmer Fredette
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the examples, better pictured
Screen-Shot-2016-03-22-at-12.06.46-PM.png
[Thumbnail for Screen-Shot-2016-03-22-at-12.06.46-PM.png]
Screen-Shot-2016-03-22-at-12.08.43-PM.png
[Thumbnail for Screen-Shot-2016-03-22-at-12.08.43-PM.png]
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, after putting code tags around your pattern, I see that it is more complex than I thought, but my basic idea still holds, I think. You need to figure out when to print which character and keep track of which character is next.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It looks to me that you have the sort of problem you cannot solve with code. You need to know what you are going to print before you try writing any code at all. I suggest you lay your hands on some squared paper. Write some numbers across the top row. You might do well to start with 0 rather than 1 so you count 0 1 2 3 4 etc both ways. Now underneath those numbers write i % j. That means the remainder when you divide i by j. One of those numbers is the width of the display and the other is the height. Underneath those numbers write i % (2 * j). That means the remainder when you divide i by j doubled. Now write similar numbers down the left of the sheet, up to j.
What you now have is a grid, where you can compare the column (x dimension) and the row (y dimension going downwards).
Now you can see whether a particular square has y equal to one of the numbers along the top. Or, do y and one of the numbers across the top add up to j? Now fill in the characters you want \/#, and see where those characters appear. Do they appear where the number at the top is equal to the number on the left? Or where the top number and the left number add up to the same total?

Work that sort of thing out and you will have half your algorithm
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!