In: Computer Science
C++ program, FOR THE MAIN WHEN YOU DECLARE FUNCTION PLEASE PUT CORNER SIZES IN A TABLE AND A LOOP SO YOU DON'T HAVE TO DECLARE FUNCTION MULTIPLE TIME
You may work in groups of two.
Your job is to write a function makeSpiral() that takes a two-dimensional array and the number of rows and columns 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 5 0 1 12 11 10 1 2 13 16 9 2 3 14 15 8 3 4 5 6 7 4 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, reading them from a file, or using multiple separate function calls (e.g., outside a loop)? Solve this problem in this lab, too. It is not hard.
In a reduced size fixed-width font, this is what a 15x20 spiral looks like:
1 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48
2 67 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110
109 108 47
3 68 125 174 173 172 171 170 169 168 167 166 165 164 163 162 161
160 107 46
4 69 126 175 216 215 214 213 212 211 210 209 208 207 206 205 204
159 106 45
5 70 127 176 217 250 249 248 247 246 245 244 243 242 241 240 203
158 105 44
6 71 128 177 218 251 276 275 274 273 272 271 270 269 268 239 202
157 104 43
7 72 129 178 219 252 277 294 293 292 291 290 289 288 267 238 201
156 103 42
8 73 130 179 220 253 278 295 305 304 303 302 301 287 266 237 200
155 102 41
9 74 131 180 221 254 279 280 281 282 283 284 285 286 265 236 199
154 101 40
10 75 132 181 222 255 256 257 258 259 260 261 262 263 264 235 198
153 100 39
11 76 133 182 223 224 225 226 227 228 229 230 231 232 233 234 197
152 99 38
12 77 134 183 184 185 186 187 188 189 190 191 192 193 194 195 196
151 98 37
13 78 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
150 97 36
14 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 35
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
Send me tests with at least the following corner sizes, plus several 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.
Working code implemented in C++ and appropriate comments provided for better understanding.
Source Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void makeSpiral(int rows, int columns, int numArray[][20])
{
int num = 1;
int startingRow = 0;
int endingRow = rows;
int startingColumn = 0;
int endingColumn = columns;
while(num <= rows*columns)
{
for (int i = startingRow; i < endingRow; i++)
{
numArray[i][startingColumn] = num++;
}
startingColumn++;
for (int i = startingColumn; i < endingColumn; i++)
{
numArray[endingRow - 1][i] = num++;
}
endingRow--;
for(int i = (endingRow - 1); i >= startingRow; i--)
{ if(num > (rows*columns))
return;
numArray[i][endingColumn - 1] = num++;
}
endingColumn--;
for(int i = (endingColumn - 1); i >= startingColumn;
i--)
{
if(num > (rows*columns))
return;
numArray[startingRow][i] = num++;
}
startingRow++;
}
}
void printSpiral(ofstream &outputFile, int numArray[][20],
int rowsToPrint,
int columnsToPrint)
{
for(int i = 0; i < 15; i++)
{
for(int j = 0; j < 20; j++)
{
outputFile << setw(4) << numArray[i][j] ;
}
outputFile << endl;
}
}
int main()
{
const int MAXROW = 15;
const int MAXCOLUMN = 20;
int numArray[MAXROW][MAXCOLUMN] = {{0}};
ofstream outputFile("outputFile-15X20.txt");
if(!outputFile)
{
cout << "output file not found";
return -1;
}
int rows = 15;
int columns = 20;
makeSpiral(rows, columns, numArray);
printSpiral(outputFile, numArray, MAXROW, MAXCOLUMN);
outputFile.close();
return 0;
}
Sample Output Screenshots: