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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Variables
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education