- 3

What is the missing part

This is how suppose to be the output 55554 4443 332 21 1 This is my code here #include <iostream> using namespace std; int main() { for(int i=5;i>1;i--) { cout<<i; for(int j=2;j<i;j++) { cout<<i; } cout<<i-1; cout<<endl; } return 0; } But the out is 55554 4443 332 21

26th Oct 2022, 1:43 PM
Bacani Rocelle M.
Bacani Rocelle M. - avatar
17 Respostas
+ 6
Bacani Rocelle M. Just follow the previous pattern logic and initialise y with 5 and then decrement it https://code.sololearn.com/cDHj1fIrwt1T/?ref=app
26th Oct 2022, 2:18 PM
A͢J
A͢J - avatar
+ 2
1) take i>=1 // instead of i>1 2) assign j = 1 , comment first cout. Only print in loop. Between, how last output is 1,... Is it not supposed to be 0?
26th Oct 2022, 2:07 PM
Jayakrishna 🇮🇳
+ 2
Then take 1) i >= 1 And print if(i-1>0) cout<<i-1; // ok or not efficient?
26th Oct 2022, 2:24 PM
Jayakrishna 🇮🇳
+ 2
A͢J if(i > 1 && x+1 >= i) 👍😎
26th Oct 2022, 2:34 PM
Solo
Solo - avatar
+ 2
Your loop stops at 2 Because your condition states i > 1 So it 2 is the last num that is greater than 1 here Thats why you need to do i>=1 Or i>0 To make it work
27th Oct 2022, 6:07 PM
ASM
ASM - avatar
+ 1
Yes the last output must 1
26th Oct 2022, 2:07 PM
Bacani Rocelle M.
Bacani Rocelle M. - avatar
+ 1
Bacani Rocelle M. Yes, I also think that it is better to use the full conditional expression "if... else" than "continue", yes and the variable j is superfluous: for(int i = 5, x = 0; i > 0;){ if(x < i){ if(i > 1 && x+1 >= i) cout << i-1 << endl; else cout << i; x++; } else { i--; x = 0; } } //Or: for(int i=5, j=0; i > 0;){ if(++j >= i){ j = 0; if(--i==0) cout << 1; else cout << i << endl; }else cout << i; }
27th Oct 2022, 12:12 AM
Solo
Solo - avatar
+ 1
Bacani Rocelle M. I edited the code. If you want to address someone personally click on @ 😎
27th Oct 2022, 1:04 AM
Solo
Solo - avatar
+ 1
Bacani Rocelle M. I mean, do you want me to explain your code? 🤔 You need five lines means i>0. You don't need the last value of i, so stop the loop if(i==1)break; If the teacher forbade the use of "break" when solving this task, then change the condition to if(i != 1) cout<<i-1<<endl; for(int i=5;i>0;i--){ cout<<i<<' '; for(int j=2;j<i;j++) cout<<i<<'-'; if(i==1)break; cout<<i-1<<endl; } P. S: "In the end, you could just add cout to the end of your code cout<<1;"😉
27th Oct 2022, 2:56 AM
Solo
Solo - avatar
+ 1
this one works, u said only using for loop right? #include <iostream> using namespace std; int main() { for(int i=5;i>=1;i--) { cout<<i; for(int j=2;j<i;j++) { cout<<i; } for(int k=i-1;k>0;k -= i) { cout<<i-1<<endl; } } return 0; } // edit : this code is not efficient, its better to use if statements
27th Oct 2022, 11:30 PM
LeaFlub
LeaFlub - avatar
0
Solo Same thing just way is different
26th Oct 2022, 4:58 PM
A͢J
A͢J - avatar
0
Aj but our prof said that dont use break or continue just use loops
26th Oct 2022, 10:31 PM
Bacani Rocelle M.
Bacani Rocelle M. - avatar
0
Thank you very much for the guide solo ill study this one
27th Oct 2022, 12:14 AM
Bacani Rocelle M.
Bacani Rocelle M. - avatar
0
Solo ok if i cant figure out my code
27th Oct 2022, 1:07 AM
Bacani Rocelle M.
Bacani Rocelle M. - avatar
0
Solo thank you for explaining
27th Oct 2022, 4:44 AM
Bacani Rocelle M.
Bacani Rocelle M. - avatar
0
Please use this code #include <iostream> using namespace std; int main() { for(int i=5;i>=1;i--) { cout<<i; for(int j=2;j<i;j++) { cout<<i; } if(i-1 > 0){ cout<<i-1; } cout<<endl; } return 0; }
27th Oct 2022, 4:39 PM
Prateek Kumar Singh
Prateek Kumar Singh - avatar
0
You could have just changed ; i>1; to ; i>=1; In the first for loop, and that would have given you the desired output.
28th Oct 2022, 1:40 PM
David Bartholomew
David Bartholomew - avatar