Solved

# magic square 4*4

Posted on 2004-11-26
Medium Priority
588 Views
need help completing program with specs below
no global variables
2d arrays

use these functions declared
bool checkRows (int theSquare[][4], const int numRows, const int magicvalue);

function takes the square to check, # of rows to process and the magic value to check for. check all 4 rows for the magic value and return true of false depending on if all rows add upto the magic value. while checking rows, output which rows are bad if any are

same for columns and diagnols

#include<iostream>
#incldue<cmath>
using namespace std;

int main()
{
bool checkRows (int theSquare[][4], const int numRows, const int magicvalue);
bool checkColumns (int theSquare[][4], const int numRows, const int magicvalue);
bool checkDiagnols (int theSquare[][4], const int numRows, const int magicvalue);
checkUnique(); // will check to see if no # is used more than once
checkRange(); // will check to see range is 1to 16
int sum;

cout << "Please enter the 4 values for row 1, separated by spaces:";
cin >> num1 >> num2 >> num3 >> num4;
cout << "Please enter the 4 values for row 1, separated by spaces:";
cin >> num1 >> num2 >> num3 >> num4;
cout << "Please enter the 4 values for row 1, separated by spaces:";
cin >> num1 >> num2 >> num3 >> num4;
cout << "Please enter the 4 values for row 1, separated by spaces:";
cin >> num1 >> num2 >> num3 >> num4;
checkRows ();
checkColumns ();
checkDiagnols ();
checkUnique();
checkRange();

return 0;
}

bool checkRows (int theSquare[][4], const int numRows, const int magicvalue)
{ // check row sums
for (i = 0; i < 4; i++)
{
sum = 0;
for (j = 0; j < N; j++)
{
sum = sum + M[i][j];
}
}
}
bool checkColumns (int theSquare[][4], const int numRows, const int magicvalue)
{  // check column sums

for (j = 0; j < N; j++)
{
sum = 0;
for (i = 0; i < N; i++)
{
sum = sum + M[i][j];
}
}

}
bool checkDiagnols (int theSquare[][4], const int numRows, const int magicvalue)
{
sum = 0;
for (i = 0; i < N; i++)
{
sum = sum + M[i][i];
}

0
Question by:osnasima

LVL 4

Expert Comment

ID: 12680174
hi,
this seems like homework, doesn't it_ EE forbids writing homeworks, so I'll give you only few clues what to fix and how to continue:

First declare a square in which you'll store the numbers. You're storing them to the same variables over and over (forgeting the previous values)
Same is for checkXXX() functions. You sum whole square together. you should store the sub-sums for every row separately.

like this:

void checkRows (int square[][4], const int magicvalue) {
for (i=0; i<4; i++)
sum = 0 ;
for (j=0; j<4; j++)
sum += square[i][j] ;
if (sum == magicvalue)
// line i sums to magic value
else
// line i doesn't sum to magicvalue
}

S.
0

LVL 1

Expert Comment

ID: 12685008
In each function add following lines at end

if(sum ==magicvalue)
return true;
else
return false;

and call the function like this:

bool state
state = ( yourSquareArr , 4 , yourMagicvalue)

if (state == true)
cout<<"Magic Number"
else
cout<< "Not a magic number"
0

LVL 11

Expert Comment

ID: 12687480
Also, in C++, it is not valid to declare (or define) one function inside of another. Thus the beginning of main:

int main()
{
bool checkRows (int theSquare[][4], const int numRows, const int magicvalue);
bool checkColumns (int theSquare[][4], const int numRows, const int magicvalue);
bool checkDiagnols (int theSquare[][4], const int numRows, const int magicvalue);

should not compile. I think you want to declare those functions before main and then define them after main (depends on your professor's preferred style).

Comment on silaupura's code: I am pretty sure it fails to match your specification (doesn't display any rows that fail to match; you can modify the if statement to fix this).

-bcl
0

LVL 1

Expert Comment

ID: 12703996

Now try your code again and tell us what happened osnasima
0

Author Comment

ID: 12711323
ok didnt work...how do i get it to display rows that fail to match
0

LVL 1

Expert Comment

ID: 12713506
send your complete code then, complete here mean only the functions which are errorneous
0

LVL 4

Accepted Solution

skypalae earned 700 total points
ID: 12713607
osnasima,

use these functions declared:
bool checkRows (int theSquare[][4], const int numRows, const int magicvalue);

this means that you have to implement the function that checks the rows. no other functions are needed. all your checkColumns, checkDiagonals etc.. can be thrown away. You may write a testing code (the main() function) but it is not necessary.

function takes the square to check, # of rows to process and the magic value to check for. check all 4 rows for the magic value and
return true of false depending on if all rows add upto the magic value. while checking rows, output which rows are bad if any are

this describes the input fields of the function. I dont get a point why numRows is present (because you allways have to check 4 rows, no more, no less), but if it is in the assignment, you should keep it

I've written very good base for this function in my first comment. Read the code carefully and copy any idea you like. You only need to insert a boolean return value. It will be set to true in initialization and turned to false when the line doesn't match. From the declaration I assume that you're using C++. That means that you'll use cout to print out the wrong line number.

S.

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress
###### Suggested Courses
Course of the Month15 days, 22 hours left to enroll