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.
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]]

#!/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


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.
Void Main wrote: Wouldn't you want to use a 2 dimensional array? I would actually probably use C.

#!/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";
}


Users browsing this forum: No registered users and 1 guest