CSCI-15 Lab 2, due 2/15/16

advertisement
CSCI-15 Lab 2, due 2/15/16
Please work in groups of exactly two, unless I clear it with you directly.
Your job is to write a function makeSpiral() that takes a two-dimensional array and the
number of rows and columns of that array to fill. The function will fill the rows-by-columns
corner of the array with the integers from 1 to (rows * columns) in a counter-clockwise spiral
pattern. The pattern starts at a[0][0] in the upper left, and goes down, then right, then up,
then left filling the elements with values; and then moves in one row and column on each side,
continuing until you run out of rows and columns. For example, with a 5 by 6 array and filling a
4 by 4 corner, the result will be something like this:
row/column
0
1
2
3
4
0
1
2
3
4
1
12
13
14
5
2
11
16
15
6
3
10
9
8
7
4
5
The top and left sides of this table just show the column and row numbers. The rest of the array
(outside the corner passed into the makeSpiral() function) will be left unchanged by the
function.
Next, write a function printSpiral() that takes an output file handle, a 2-d array and the
number of rows and columns to print, and prints the spiral in the array in a reasonable format.
Use setw() with the size (number of digits) of the value of ( rows * columns ) + 1 to set the
size of each value to print. Do not skip lines, and do not print the row or column numbers, just
the spiral. Print output directly to a text file.
Then write a program that declares a 15 by 20 array and opens a text file for output. The
program will then loop, filling the array completely with zeroes (use a function for this task),
calling makeSpiral() with the various values for the size of the corner to fill, and then
calling printSpiral() to print the spiral. Run this loop to test all of the spiral corner sizes
you need.
How can you pass in multiple corner sizes to the various functions without prompting for the
values (or using multiple separate function calls)? Solve this problem in the lab, too. It is not
hard.
In a reduced size fixed-width font, this is what a 15x20 spiral looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
66
67
68
69
70
71
72
73
74
75
76
77
78
79
16
65
124
125
126
127
128
129
130
131
132
133
134
135
80
17
64
123
174
175
176
177
178
179
180
181
182
183
136
81
18
63
122
173
216
217
218
219
220
221
222
223
184
137
82
19
62
121
172
215
250
251
252
253
254
255
224
185
138
83
20
61
120
171
214
249
276
277
278
279
256
225
186
139
84
21
60
119
170
213
248
275
294
295
280
257
226
187
140
85
22
59
118
169
212
247
274
293
296
281
258
227
188
141
86
23
58
117
168
211
246
273
292
297
282
259
228
189
142
87
24
57
116
167
210
245
272
291
298
283
260
229
190
143
88
25
56
115
166
209
244
271
290
299
284
261
230
191
144
89
26
55
114
165
208
243
270
289
300
285
262
231
192
145
90
27
54
113
164
207
242
269
288
287
286
263
232
193
146
91
28
53
112
163
206
241
268
267
266
265
264
233
194
147
92
29
52
111
162
205
240
239
238
237
236
235
234
195
148
93
30
51
110
161
204
203
202
201
200
199
198
197
196
149
94
31
50
109
160
159
158
157
156
155
154
153
152
151
150
95
32
49
108
107
106
105
104
103
102
101
100
99
98
97
96
33
48
47
46
45
44
43
42
41
40
39
38
37
36
35
34
Send me tests with at least the following corner sizes, plus a couple more of your own choice:
1 by 1, 1 by 2, 2 by 1, 2 by 2, 3 by 3, 4 by 4, 5 by 5, 4 by 7, 7 by 4, 4 by 8, 6 by 4, 15 by 20
Designing this completely before trying to code any of it will save you several hours of effort.
You may write little test programs (stubs) to test specific ideas before you complete the design.
Download