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;}
5 Respostas
+ 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
+ 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;
}
}
0
It can't run because baris is not defined
0
It can't run because baris is not defined
0
It can't run because baris is not defined