Question

In: Computer Science

C++ program, FOR THE MAIN WHEN YOU DECLARE FUNCTION PLEASE PUT CORNER SIZES IN A TABLE...

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.

Solutions

Expert Solution

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:


Related Solutions

c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an integer pointer p. 3. Let p pointing to the array a[ ]. 4. Use p (you have to use p) to put 0 into the first element of this array, 2 into the second element, 4 into the 3rd element, 6 into the 4th element, ... 198 into the 100th element of this array. 5. Use a (you have to use a) to display...
Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
assume that the following pseudocode is executed in a program module main declare x = 1...
assume that the following pseudocode is executed in a program module main declare x = 1 declare sum = 0 declare max = 5 while ( x < max) Display "x is ", x set x = x + 1 set sum = s + x end while end module after the execution of this program the final value of x is_____ and the final value of sum is_____
C++ Vectors. Create a program do the following in the program: 1. declare an vector without...
C++ Vectors. Create a program do the following in the program: 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector display the smallest, largest, and average of the numbers in the vector
Write a program in c# that declare a variable and store user name jjohn in. Then,...
Write a program in c# that declare a variable and store user name jjohn in. Then, write a statement that will make your program display at the screen the content of that variable, followed by " I would like to know your height in centimeter. Please enter it:". Then, write a statement that will store the value entered by the user, allowing decimal numbers ie some precision, a fraction part. Finally, write statements (more than one can be needed) so...
Can you solve this C program by using Function? Q1. Write a C program to ring...
Can you solve this C program by using Function? Q1. Write a C program to ring the computer bell at any number of times you specify. Use the system clock as a delay, you need to include the time header file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT