I tried writing a code for the required output myself before watching the vid. I came up with a logic which seems perfect to me but upon executing it the output is totally wrong. Can I get the flaws in the logic related to the required output pointed out?
This is the code I wrote
#include
using namespace std;
main()
{
int rows,val,r,i,j,z,k;
rows=1;
r=4;
z=4;
while(rows<=4)
{
val=rows;
for(i=1;i<=7;i++)
{
if(i<r || i>z)
{
cout<<" ";
}
else if(i>=r && i<=4)
{
cout<<val;
val=val+1;
}
else
{
k=val-1;
for(j=i;j<=z;j++)
{
k=k-1;
cout<<k;
}
}
}
cout<<endl;
r=r-1;
z=z+1;
rows=rows+1;
}
}
The output I get is
1
232
345434
4567654656
I am getting redundant values in the third and fourth lines,which increase even more if I increase the number of rows. Please help me improve my logic and see the flaw which is causing these redundant values to appear.