Hourglass and For Loops | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Hourglass and For Loops

Hello!! I wrote this code: https://code.sololearn.com/cbne3YJXCuF7/?ref=app I was wondering how to get rid of the extra row of 3 *’s in the middle... also, I have to print “Invalid number of rows.” if rows is less than 1 or if rows is a number that leads to a row in the hour glass with less than two *’s. *WANT*: (If 7 and 3 are entered) ******* ***** *** ***** ******* ... the spaces kind of mess up here, but it should be right.

4th Oct 2018, 4:30 AM
Miqaela Weller
Miqaela Weller - avatar
4 Answers
+ 6
Your outer loops is based on topRows - equivalent to the width, not the height - which can lead to missing lines as the input becomes larger. It's a bit hard to follow with all the variable naming, so I'm sorry to say that I'm unable to point out which part is correct and which part isn't. Here's my version. It can take any input, as long as they're integers. I also named the variables differently for more readability. https://code.sololearn.com/cA0y3CTzC9kB/#cpp
4th Oct 2018, 5:45 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
+ 5
Although Hoàng Nguyễn Văn has answered your question in an amazing manner I think this might help... https://code.sololearn.com/cJ2P34jkeCHR/?ref=app
4th Oct 2018, 7:02 AM
Madhav
Madhav - avatar
+ 1
Miqaela Weller as per in my code if you will convert k = i to k = i - 1 that should do the work
5th Oct 2018, 5:40 AM
Madhav
Madhav - avatar
0
How do I get rid of the intital space in the beginning? its like the whole hourglass needs to move to the left one.
5th Oct 2018, 2:43 AM
Miqaela Weller
Miqaela Weller - avatar