Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A magic square is a matrix of integers where the sum of each row, the sum of each column, and the sum of each main

A magic square is a matrix of integers where the sum of each row, the sum of each column, and the sum of each main diagonal are all exactly the same number. Well call this number the magic number. Magic squares can be of any size.

Using NumPy to contain the matrix, write a Python program that repeatedly:

1. Reads the next matrix from a data file. 2. Determines if the matrix is a magic square. If it is, it prints the magic number; if not, states that the matrix is not a magic square. 3. Repeats until no matrices remain in the file. Use a fruitful function called magic() to determine whether the matrix is a magic square or not and returns the value of the magic number if it is or returns -1 if the matrix is not a magic square.

The main driver should handle the input of the matrices and calling magic(). Since we have not covered file input yet, I suggest you use the code in ReadSquares.py to handle reading the square data. Although it is a standalone program right now (run it!), you can extract the important parts of the code and incorporate them into your solution to this assignment. Run your program several times using different inputs sufficient to demonstrate that your program meets all the assignment requirements. One of your inputs must be the file squaredata.txt. Capture a screen shot of each run and paste them into an MS Word document. Place a caption above each image.

squaredata.txt contains the data below:

7

2 2 2 2

2 7 6 9 5 1 4 3 8

16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1

17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9

30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20

18 26 2 9 11 4 6 13 21 23 15 17 25 1 8 22 3 10 12 19 7 14 16 24 5

25 32 9 16 18 11 13 20 27 29 22 24 31 8 15 28 10 17 19 26 14 21 23 30 12

256 2 3 253 252 6 7 249 248 10 11 245 244 14 15 241 17 239 238 20 21 235 234 24 25 231 230 28 29 227 226 32 33 223 222 36 37 219 218 40 41 215 214 44 45 211 210 48 208 50 51 205 204 54 55 201 200 58 59 197 196 62 63 193 192 66 67 189 188 70 71 185 184 74 75 181 180 78 79 177 81 175 174 84 85 171 170 88 89 167 166 92 93 163 162 96 97 159 158 100 101 155 154 104 105 151 150 108 109 147 146 112 144 114 115 141 140 118 119 137 136 122 123 133 132 126 127 129 128 130 131 125 124 134 135 121 120 138 139 117 116 142 143 113 145 111 110 148 149 107 106 152 153 103 102 156 157 99 98 160 161 95 94 164 165 91 90 168 169 87 86 172 173 83 82 176 80 178 179 77 76 182 183 73 72 186 187 69 68 190 191 65 64 194 195 61 60 198 199 57 56 202 203 53 52 206 207 49 209 47 46 212 213 43 42 216 217 39 38 220 221 35 34 224 225 31 30 228 229 27 26 232 233 23 22 236 237 19 18 240 16 242 243 13 12 246 247 9 8 250 251 5 4 254 255 1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

More Books

Students also viewed these Databases questions

Question

Have roles been defined and assigned?

Answered: 1 week ago

Question

How do Data Types perform data validation?

Answered: 1 week ago

Question

How does Referential Integrity work?

Answered: 1 week ago