| Soorena (15) | |
|
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 | |
|
|
|
| Darkmaster (494) | |
| and what excactly is your problem? | |
|
|
|
| Soorena (15) | |
| I can solve this with compare it to 8 triangle but its need many loops, how i can write that with 2 loop? | |
|
|
|
| Darkmaster (494) | |
|
1 loop for rows and 1 for columns you also need if and please post the code, you have so far | |
|
Last edited on
|
|
| Soorena (15) | |
| this is exactly what my teacher says , and you can imagine my face mood when he says... | |
|
|
|
| Darkmaster (494) | |
|
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 | |
|
|
|
| usandfriends (186) | |
|
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. [edit]12.18.12: Almost done completing the code...[/edit] | |
|
Last edited on
|
|
| Soorena (15) | |
|
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 | |
|
|
|
| Darkmaster (494) | |
| yes you can also do this with 1 loop | |
|
|
|
| Soorena (15) | |
|
all the pattern with 1 loop!!!! its impossible! i bet.... you guys give usefull guidances , thank you very much | |
|
|
|
| TheIdeasMan (1753) | |
|
@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? | |
|
|
|
| Soorena (15) | |
|
//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?? | |
|
|
|
| Darkmaster (494) | |||
|
i would rather change it to compile first :P thats's your code:
| |||
|
Last edited on
|
|||
| Darkmaster (494) | |
|
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
|
|
| Soorena (15) | |
|
1.in line 7 its an unlimited loop to reload page after getch 2.Im beginner and dont know what is pointer exactly?! | |
|
|
|
| Darkmaster (494) | |
| just use a vector then, or dont use an array at all. you only need output here anyways | |
|
|
|
| Soorena (15) | |
|
thanks to giving your time i will study around thease | |
|
|
|
| usandfriends (186) | |
|
@ 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
|
|
| Darkmaster (494) | |
|
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
|
|
| Soorena (15) | |
|
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
|
|