+ 2

How to make pattern

*** *** * *** *** https://code.sololearn.com/cpRPW5UbXV8o/?ref=app

25th May 2023, 6:22 AM
Harshita Tyagi
Harshita Tyagi - avatar
9 ответов
+ 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.
25th May 2023, 6:39 AM
Orin Cook
Orin Cook - avatar
+ 2
Orin Cook But I have to do it with loop
25th May 2023, 3:26 PM
Harshita Tyagi
Harshita Tyagi - avatar
+ 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.
25th May 2023, 4:46 PM
Emerson Prado
Emerson Prado - avatar
0
hardcoding at its finest
25th May 2023, 11:26 AM
Jared
Jared - avatar
0
I feel like hardcoding in this situation would be more along the lines of: printf("*** ***\n *\n*** ***\n");
25th May 2023, 1:57 PM
Orin Cook
Orin Cook - avatar
0
Right, I was just responding to Jared there
25th May 2023, 3:45 PM
Orin Cook
Orin Cook - avatar
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
25th May 2023, 6:02 PM
Orin Cook
Orin Cook - avatar
26th May 2023, 8:38 AM
Nitesh Pawar
Nitesh Pawar - avatar
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.
26th May 2023, 9:13 AM
Emerson Prado
Emerson Prado - avatar