Question: CS 135 Computer Science I Fall 2018 Chapter 8 Programming Assignment B (pa08b) 100 points 3001 Due by 2:00pm 2018-11-29 3002 Due by 2:00pm 2018-11-30

CS 135 Computer Science I Fall 2018 Chapter 8 Programming Assignment B (pa08b) 100 points 3001 Due by 2:00pm 2018-11-29 3002 Due by 2:00pm 2018-11-30 4001 Due by 9:00am 2018-12-01 Problem description Magic Squares are made up of consecutive numbers that have their rows and columns add to the same value. A 1x1 Magic Square is trivial, and a 2x2 Magic Square is not possible. That leaves Magic Squares that are 3x3 to NxN, where N can be any odd number greater than 3. Odd-numbered Magic Squares (3x3, 5x5, 7x7, etc.) can be generated using a very simple algorithm, described below. For this assignment, you will write a program that generates an odd-numbered Magic Square using the algorithm described below. 1 We start by placing a 1 in the top left square. Since this is a 3x3 Magic Square, we will place 3 numbers at a time in a pattern. (A 5x5 square would require placing 5 numbers at a time.) The pattern will be to place each consecutive number down and to the right of the previous number (if possible). If not possible, wrap around to the appropriate square that is still within the 3x3 Magic Square. 1 For example, placing the first three consecutive numbers down and to the right forms a diagonal. Since we are dealing with a diagonal, there is no difficulty in placing the first pattern of three numbers. 2 3 1 At this point, we have placed the first three numbers in the pattern. From here, we simply move up one row and place the next three numbers in the pattern, down and to the right as before. The number 4 is placed on the row above the previous number. The number 5 should be placed down and to the right of 4, but this would be outside the bounds of the Magic Square, so we wrap around and place it in the bottom left corner. 2 4 5 3 1 6 The number 6 should be placed down and to the right, but this would be outside the bounds of the Magic Square, so we wrap around and place it in the middle of the top row. As before, if we move up one row from the last number, we can complete the Magic Square by the down and to the right (wrap around) sequence that we have used. 2 4 5 7 3 1 6 8 If you look at the completed 3x3 Magic Square to the left, you will see that all the rows add to 15. You should also find that all the columns as well as all the diagonals add to 15 as well. This value is known as the Magic Sum, calculated by the formula = (% + 1)/2. 9 2 4 5 7 3 You can apply the technique used in the 3x3 Magic Square to create any odd-numbered Magic Square. 11/15/18 7:22 PM 2 Command line input In C++ it is possible to accept command line arguments. Command-line arguments are given after the name of a program in command-line operating systems like Linux and are passed in to the program from the operating system. To use command line arguments in your program, you must first understand the full declaration of the main function, which until now has accepted no arguments. In fact, main can accept two arguments: one argument is number of command line arguments, and the other argument is a full list of all of the command line arguments. The full header of main looks like this: int main(int argc, char *argv[]) The integer, argc is the ARGument Count (hence argc). It is the number of arguments passed into the program from the command line, including the name of the program. The array of character pointers is the listing of all the arguments. argv[0] is the name of the program, or an empty string if the name is not available. After that, every element number less than argc is a command line argument. You can use each argv element just like a cstring or use argv as a two-dimensional array. argv[argc] is a null pointer. How could this be used? Almost any program that wants its parameters to be set when it is executed would use this. Lets examine one such program: #include using namespace std; int main(int argc, char *argv[]) { // process command line arguments cout << "The name of the program is " << argv[0] << endl; if (argc == 1) { cout << "Usage: Provide some command line arguments! "; } else { cout << "The command line arguments are: "; for (int i = 1; i < argc; ++i) { int value = std::atoi(argv[i]); cout << argv[i]; if (value != 0) { cout << " (value: " << value << ") "; } cout << endl; } } return 0; } This demonstration program is fairly simple. It first checks that the user added a second argument. The program checks to see if the argument is valid by trying to convert it using std::atoi. (See cppreference.com for how to use this function and the header required to use it.) 11/15/18 7:22 PM 3 Your program gets the size of the Magic Square from the command line only. Do not interactively prompt the user for the size. The size must be odd and in the range [3..99] (e.g., the set of values { 3, 5, 7, , 99 }). If (1) no command line arguments are passed to the program, (2) the command line argument is not an integer, or (3) the value of the command line argument is not in the set of valid values, your program should display a usage message to the user and terminate. You may use the usage message shown in the sample interaction, or you may create your own. The goal is to educate the user, briefly. Your programs output should consist of a blank line, followed by the neatly-formatted Magic Square, followed by a blank line, followed by the Magic Sum, followed by a blank line. Use a Boolean function to validate that the sum of each row and each column is equal to the Magic Sum. Your main function returns 0 on success and non-zero if an error occurs. (You may optionally implement unique return codes to signify different error states, e.g., no command line argument, not an integer, value out of range, etc.). Hints: Declare one array large enough for the largest Magic Square. You can use as much of the array as you need to process smaller Magic Squares. Use functions to implement abstractions, like moving up, right, down, or left. Sample interaction $ ./a.out 5 1 8 15 17 24 25 2 9 11 18 19 21 3 10 12 13 20 22 4 6 7 14 16 23 5 Magic sum: 65 $ ./a.out Usage: ./a.out int Specify the size of the magic square as a command line argument. The size must be an ODD integer in range [3..99]. Example: ./a.out 5 $ ./a.out hello Usage: ./a.out int Specify the size of the magic square as a command line argument. The size must be an ODD integer in range [3..99]. Example: ./a.out 5 $ ./a.out 4 Usage: ./a.out int Specify the size of the magic square as a command line argument. The size must be an ODD integer in range [3..99]. Example: ./a.out 5 11/15/18 7:22 PM 4 Submission Name your solution pa08b.cpp and use the turnin command to submit.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!