Question

C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...

C++

Lab 11 – Is This Box a Magic Box?

Objectives:

  • Define a two dimensional array

  • Understand how to traverse a two dimensional array

  • Code and run a program that processes a two dimensional array

Instructions:

A magic square is a matrix (two dimensional arrays) in which the sum of each row, sum of each column, sum of the main diagonal, and sum of the reverse diagonal are all the same value.

You are to code a program to determine if a given two-dimensional array (that will be read in from a file) is a magic square or not. The functions you will need to code are as follows:

Code a function/method for each of the following:

  • Read data into the matrix

  • Print the matrix on the screen

  • Sum a row given the index for that row

  • Sum a column given the index for that column

  • Sum the main diagonal

  • Sum the reverse diagonal

  • Determine if the two dimensional array is a magic square

The main method will read the size of the array outside of the while loop. Then inside the while loop it will call all the methods given above to read the data into the matrix, print the matrix, print all the row sums, print all the column sums, and print both diagonals. Then the program will determine if the matrix is a magic square and print an appropriate message. The next size should be read in at the end of the while loop. The main will continue with the next matrix in the data file, until a size of -1 is reached. See the Additional Notes on Two Dimension Arrays for more help with this assignment. Do not use dynamic arrays or pointers in this solution. Follow the solution given in the video and in Additional Notes on Two Dimension Arrays.

Sample Output:

The first part of the output should look similar to this:

=========

= Square 1=

=========

8 1 6

3 5 7

4 9 2

The sum of row 0 is 15

The sum of row 1 is 15

The sum of row 2 is 15

The sum of column 0 is 15

The sum of column 1 is 15

The sum of column 2 is 15

The main diagonal is 15

The other diagonal is 15

This matrix is a magic box!


Data File:

Paste everything below this block of text into a notepad file. Your input file must appear exactly as it below and include all of this data in the same file.

3

8 1 6

3 5 7

4 9 2

7

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

4

48 9 6 39

27 18 21 36

15 30 33 24

12 45 42 3

3

6 2 7

1 5 3

2 9 4

4

3 16 2 13

6 9 7 12

10 5 11 8

15 4 14 1

5

17 24 15 8 1

23 5 16 14 7

4 6 22 13 20

10 12 3 21 19

11 18 9 2 25

7

30 39 48 1 10 28 19

38 47 7 9 18 29 27

46 6 8 17 26 37 35

5 14 16 25 34 45 36

13 15 24 33 42 4 44

21 23 32 41 43 12 3

22 31 40 49 2 20 11

-1

Run:

Run the program to see if your results are correct.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOLUTION;-

-;GIVEN DATA;-

*include <iostream>
*include <fstream>

using namespace std;

void readData(int** mat, int size, std::fstream &myfile) {
int n;
for(int i=0; i<size; i++) {
for(int j=0; j<size; j++) {
// read from file
myfile >> n;
  
// assign to matrix
mat[i][j] = n;
}
}
}

void printMatrix(int** mat, int size) {
for(int i=0; i<size; i++) {
for(int j=0; j<size; j++) {
  
cout << mat[i][j] << " ";
}
cout << endl;
}
cout << endl;
}

int rowSum(int** mat, int row, int size) {
int sum = 0;
for(int j=0; j<size; j++) {
sum += mat[row][j];
}
return sum;
}

int colSum(int** mat, int col, int size) {
int sum = 0;
for(int j=0; j<size; j++) {
sum += mat[j][col];
}
return sum;
}

int mainDiaSum(int** mat, int size) {
int sum = 0;
for(int j=0; j<size; j++) {
sum += mat[j][j];
}
return sum;
}

int revDiaSum(int** mat, int size) {
int sum = 0;
for(int j=size-1; j>=0; j--) {
sum += mat[j][j];
}
return sum;
}

int main()
{
std::fstream myfile("data.txt", std::ios_base::in);

int size, a;
int squareNo = 0;
myfile >> size;
  
while (size > 0)
{
squareNo++;
  
// create matrix
int** mat = new int*[size];
for(int i = 0; i < size; ++i) {
mat[i] = new int[size];
}
  
int sum;
  
readData(mat, size, myfile);
cout << "========================\n";
cout << "====== Square " << squareNo << " ======" << endl;
cout << "========================\n";
  
printMatrix(mat, size);
  
bool isMagic = true;
  
// assigning row 0 sum as magic sum..
// if matrix is not magic, we will set above boolean as false
int magicSum = rowSum(mat, 0, size);
  
for(int i=0; i<size; i++) {
sum = rowSum(mat, i, size);
if(sum != magicSum)
isMagic = false;
cout << "The sum of row " << i << " is " << sum << endl;
}
cout << endl;
  
for(int j=0; j<size; j++) {
sum = colSum(mat, j, size);
if(sum != magicSum)
isMagic = false;
cout << "The sum of col " << j << " is " << sum << endl;
}
cout << endl;
  
sum = mainDiaSum(mat, size);
if(sum != magicSum)
isMagic = false;
cout << "The sum of main diagonal is " << sum << endl;
  
sum = mainDiaSum(mat, size);
if(sum != magicSum)
isMagic = false;
cout << "The sum of other diagonal is " << sum << endl;
  
if(isMagic)
cout << "The matrix is a magic Box" << endl << endl;
else
cout << "The matrix is not a magic Box" << endl << endl;
  
for(int i = 0; i < size; ++i) {
delete [] mat[i];
}
delete [] mat;


// get next matrix size
myfile >> size;
}

return 0;
}


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

Sample output:

Sh-4.2$ main

=======================

=======Square 1=========

=======================

8 1 6

3 5 7

4 9 2

The sum of row 0 is 15

The sum of row 1 is 15

The sum of row 2 is 15

The sum of column 0 is 15

The sum of column 1 is 15

The sum of column 2 is 15

The sum of main diagonal is 15

The sum of other diagonal is 15

This matrix is a magic box

=====================

===== Square 2 =======

====================

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

The sum of row 0 is 175

The sum of row 1 is 175

The sum of row 2 is 175

The sum of row 3 is 175

The sum of row 4 is 175

The sum of row 5 is 175

The sum of row 6 is 175

''The sum of column 0 is 175''

The data .txt in same folder....

Add a comment
Know the answer?
Add Answer to:
C++ Lab 11 – Is This Box a Magic Box? Objectives: Define a two dimensional array Understand h...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that h...

    9. Lo Shu Magic Square The Lo Shu Magic Square is a grid with three rows and three columns that has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown in the following figure · 15 4 9 2-15 3 5 7-15 81 615 15 15 15 15 Write a program that simulates a magic square using...

  • Use basic java for this after importing PrintWriter object You will make a simple Magic Square...

    Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...

  • I am using C++ Write a program to determine if a gird follows the rules to...

    I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...

  • Define a two-dimensional int array which has 5 rows and 3 columns. The elements in array...

    Define a two-dimensional int array which has 5 rows and 3 columns. The elements in array is randomly initialized (using Math.random()). Write a method to find the maximum value in this two dimensional array; Write a method to find the average value in the array; Write a method to count how many elements are smaller than average; Write a method to copy contents in a two-dimensional array to a one-dimensional array. The method will return this one-dimensional array. Method is...

  • please use java language please used ArrayList The Lo Shu Magic Square is a grid with...

    please use java language please used ArrayList The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in Figure 7-31. The • The sum of each row, each column, and each diagonal all add up to the same number 20. Lo Shu Magic Square Lo Shu Magic Square has the following properties: • The grid contains the numbers 1 through 9 exactly. This is shown in Figure 7-32. In a program you can simulate a...

  • I need this written in C++. Magic Squares. An n x n matrix that is filled...

    I need this written in C++. Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square,...

  • C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is...

    C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square, place k+1 into the square...

  • Create a square array and fill out as shown below. Use nested loop for this step....

    Create a square array and fill out as shown below. Use nested loop for this step. 1 3 5 7 9 2 4 6 8 10 3 5 7 9 11 4 6 8 10 12 5 7 9 11 13 Then create two 1D arrays to with row and column sum of the items. You have then 15 25 35 45 55 for the column sum, and 25 30 35 40 45 for the row sum. Print row and...

  • 8. Ann x n matrix that is filled with the numbers 1,2,3, ....n2 is a magic...

    8. Ann x n matrix that is filled with the numbers 1,2,3, ....n2 is a magic square if the sum of the elements in each row, in each column, and in the two main diagonals is the same value. For example, 16 3 213 5 10 11 8 9 6 7 12 4 15 14 1 Write the program that reads in 16 values from the keyboard and tests whether they form a magic square when put into a 4...

  • Create a square array and fill out as shown below. Use a nested loop for this...

    Create a square array and fill out as shown below. Use a nested loop for this step. 1 3 5 7 9 2 4 6 8 10 3 5 7 9 11 4 6 8 10 12 5 7 9 11 13 Then create two 1D arrays to with row and column sum of the items. You have then 15 25 35 45 55 for the column sum, and 25 30 35 40 45 for the row sum. Print row...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT