2D Array

Hello, i want to add a function that determines whether in the component of the matrix A exists a magic square of dimension k >= 2, that is, the sums of the elements on the rows, columns, and diagonals of the square are equal; if so, the screen will display YES and the coordinates of the left-top corner and the size k of the square found, otherwise the message NO will be displayed. How can i implement thhis?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include <bits/stdc++.h>
#include <vector>

using namespace std;

 int n,m;
 int A[50][50];

 ifstream in("Lacusta.in");
 ofstream out("Lacusta.out", ios::app);

 
void read(){ //reading the data from file
if (!in.is_open())
    cout<<"Error opening file";

    in>>n>>m;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
          in >> A[i][j];
        }
    }

}

void display(){ //matrix data display
 cout<<endl;
 for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cout << A[i][j] << "\t";
        }
        cout<<endl;
    }

}



void addrow(){  //inserting a new row
    int temp[50][50];
    n++;
    cout<<endl;

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if(i==0){
            cout<<"["<<i<<"]["<<j<<"]=";
            cin>>temp[i][j];
            }
            else temp[i][j] = A[i-1][j];
        }
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
           A[i][j]=temp[i][j];
        }
    }

}

int main()
{
read();
    cout<< "[1] - Display the matrix "<<endl;
    cout<< "[2] - Input a new row"<<endl;
char a;
do{
    cout<<"\nInput option:  ";
    a = getch();
    switch(a){
        case '0': break;
        case '1':
        display(); 
        break;
        case '2':
        addrow(); 
        break;
        default: cout<< "\n \t~ INEXISTENT ~ \n"; getch();
        }
}while(a!='0');


return 0;
}


1
2
3
4
5
6
Lacusta.in Data :
4 5
3 4 5 7 9
6 6 3 4 4
6 3 3 9 6
6 5 3 8 2

Here's how geeksforgeeks would do it:
https://www.geeksforgeeks.org/check-given-matrix-is-magic-square-or-not/?ref=lbp
Next step might be to utilize vector<vector <int> > matrix2d; and double check that the number of rows matches number of columns at the top of the function (not sure why gfg doesn't bother with that).
Last edited on
Topic archived. No new replies allowed.