Next challenge:

Fill an n by n array in the following manner:

For example, suppose that n = 3. Your program should create the following array:

[[1 2 3]

[8 9 4]

[7 6 5]]

**I'd love to see a solution from you clever folk.**

This is a little programming challenge posed by TheQuirkat 'the other place'.

**I'd love to see a solution from you clever folk.**

Next challenge:

Fill an n by n array in the following manner:

For example, suppose that n = 3. Your program should create the following array:

[[1 2 3]

[8 9 4]

[7 6 5]]

Instead I wrote a kludgy bash shell script to do the job!

Code: Select all

```
#!/bin/bash
echo -n "Enter n: "
read -e N;
MATRIX[0]='Results: \n'
Y=0
while [ $Y -le $[$N-1] ];do
let "Y += 1"
: $[POS_N = $[$N+1] * $Y]
MATRIX[$POS_N]='\n'
done
TOP=2
BOT=$N
LEFT=1
RIGHT=$N
COL=1
ROW=1
VAL=1
ARR=$[$COL + $[$[$N+1] * $[$ROW-1]]]
MATRIX[$ARR]=$VAL
: $((X = $N * $N))
until [ "$VAL" == "$X" ]
do
until [ "$COL" == "$RIGHT" ]
do
let "COL += 1"
let "VAL += 1"
ARR=$[$COL + $[$[$N+1] * $[$ROW-1]]]
MATRIX[$ARR]=$VAL
done
if [ "$COL" == "$RIGHT" ]; then
if [ "$VAL" != "$X" ]; then
let "RIGHT -= 1"
let "ROW += 1"
let "VAL += 1"
ARR=$[$COL + $[$[$N+1] * $[$ROW-1]]]
MATRIX[$ARR]=$VAL
fi
fi
until [ "$ROW" == "$BOT" ]
do
let "ROW += 1"
let "VAL += 1"
ARR=$[$COL + $[$[$N+1] * $[$ROW-1]]]
MATRIX[$ARR]=$VAL
done
if [ "$ROW" == "$BOT" ]; then
if [ "$VAL" != "$X" ]; then
let "BOT -= 1"
let "COL -= 1"
let "VAL += 1"
ARR=$[$COL + $[$[$N+1] * $[$ROW-1]]]
MATRIX[$ARR]=$VAL
fi
fi
until [ "$COL" == "$LEFT" ]
do
let "COL -= 1"
let "VAL += 1"
ARR=$[$COL + $[$[$N+1] * $[$ROW-1]]]
MATRIX[$ARR]=$VAL
done
if [ "$COL" == "$LEFT" ]; then
if [ "$VAL" != "$X" ]; then
let "LEFT += 1"
let "ROW -= 1"
let "VAL += 1"
ARR=$[$COL + $[$[$N+1] * $[$ROW-1]]]
MATRIX[$ARR]=$VAL
fi
fi
until [ "$ROW" == "$TOP" ]
do
let "ROW -= 1"
let "VAL += 1"
ARR=$[$COL + $[$[$N+1] * $[$ROW-1]]]
MATRIX[$ARR]=$VAL
done
if [ "$ROW" == "$TOP" ]; then
if [ "$VAL" != "$X" ]; then
let "TOP += 1"
let "COL += 1"
let "VAL += 1"
ARR=$[$COL + $[$[$N+1] * $[$ROW-1]]]
MATRIX[$ARR]=$VAL
fi
fi
done
for ((i=0;i<$[$X + $N + 1];i++)); do
echo -en "\t${MATRIX[${i}]}"
done
```

[01 02 03 04] = array element one

[12 13 14 05] = array element two

[11 16 15 06] = array element three

[10 09 08 07] = array element four

So you have one array, with n number of elements, and each element is an array with n number of elements. And the elements follow a pattern.

Actually, to be totally correct, the program should place them into the arrays in ordinal order - 5 should be the 5th element, etc. Tux's doesn't do this - but perhaps one day it will.

Great!Void Main wrote:Ok, I see the pattern now. I've been working on something else and couldn't put full attention on it. I still can't put full attention on it but at least I understand the question now (duh). I'll try and look at it sometime this weekend.

I'd love to see how this is done. Properly.

Yeah, I think that's the way it's supposed to be done but bash doesn't seem to have them.Void Main wrote: Wouldn't you want to use a 2 dimensional array? I would actually probably use C.

Code: Select all

```
#!/usr/bin/perl -w
use strict;
print "Enter n: ";
chomp(my $n = <>);
my @shiza;
my $firstD = my $secondD = 0;
my $cnt = my $inc = 1;
while ($cnt <= ($n*$n)) {
while (!$shiza[$firstD][$secondD] && $secondD < $n && $inc == 1) {
$shiza[$firstD][$secondD] = $cnt;
$secondD++ unless($shiza[$firstD][$secondD+1] || ($secondD+1) >= $n);
$cnt++;
}
$firstD++;
while (!$shiza[$firstD][$secondD] && $firstD < $n && $inc == 1) {
$shiza[$firstD][$secondD] = $cnt;
$firstD++ unless($shiza[$firstD+1][$secondD] || ($firstD+1) >= $n);
$cnt++;
}
$secondD--;
$inc = 0;
while (!$shiza[$firstD][$secondD] && $secondD >= 0 && $inc == 0) {
$shiza[$firstD][$secondD] = $cnt;
$secondD-- unless($shiza[$firstD][$secondD-1] || ($secondD-1) < 0);
$cnt++;
}
$firstD--;
while (!$shiza[$firstD][$secondD] && $firstD >= 0 && $inc == 0) {
$shiza[$firstD][$secondD] = $cnt;
$firstD-- unless($shiza[$firstD-1][$secondD] || ($firstD-1) < 0);
$cnt++;
}
$secondD++;
$inc = 1;
}
for (@shiza) {
for (@{$_}) {
print "$_\t" if($_);
}
print "\n";
}
```

I am not quiet sure what worker is talking about when he stated something about "ordinal order" but I think my code above satisfies the challenge.

I used to develop for a message board system and a couple of users actually came to the support forum one time to complain about the off color humor in the comments of the source code.

I did not write the comments but I thoguht they were humorous and could not see why someone would have an issue with it. I told them to not look at the source code if it offended them that much. LOL..

I mean come one, it is not like these comments were in the actual displayable text for the message board. Some people just need something to complain about to make themselves feel happy.