Stop Microsoft
Miscellaneous => Programming & Networking => Topic started by: TheQuirk on 19 August 2005, 17:02
-
I figured that no one cared for this thread. It seems that I was incorrect. Next challenge:
Fill an n by n array in the following manner:
(http://img377.imageshack.us/img377/591/chal39cl.png)
For example, suppose that n = 3. Your program should create the following array:
[[1 2 3]
[8 9 4]
[7 6 5]]
-
#!/usr/bin/perl
print "n? ";
$n = ;
chomp($n);
if($n == "3") {
open(TEMP, ">/tmp/array");
print TEMP "1 2 3\n";
print TEMP "8 9 4\n";
print TEMP "7 6 5";
close(TEMP);
open(TEMP, " @array = ;
close(TEMP);
}
#The array is @array
is this what you mean?
-
is this what you mean?
:rolleyes:
-
for any positive integer x,
x + (x+1) = (x+1)^2 - x^2
example:
6 + 7 = 49 - 36
try it, it works.
I made this up all by myself.
-
I don't understand how to navigate through the square maze :(
Making an array is easy enough, I guess. But I don't see the pattern in the circular motion.
-
It's simple, worker201.
1 -> 2 -> 3
|
V
8 -> 9 4
^ |
| V
7 <- 6 <- 5
or for n=4
1 -> 2 -> 3 -> 4
|
V
12-> 13-> 14 5
^ | |
| V V
11 16<- 15 6
^ |
| V
10<- 9 <- 8 <- 7
-
That's even more confusing (an ascii art issue)
-
Fuck.
(http://illhostit.com/thumbnails/1405442234078754/sq3.bmp) illhostit.com - sq3.bmp (http://illhostit.com/files/1405442234078754/sq3.bmp)
-
Oh, ok. I kinda get it now.
Nobody post an answer to this, because I want to take a couple days to figure it out.
-
Oh, ok. I kinda get it now.
Nobody post an answer to this, because I want to take a couple days to figure it out.
Yes, me too.
-
learning a language to create and populate the arrays looks like the tricky part...
-
It shouldn't take longer than 2 mins to work out the strategy to do it.
It can be very tricky so just set small goals that show your strategy is working.
Think Recursively... What it being repeated?
Then you just need to work out the base steps and how to implement the recursion.
I didn't use an actual 2d dimensional array, I simulated one with a 1d array (n*n) if that helps.
-
i already posted answer
-
oh c'mon. there are lots of trivial solutions to this spiral thing. you can just write code to do it the way you'd do it on paper.
i suppose i could write a solution for you, but i don't want to spoil the fun if you want to solve it on your own :)
also, recursion isn't the best approach imo, i'd go for a simple state machine with iteration.
-
This is very kludgy, and done in bash. (Doesn't format the results properly)
Please don't laugh! :rolleyes:
When I finish my degree I will be better.
Honest. :D
[verwijderd door de beheerder]
-
i already posted answer
Except yours only works when n=3. What if n=8?
I tried Tux's with n=6, and it worked. Right on!
I really don't have the skills with initializing and populating arrays at this time, so I failed the challenge. Currently, I am working through the first chapter of K&R, relearning for loops. The last programming language I used (besides awk and JavaScript) was C++ in '97. So I kinda have to relearn.
-
Except yours only works when n=3. What if n=8?
I tried Tux's with n=6, and it worked. Right on!
I really don't have the skills with initializing and populating arrays at this time, so I failed the challenge. Currently, I am working through the first chapter of K&R, relearning for loops. The last programming language I used (besides awk and JavaScript) was C++ in '97. So I kinda have to relearn.
Yup I know how you feel.
I used to do a bit of C++ but now feel absolutely retarded when faced with a problem like this. :(
Time to brush up on things.
-
recursion is always the best solution :)
Anyway, in this excerise a recursive solution is pretty much the same as the iterative.
-
I was bored so I edited my script to format the results properly,
again it is kludgy.
Attached as matrix.txt :)
Also, I want one of you fuckers to demonstrate a solution in C/C++ or maybe Pascal so I can learn from it.
Preferably using function recursion but anything is good. :D
[verwijderd door de beheerder]
-
Nice work, I have verified your correctness using n=8.