Debian Codegraph @

twodim.sh

raw symbols SCIP
1 #!/bin/bash
2 # twodim.sh: Simulating a two-dimensional array.
3
4 # A one-dimensional array consists of a single row.
5 # A two-dimensional array stores rows sequentially.
6
7 Rows=5
8 Columns=5
9 # 5 X 5 Array.
10
11 declare -a alpha # char alpha [Rows] [Columns];
12 # Unnecessary declaration. Why?
13
14 load_alpha ()
15 {
16 local rc=0
17 local index
18
19 for i in A B C D E F G H I J K L M N O P Q R S T U V W X Y
20 do # Use different symbols if you like.
21 local row=`expr $rc / $Columns`
22 local column=`expr $rc % $Rows`
23 let "index = $row * $Rows + $column"
24 alpha[$index]=$i
25 # alpha[$row][$column]
26 let "rc += 1"
27 done
28
29 # Simpler would be
30 #+ declare -a alpha=( A B C D E F G H I J K L M N O P Q R S T U V W X Y )
31 #+ but this somehow lacks the "flavor" of a two-dimensional array.
32 }
33
34 print_alpha ()
35 {
36 local row=0
37 local index
38
39 echo
40
41 while [ "$row" -lt "$Rows" ] # Print out in "row major" order:
42 do #+ columns vary,
43 #+ while row (outer loop) remains the same.
44 local column=0
45
46 echo -n " " # Lines up "square" array with rotated one.
47
48 while [ "$column" -lt "$Columns" ]
49 do
50 let "index = $row * $Rows + $column"
51 echo -n "${alpha[index]} " # alpha[$row][$column]
52 let "column += 1"
53 done
54
55 let "row += 1"
56 echo
57
58 done
59
60 # The simpler equivalent is
61 # echo ${alpha[*]} | xargs -n $Columns
62
63 echo
64 }
65
66 filter () # Filter out negative array indices.
67 {
68
69 echo -n " " # Provides the tilt.
70 # Explain how.
71
72 if [[ "$1" -ge 0 && "$1" -lt "$Rows" && "$2" -ge 0 && "$2" -lt "$Columns" ]]
73 then
74 let "index = $1 * $Rows + $2"
75 # Now, print it rotated.
76 echo -n " ${alpha[index]}"
77 # alpha[$row][$column]
78 fi
79
80 }
81
82
83
84
85 rotate () # Rotate the array 45 degrees --
86 { #+ "balance" it on its lower lefthand corner.
87 local row
88 local column
89
90 for (( row = Rows; row > -Rows; row-- ))
91 do # Step through the array backwards. Why?
92
93 for (( column = 0; column < Columns; column++ ))
94 do
95
96 if [ "$row" -ge 0 ]
97 then
98 let "t1 = $column - $row"
99 let "t2 = $column"
100 else
101 let "t1 = $column"
102 let "t2 = $column + $row"
103 fi
104
105 filter $t1 $t2 # Filter out negative array indices.
106 # What happens if you don't do this?
107 done
108
109 echo; echo
110
111 done
112
113 # Array rotation inspired by examples (pp. 143-146) in
114 #+ "Advanced C Programming on the IBM PC," by Herbert Mayer
115 #+ (see bibliography).
116 # This just goes to show that much of what can be done in C
117 #+ can also be done in shell scripting.
118
119 }
120
121
122 #--------------- Now, let the show begin. ------------#
123 load_alpha # Load the array.
124 print_alpha # Print it out.
125 rotate # Rotate it 45 degrees counterclockwise.
126 #-----------------------------------------------------#
127
128 exit 0
129
130 # This is a rather contrived, not to mention inelegant simulation.
131
132 # Exercises:
133 # ---------
134 # 1) Rewrite the array loading and printing functions
135 # in a more intuitive and less kludgy fashion.
136 #
137 # 2) Figure out how the array rotation functions work.
138 # Hint: think about the implications of backwards-indexing an array.
139 #
140 # 3) Rewrite this script to handle a non-square array,
141 # such as a 6 X 4 one.
142 # Try to minimize "distortion" when the array is rotated.