+ 2
How to make pattern
*** *** * *** *** https://code.sololearn.com/cpRPW5UbXV8o/?ref=app
9 Réponses
+ 3
idk what you were hoping for with that jumble of unconnected ifs, you were always gonna have problems doing it like that. Let's start by linking those all together as a chain of else ifs (and a final else instead of no condition on that last *), so that it only ever prints one character per iteration.
That actually gets us surprisingly close by itself, but you'll also want to switch your first two clauses around--it's generally a good idea to list the most restrictive conditions first, even if you're not sure if it matters--and of course correct the typo so it checks for j==4, rather than i == 2 and 4 at the same time lol
Just make those handful of changes and it'll work correctly.
+ 2
Orin Cook
But I have to do it with loop
+ 2
Harshita Tyagi Besides Orin Cook hints, I also recommend, about the chain of ifs:
1. Since there are only two possible chars to print, you can have a single if/else block. Practice on reducing conditionals - this will help your learning and yield simpler and faster code.
2. When you have more actions to choose from, use a switch clause instead of a if/elif/else chain.
0
hardcoding at its finest
0
I feel like hardcoding in this situation would be more along the lines of:
printf("*** ***\n *\n*** ***\n");
0
Right, I was just responding to Jared there
0
Extra credit, since we're talking about reducing to a single if/else: this is a good chance to practice your ternary operator skills. idk offhand if there's a good ternary solution in this case, but it's good practice to think about it.
Also, since it's a simple alternating pattern, see if you can refactor to make it repeat for an arbitrary number of lines by using *=(-1) somewhere
0
please a look on below code
https://www.sololearn.com/compiler-playground/cXXREMe1miya
0
Nitesh Pawar Pls avoid giving finished code as answer, because it makes the OP skip the most important part of learning. And it encourages the Bad Habit of asking others to solve one's tasks. Prefer giving hints for the OP to find the solution instead.