?
Solved

magic square 4*4

Posted on 2004-11-26
7
Medium Priority
?
588 Views
Last Modified: 2008-01-09
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
Comment
Question by:osnasima
7 Comments
 
LVL 4

Expert Comment

by:skypalae
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

by:silaupura
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

by:bcladd
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:silaupura
ID: 12703996
exactly, bcladd is correct.

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

Author Comment

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

Expert Comment

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

Accepted Solution

by:
skypalae earned 700 total points
ID: 12713607
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question