I have finished all the functions except one. I need help with writing a boolean function to determine if the magic square is a magic square. I don't completely understand boolean functions so any help would be appreciated.

Thank you

#include<iostream>
using namespace std;
#include<fstream>
{
ifstream infile;
infile.open("magic.dat");
infile >>n;
for (int i =0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
infile >>square[i][j];
}
}
}

//Print the 2 dimensional arrray
void print2D(int n, int square)
{

for (int i =0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout <<square[i][j]<<" ";
}
cout <<endl;
}
}

int main()
{
int n;
int square;
print2D(n,square);
system("pause");
return 0;
}

There isn't too much to a boolean function. They return true or false based on whatever. Here's an example of a boolean function that returns true if both numbers passed to it are the same, false if they are different. You'll need to change it to accept the parameters you …

All 4 Replies

There isn't too much to a boolean function. They return true or false based on whatever. Here's an example of a boolean function that returns true if both numbers passed to it are the same, false if they are different. You'll need to change it to accept the parameters you want for the magic square, presumably the same parameters as your print2D function. Check whether it's a magic square inside the function and return false or true based on that.

#include <iostream>
using namespace std;

bool NumbersAreTheSame (int num1, int num2)
{
if (num1 == num2)
return true;
else
return false;
}

int main ()
{
int num1, num2;
cout << "Enter an integer : ";
cin >> num1;
cout << "Enter another integer : ";
cin >> num2;
bool numsAreTheSame = NumbersAreTheSame (num1, num2);

if (numsAreTheSame)
cout << num1 << " and " << num2 << " are the same." << endl;
else
cout << num1 << " and " << num2 << " are not the same." << endl;

// pause program.  Enter return key to end.
cin.get ();
cin.get ();
return 0;
}

I have added the boolean function but cannot get it to print out whether or not its true or false it still just prints out the whats in the magic square.

#include<iostream>
using namespace std;
#include<fstream>
{
ifstream infile;
infile.open("magic.dat");
infile >>n;
for (int i =0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
infile >>square[i][j];
}
}
}

//Print the 2 dimensional arrray
void print2D(int n, int square)
{

for (int i =0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout <<square[i][j]<<" ";
}
cout <<endl;
}
}
//Determines if it is a magic sqaure
bool isMagic(int n, int square)
{
if (n == square)
return true;
else
return false;
}

int main()
{
int n;
int square;
print2D(n,square);
isMagic(n, square);

system("pause");
return 0;
}

You need to do something with the return value. Otherwise it may as well be a void function. See line 20 in the code I posted. I assign a boolean variable to the return value. In you program in line 47, you call the function, but don't assign it to a variable or do anything else with it. Try replacing line 47 in your code with something like this:

bool isMagicSquare = isMagic(n, square);
if (isMagicSquare)
cout << "Square is magic\n";
else
cout << "Square is not magic\n";

I see how the boolean function works now thanks a lot for your help I really appreciate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.