~ubuntu-branches/ubuntu/natty/abs-guide/natty

« back to all changes in this revision

Viewing changes to abs/msquare.sh

  • Committer: Bazaar Package Importer
  • Author(s): Sandro Tosi
  • Date: 2010-05-09 17:49:52 UTC
  • mfrom: (1.2.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20100509174952-1s583w0zbh7rl1po
Tags: 6.2-1
* New upstream release; Closes: #441278
* debian/control
  - adopting package; Closes: #577181
  - removed bzip2 from b-d-i, no more needed
  - debhelper has to be in b-d
  - bump Standards-Version to 3.8.4 (no changes needed)
  - added Homepage field
  - added misc:Depends to Depends
  - added Vcs-{Browser, Git} field
* debian/rules
  - removed the unpacking stuff, we now use an alrqady unpacked source
  - added more example scripts to fix
* debian/{compat, control, rules}
  - switched to debhelper 7
* debian/watch
  - updated to new upstream location; thanks to Raphael Geissert for the
    report; Closes: #453596
* debian/copyright
  - updated with new location and copyright/license notices
* debian/source/format
  - set source package format to 1.0 explicitly
* debian/abs-guide.doc-base
  - set Section field correctly
* debian/{copyright, abs-guide.doc-base}
  - updated upstream email
* debian/abs-guide.lintian-overrides
  - added override for "known syntax-errored" scripts

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/bash
 
2
# msquare.sh
 
3
# Magic Square generator (odd-order squares only!)
 
4
 
 
5
# Author: mendel cooper
 
6
# reldate: 19 Jan. 2009
 
7
# License: Public Domain
 
8
# A C-program by Kwon Young Shin inspired this script.
 
9
# See http://user.chollian.net/~brainstm/MagicSquare.htm ...
 
10
 
 
11
# Definition: A "magic square" is a two-dimensional array
 
12
#             of integers in which all the rows, columns,
 
13
#             and *long* diagonals add up to the same number.
 
14
#             Being "square," the array has the same number
 
15
#             of rows and columns.
 
16
# An example of a magic square of order 3 is:
 
17
#   8  1  6   
 
18
#   3  5  7   
 
19
#   4  9  2   
 
20
# All the rows, columns, and long diagonals add up to 15.
 
21
 
 
22
 
 
23
# Globals
 
24
EVEN=2
 
25
MAXSIZE=31   # 31 rows x 31 cols.
 
26
E_usage=90   # Invocation error.
 
27
dimension=
 
28
declare -i square
 
29
 
 
30
usage_message ()
 
31
{
 
32
  echo "Usage: $0 square-size"
 
33
  echo "   ... where \"square-size\" is an ODD integer"
 
34
  echo "       in the range 3 - 31."
 
35
  #  Actually works for squares up to order 159,
 
36
  #+ but large squares will not display pretty-printed in a term window.
 
37
  #  Try increasing MAXSIZE, above.
 
38
  exit $E_usage
 
39
}
 
40
 
 
41
 
 
42
calculate ()       # Here's where the actual work gets done.
 
43
{
 
44
  local row col index dimadj j k cell_val=1
 
45
  dimension=$1
 
46
 
 
47
  let "dimadj = $dimension * 3"; let "dimadj /= 2"   # x 1.5, then truncate.
 
48
 
 
49
  for ((j=0; j < dimension; j++))
 
50
  do
 
51
    for ((k=0; k < dimension; k++))
 
52
    do  # Calculate indices, then convert to 1-dim. array index.
 
53
        # Bash doesn't support multidimensional arrays. Pity.
 
54
      let "col = $k - $j + $dimadj"; let "col %= $dimension"
 
55
      let "row = $j * 2 - $k + $dimension"; let "row %= $dimension"
 
56
      let "index = $row*($dimension) + $col"
 
57
      square[$index]=cell_val; ((cell_val++))
 
58
    done
 
59
  done
 
60
}     # Plain math, no visualization required.
 
61
 
 
62
 
 
63
print_square ()               # Output square, one row at a time.
 
64
{
 
65
  local row col idx d1
 
66
  let "d1 = $dimension - 1"   # Adjust for zero-indexed array.
 
67
 
 
68
  for row in $(seq 0 $d1)
 
69
  do
 
70
 
 
71
    for col in $(seq 0 $d1)
 
72
    do
 
73
      let "idx = $row * $dimension + $col"
 
74
      printf "%3d " "${square[idx]}"; echo -n "  "
 
75
    done   # Displays up to 13-order neatly in 80-column term window.
 
76
 
 
77
    echo   # Newline after each row.
 
78
  done
 
79
}
 
80
 
 
81
 
 
82
#################################################
 
83
if [[ -z "$1" ]] || [[ "$1" -gt $MAXSIZE ]]
 
84
then
 
85
  usage_message
 
86
fi
 
87
 
 
88
let "test_even = $1 % $EVEN"
 
89
if [ $test_even -eq 0 ]
 
90
then           # Can't handle even-order squares.
 
91
  usage_message
 
92
fi
 
93
 
 
94
calculate $1
 
95
print_square   # echo "${square[@]}"   # DEBUG
 
96
 
 
97
exit $?
 
98
#################################################
 
99
 
 
100
 
 
101
# Exercises:
 
102
# ---------
 
103
# 1) Add a function to calculate the sum of each row, column,
 
104
#    and *long* diagonal. The sums must match.
 
105
#    This is the "magic constant" of that particular order square.
 
106
# 2) Have the print_square function auto-calculate how much space
 
107
#    to allot between square elements for optimized display.
 
108
#    This might require parameterizing the "printf" line.
 
109
# 3) Add appropriate functions for generating magic squares
 
110
#    with an *even* number of rows/columns.
 
111
#    This is non-trivial(!).
 
112
#    See the URL for Kwon Young Shin, above, for help.