Tried tackling the question before watching the vid and failed

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.

Hey @Nitin-Bhattacharyya-2319140631658033
Please share your code in Coding Blocks IDE

Also I suggest u to to see the video first because u are trying to write a soln which is specific to given sample and not a genral code
So this will not help

this is the code,it is very close to the actual output but i think there is mainly some error in the for loop inside the last else.
The required output was(in a pyramid)
1
232
34543
4567654

Hey @Nitin-Bhattacharyya-2319140631658033
Here updated ur code https://ide.codingblocks.com/s/352427

But its not generic and only test specific

Hey thanks a lot,it was such a minor mistake that both complicated and made the program wrong.I was essentially running that loop twice and making the values come twice.I was at it for 2-3h and now I can finally rest. Thanks again.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.