magic square 4*4

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];
        }
       
osnasimaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

skypalaeCommented:
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
silaupuraCommented:
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
bcladdCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

silaupuraCommented:
exactly, bcladd is correct.

Now try your code again and tell us what happened osnasima
0
osnasimaAuthor Commented:
ok didnt work...how do i get it to display rows that fail to match
0
silaupuraCommented:
send your complete code then, complete here mean only the functions which are errorneous
0
skypalaeCommented:
osnasima,
please read your specification in detail and you'll find that lot of your code is completely useless.

    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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.