complete magic Square #include using namespace std; /*int f( int x, int y, int* p, int* q ) { if ( y == 0 ) { p = 0, q = 0; return 404; // status: Error 404 } *p = x * y; // product *q = x / y; // quotient return 200; // status: OK 200
C++ complete magic Square
#include <iostream>
using namespace std;
/*int f( int x, int y, int* p, int* q )
{
if ( y == 0 )
{
p = 0, q = 0;
return 404; // status: Error 404
}
*p = x * y; // product
*q = x / y; // quotient
return 200; // status: OK 200
}
int main()
{
int p, q;
int status = f(10, 2, &p, &q);
if ( status == 404 )
{
cout << "[ERR] / by zero!" << endl;
return 0;
}
cout << p << endl;
cout << q << endl;
return 0;
}
*/
/*struct F_Return
{
int p;
int q;
int status;
};
F_Return f( int x, int y )
{
F_Return r;
if ( y == 0 )
{
r.p = 0, r.q = 0;
r.status = 404;
return r;
}
r.p = x * y;
r.q = x / y;
r.status = 200;
return r;
}
int main()
{
F_Return r = f(10, 0);
if ( r.status == 404 )
{
cout << "[ERR] / by zero" << endl;
return 0;
}
cout << r.p << endl;
cout << r.q << endl;
return 0;
}*/
int sumByRow(int *m, int nrow, int ncol, int row)
{
int total = 0;
for ( int j = 0; j < ncol; j++ )
{
total += m[row * ncol + j]; //m[row][j];
}
return total;
}
/*
(i,j) -> y = i * ncol + j
(0,0) -> 0 = 0 * 2 + 0
(0,1) -> 1 = 0 * 2 + 1
(1,0) -> 2 = 1 * 2 + 0
(1,1) -> 3 = 1 * 2 + 1
(2,0) -> 4 = 2 * 2 + 0
(2,1) -> 5 = 2 * 2 + 1
*/
/*
3x3
(0,0) -> 0 * 3 + 0 = 0
(1,1) -> 1 * 3 + 1 = 4
(2,2) -> 2 * 3 + 2 = 8
0 1 2
3 4 5
6 7 8
4x4
(0,0) -> 0 * 4 + 0 = 0
(1,1) -> 1 * 4 + 1 = 5
(2,2) -> 2 * 4 + 2 = 10
(3,3) -> 3 * 4 + 3 = 15
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
*/
//
bool isMagicSquare(m, 5) // Any size!!!
{
// TODO:
}
int main()
{
int m[3][3] = {
{ 2, 4, 6 },
{ 6, 7, 9 },
{ 9, 1, 2 }
};
cout << sumByRow((int *) m, 3, 3, 1) << endl;
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps