I want to generate the following pattern using for() loop only.... how can i do this i've tried but unfortunately failed to solve ....

when user inputs 3
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

when user inputs 4;

1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 1 1 1 1 1 1

I just want to know the algorithm or just a hint?

Recommended Answers

All 4 Replies

The first sequence you posted has a distinct pattern, but I see no repeating or logical pattern in the second sequence ? Try to put down your thoughts in a more concise manner and you would yourself know the algorithm.

I want to generate the following pattern using for() loop only.... how can i do this i've tried but unfortunately failed to solve ....

when user inputs 3
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

when user inputs 4;

1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 1 1 1 1 1 1

I just want to know the algorithm or just a hint?

sorry my Mistake it goes like this

1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1

You're making a series of nested boxes. The reason you failed is because you're trying to do it all at once, probably with no prior experience solving this kind of problem. So what you need to do is simplify it down to something that you can solve. For example, create a single filled box:

N: 4

1111111
1111111
1111111
1111111
1111111
1111111
1111111

Now draw it empty:

N: 4

1111111
1     1
1     1
1     1
1     1
1     1
1111111

The key element is that N designates a cross down the center of the box:

111*111
111*111
111*111
*******
111*111
111*111
111*111

You count up to N and then back down to 0. Once you understand and can implement all of this, try looking for patterns between the first row/column and the central row/column. That's where you come up with the final parts of the algorithm.

And no, I'm not going to tell you how to do it. It's important that you work through the process on your own. However, I can and will offer hints after you make some kind of attempt and show us your results.

You're making a series of nested boxes. The reason you failed is because you're trying to do it all at once, probably with no prior experience solving this kind of problem. So what you need to do is simplify it down to something that you can solve. For example, create a single filled box:

N: 4
 
1111111
1111111
1111111
1111111
1111111
1111111
1111111

Now draw it empty:

N: 4
 
1111111
1     1
1     1
1     1
1     1
1     1
1111111

The key element is that N designates a cross down the center of the box:

111*111
111*111
111*111
*******
111*111
111*111
111*111

You count up to N and then back down to 0. Once you understand and can implement all of this, try looking for patterns between the first row/column and the central row/column. That's where you come up with the final parts of the algorithm.

And no, I'm not going to tell you how to do it. It's important that you work through the process on your own. However, I can and will offer hints after you make some kind of attempt and show us your results.

Thank you for such a nice description... friend i come up with a solution with a difference logic but still thank you for your help...

//******************** for n=2;
/* 1 1 1
1 2 1
1 1 1 
*/
/********************* for n=3 ******************
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

/************** for n =4 ************************
1 1 1 1 1 1 1
1 2 2 2 2 2 1
1 2 3 3 3 2 1
1 2 3 4 3 2 1
1 2 3 3 3 2 1
1 2 2 2 2 2 1
1 1 1 1 1 1 1
*/
 
int n=5;
int m=1;
for(int i=1; i<=n; ++i)
{
for(int j=1; j<i; ++j)
cout<<j;
for(int j=i; j<(n*2-m); ++j)
{
cout<<m;
}
m++;
for(int k=i; k>=1; --k)
cout<<k;
cout<<endl;
}

int k=2;
for(int i=n; i>1; --i)
{
for(int j=1; j<i; ++j)
cout<<j;
for(int j=1 ;j<k; ++j)

cout<<(i-1);


for(int j=(i-1); j>=1; --j)
cout<<j;

k+=2;
cout<<endl;
}

I didn't analyze the problem... I was just seeking an answer from your forum. I did it and make it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.