### pattern

Pages: 12
please help me to make program that print this pattern with Arbitrary <n> by just 2 for loop !!!

example : 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

sorry to take your time and sorry for my terrible english

and what excactly is your problem?
I can solve this with compare it to 8 triangle but its need many loops, how i can write that with 2 loop?
1 loop for rows and 1 for columns
you also need if

and please post the code, you have so far
Last edited on
this is exactly what my teacher says , and you can imagine my face mood when he says...
i'm not going to solve this for you.
post what you have so far and i can tell you what to change and what your problems are. but i won't just give you the code
Whenever I program a problem, I always look for patterns.

In this particular problem:

You see that the width of the square is 7 when you enter 4. Therefore, we can come up with the equation `2n-1=width.`

Next you see that the width is decreasing by 2s. 7 1s to 5 2s to 3 3s and so on. Therefore, we can come up with the for loop:

`for(i=0;i<=width;i++) //Where width is 2n-1 `

And we can continue this with our other equation, width-2=new width of next number.

`for(i=0;i<=width-new;i++) //Where new is =0 and then after every loop, n=n+2 `

We know the middle of the loop, where n will be placed is with the equation: n=middle because for example, you will have 1s,then 2s, then 3s, then 4s. So 4=4. After this middle, the loop can re-loop the others backward and finish. This is where the tricky part comes in. I am also a noob too ;).

So keep searching for patterns and good luck. I will also program this too for a good practice.

12.18.12: Almost done completing the code...[/edit]
Last edited on
i can chang the rows with 1 for loop
also with another i can print for example : 1233 -> this the third row of my example but i cant print numbers after second 3 -> 321 , for doing this i will need another loop
yes you can also do this with 1 loop
all the pattern with 1 loop!!!!
its impossible!
i bet....

you guys give usefull guidances , thank you very much
@usandfriends

This is the same code as in your other thread that talked about it being a virus.

And it is full of goto's - which we have already talked about.

And it uses printf, which is poor form for a C++ program.

It is also unnecessarily long IMO.

@Soorena

So I would ignore usandfriends code. Where is your code?
//znu 91463131//
#include<iostream>
#include<conio.h>
#include<stdio.h>
using namespace std;
main()
{
for(;;)
{
int n;
printf ("please enter a number lower than 14 : "), scanf ("%d",&n);
int r [2*n-1][2*n-1];
if (n<=13)
{
for (int i=n ; i>=1 ; i--)
{
for (int j=1 ; j<=2*n-1 ; j++)
{
r[j][i]=i,r[i][j]=i,r[j][2*n-i]=i,r[2*n-i][j]=i;
}
}
for (int i=1 ; i<=2*n-1 ; i++)
{
for (int j=1 ; j<=2*n-1 ; j++)
{
printf ("%3d",r[i][j]);
}
printf("\n");
}
}
else printf ("your number is too big","\n");
getch();
}
}

how can i change this to run just with 1 loop??
i would rather change it to compile first :P

 ``123456789101112131415161718192021222324252627282930313233`` ``````#include #include #include using namespace std; main() { for(;;) { int n; printf ("please enter a number lower than 14 : "), scanf ("%d",&n); int r[2*n-1][2*n-1]; if(n<=13) { for (int i=n ; i>=1 ; i--) { for (int j=1 ; j<=2*n-1 ; j++) { r[j][i]=i,r[i][j]=i,r[j][2*n-i]=i,r[2*n-i][j]=i; } } for (int i=1 ; i<=2*n-1 ; i++) { for (int j=1 ; j<=2*n-1 ; j++) { printf ("%3d",r[i][j]); } printf("\n"); } } else printf ("your number is too big","\n"); getch(); } }``````
Last edited on
line 7: whats that for loop for?

in line 11 you try to set up an array. it's size gets set during runtime via user-input. that's only possible with pointers.
Last edited on
1.in line 7 its an unlimited loop to reload page after getch
2.Im beginner and dont know what is pointer exactly?!
just use a vector then, or dont use an array at all. you only need output here anyways
i will study around thease
@ Soorena: Your code compiles but doesn't work. The program glitches out when I enter 13 and when I entered 9, it stopped working completely. I am fixing my code so it wont have gotos and viruses.

@TheIdeasMan: Yeah, caught that didn't you. Don't worry, fixed my libraries, now fixing my code. Will make you proud!
Last edited on
thats not my code, that is soorenas.
it also says "that's your code" in my post :P
i just edited it a little and used codetags, to improve readability
Last edited on
usandfriends : My code are tested with all 13 input;and i dont see any problem!!
my compiler cant compile your libraries in code that you written before!
Last edited on
Pages: 12