- 2

Can somebody solve this problem using nasted loops in c++ ?it's crashing my mind!

Out put: 1 2 3 4 0 2 4 1 3 0 3 1 4 2 0 0 1 2 3 4 Here's my attempt but it doesn't shows the same out put as i want and i know it could be solved using nasted loops #include <iostream> using namespace std; int main() { for(int j=1;j<=4;j++) { { if (j==1) { for(int l=1;l<=4;l++) cout<<l<<"\t"; cout<<"0\n"; } else if (j==2) { for(int q=1;q<=4;q++) if (q%2==0) cout<<q<<"\t"; for(int l=4;l>=1;l--) if (l%2!=0) cout<<l<<"\t"; cout<<"0\n"; } else if (j==3) { for(int q=1;q<=4;q++) if (q%2!=0) cout<<q<<"\t"; for(int l=1;l<=4;l++) if (l%2==0) cout<<l<<"\t"; cout<<"0\n"; } if (j==4) { cout<<"0\t"; for(int q=1;q<=4;q++) cout<<q<<"\t"; } } } }

14th Jun 2022, 11:11 AM
Emma
Emma - avatar
8 Antworten
+ 2
https://code.sololearn.com/c1EIcruZYrWJ/?ref=app The first three lines follow a given pattern but the last deviate. I did some tweak to get it fixed
14th Jun 2022, 12:05 PM
Baribor Saturday
Baribor Saturday - avatar
+ 4
Can you specify the input and also show us your attempt
14th Jun 2022, 11:29 AM
Baribor Saturday
Baribor Saturday - avatar
+ 4
Emma Please link your attempt from Sololearn Playground with your question for needed help.
14th Jun 2022, 11:32 AM
JaScript
JaScript - avatar
+ 1
Your implementation is unnecessarily long. The output seems to be following a given pattern and requires only two for loops
14th Jun 2022, 11:43 AM
Baribor Saturday
Baribor Saturday - avatar
+ 1
You're the best!!!!!!! Baribor Saturday
14th Jun 2022, 12:18 PM
Emma
Emma - avatar
+ 1
Emma That is a ternary operator
14th Jun 2022, 12:35 PM
Baribor Saturday
Baribor Saturday - avatar
+ 1
Emma I meant link your code in this way like Baribor Saturday did. That make a help more easier. Happy coding!
14th Jun 2022, 2:12 PM
JaScript
JaScript - avatar
0
What is " (i==4)?1:i " name? I wanna learn more about it. Baribor Saturday
14th Jun 2022, 12:30 PM
Emma
Emma - avatar