Hai guys i need to understand the looping of this code. Can u guys help me? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hai guys i need to understand the looping of this code. Can u guys help me?

int rows, i, j, k=0; cin>>baris; for(i=1;i<=baris;i++){ for(j=1;j<=i;++j) cout<<k+j<<" "; k++; cout<<endl;}

31st Mar 2017, 9:01 AM
Yabel Aurelius
Yabel Aurelius - avatar
5 Answers
+ 3
I corrected the code so it would compile, added braces to make clear what is iterated in the inner for loop and removed k, as it always is i-1. Run it, the comments in the code should explain it: https://code.sololearn.com/cAuBIswSrUe1/?ref=app
31st Mar 2017, 9:29 AM
Tob
Tob - avatar
+ 1
int rows, i, j, k=0; cin>>baris; for(i=1;i<=baris;i++){ for(j=1;j<=i;++j) cout<<k+j<<" "; k++; cout<<endl;} ---------------------------------------------------- int rows; int i; int j; int k = 0; cin>>baris; // it means 'i' starts from 1 and it is less than or equal the input value 'baris' // For e.g. baris = 3 then 'i' = 1, 'baris' = 3; it will loop, // i = 1, one time, i is still less than baris // i = 2; two time, i is still less than baris // i = 3; three time, i is not less than baris but same as baris so will loop // i = 4; i is not less than and same as baris, so it will loop three times. for(i = 1; i <= baris; i++) { // Same logic goes for j, // For the result, play around and figure it out. for (j = 1; j <= i; ++j) { cout<<k+j<<""; k++; cout<<endl; } }
31st Mar 2017, 9:20 AM
Yhal Htet Aung
Yhal Htet Aung - avatar
0
It can't run because baris is not defined
31st Mar 2017, 9:25 AM
Arnold
0
It can't run because baris is not defined
31st Mar 2017, 9:25 AM
Arnold
0
It can't run because baris is not defined
31st Mar 2017, 9:25 AM
Arnold