Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ PROGRAMMING Objectives : Define a two dimensional array Understand how to tr

ID: 3858724 • Letter: C

Question

C++ PROGRAMMING

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.

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.

Explanation / Answer

Given below is the code in C++ for the question . Also the output is shown. Please rate the answer if it helped. Thank you.

#include <iostream>

#include <fstream>

#include <cstdlib>

using namespace std;

void read_data(ifstream &infile, int size, int matrix[][10]);

void print_matrix(int size, int matrix[][10]);

int sum_row(int row, int size, int matrix[][10]);

int sum_column(int row, int size, int matrix[][10]);

int sum_main_diagonal(int size, int matrix[][10]);

int sum_reverse_diagonal(int size, int matrix[][10]);

int main()

{

  

string filename;

ifstream infile;

  

cout << "Enter filename containing input data: ";

cin >> filename;

  

infile.open(filename.c_str());

if(!infile.is_open())

{

cout << "Error opening input file " << filename << endl;

exit(1);

}

  

int n = 1;

int size;

while(infile >> size)

{

if(size == -1) //end of file

break;

  

int matrix[10][10]; //maximum 10 rows and 10 columns

read_data(infile, size, matrix);

cout << "==============" << endl;

cout << "== Square " << n << " ==" << endl;

cout << "==============" << endl;

print_matrix(size, matrix);

bool isMagic = true;

int SUM = sum_row(0, size, matrix); //get sum of hte 1st row, all other sums should be equal to this

int s;

for(int row = 0; isMagic && row < size; row++)

{

s = sum_row(row, size, matrix);

cout << "The sum of row " << row << " is " << s << endl;

if(s != SUM) //if this row's sum is not the same as SUM

{

isMagic = false;

}

}

  

for(int col = 0; isMagic && col < size; col++)

{

s = sum_column(col, size, matrix);

cout << "The sum of column " << col << " is " << s << endl;

if(s != SUM) //if this col's sum is not the same as SUM

{

isMagic = false;

}

}

if(isMagic)

{

s = sum_main_diagonal(size, matrix);

cout << "The sum of main diagonal " << " is " << s << endl;

if(s != SUM)

isMagic = false;

}

  

if(isMagic)

{

s = sum_reverse_diagonal(size, matrix);

cout << "The sum of reverse diagonal " << " is " << s << endl;

if(s != SUM)

isMagic = false;

}

  

if(isMagic)

cout << "This matrix is a magic box!" << endl;

else

cout << "This matrix is NOT a magic box!" << endl;

n++;

}

infile.close();

return 0;

}

void read_data(ifstream &infile, int size, int matrix[10][10])

{

for(int i = 0 ; i < size; i++)

for(int j = 0; j < size; j++)

infile >> matrix[i][j];

}

void print_matrix(int size, int matrix[10][10])

{

for(int i = 0 ; i < size; i++)

{

cout << endl;

for(int j = 0; j < size; j++)

cout << matrix[i][j] << " ";

}

cout << endl;

}

int sum_row(int row, int size, int matrix[10][10])

{

int total = 0;

for(int col = 0; col < size; col++)

total += matrix[row][col];

  

return total;

}

int sum_column(int col, int size, int matrix[10][10])

{

int total = 0;

for(int row = 0; row < size; row++)

total += matrix[row][col];

  

return total;

}

int sum_main_diagonal(int size, int matrix[10][10])

{

int total = 0;

for(int i = 0; i < size; i++)

total += matrix[i][i];

return total;

}

int sum_reverse_diagonal(int size, int matrix[10][10])

{

int total = 0;

for(int row = 0; row < size; row++)

total += matrix[row][size - 1 - row];

return total;

}

output

Enter filename containing input data: magic.txt
==============
== 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 reverse 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 sum of column 1 is 175
The sum of column 2 is 175
The sum of column 3 is 175
The sum of column 4 is 175
The sum of column 5 is 175
The sum of column 6 is 175
The sum of main diagonal is 175
The sum of reverse diagonal is 175
This matrix is a magic box!
==============
== Square 3 ==
==============

48   9   6   39
27   18   21   36
15   30   33   24
12   45   42   3
The sum of row 0 is 102
The sum of row 1 is 102
The sum of row 2 is 102
The sum of row 3 is 102
The sum of column 0 is 102
The sum of column 1 is 102
The sum of column 2 is 102
The sum of column 3 is 102
The sum of main diagonal is 102
The sum of reverse diagonal is 102
This matrix is a magic box!
==============
== Square 4 ==
==============

6   2   7
1   5   3
2   9   4
The sum of row 0 is 15
The sum of row 1 is 9
This matrix is NOT a magic box!
==============
== Square 5 ==
==============

3   16   2   13
6   9   7   12
10   5   11   8
15   4   14   1
The sum of row 0 is 34
The sum of row 1 is 34
The sum of row 2 is 34
The sum of row 3 is 34
The sum of column 0 is 34
The sum of column 1 is 34
The sum of column 2 is 34
The sum of column 3 is 34
The sum of main diagonal is 24
This matrix is NOT a magic box!
==============
== Square 6 ==
==============

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
The sum of row 0 is 65
The sum of row 1 is 65
The sum of row 2 is 65
The sum of row 3 is 65
The sum of row 4 is 65
The sum of column 0 is 65
The sum of column 1 is 65
The sum of column 2 is 65
The sum of column 3 is 58
This matrix is NOT a magic box!
==============
== Square 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
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 sum of column 1 is 175
The sum of column 2 is 175
The sum of column 3 is 175
The sum of column 4 is 175
The sum of column 5 is 175
The sum of column 6 is 175
The sum of main diagonal is 175
The sum of reverse diagonal is 168
This matrix is NOT a magic box!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote