-
Boolean and functions
Ok here is a little code I am working on:
#include<iostream.h>
using std::cout;
using std::cin;
using std::endl;
const int n = 4;
bool checkrows (int thesquare[][n], int numrows, int magicvalue);
int main()
{
int a[n][n],i,j,x,y;
for ( int b = 0; b < n; b++ )
{
cout << "Enter the 4 values for row " << b + 1 << ", seperated by space: ";
for (int c = 0; c < n; c++)
cin >> a[b][c];
}
cout << "Hmmm.... is it a magic square? Let's see...." << endl;
cout << "Checking rows ... " << endl;
for ( int d = 0; d < n; d++ )
{
bool checkrows ( a[d][n], i, j);
if ( checkrows == false )
cout << setw( 8 ) << "Eek! Row " << d+1 << " does not add up to the magic value!";
}
for ( x = 0; x < n; x++ )
{
cout <<"Here is row " << x+1 << ":";
for ( y = 0; y < n; y++ )
cout << a[x][y] << " ";
cout << endl;
}
return 0;
}
bool checkrows (int thesquare[][n], int numrows, int magicvalue);
{
int l, m, o, p;
m = 0;
for ( l = 0; l < n; l++ )
m = m + thesquare[][l];
if ( m != (n(n^2+1)/2))
return false;
else
return true;
}
and here are my errors:
prog_21.cpp: In function `int main()':
prog_21.cpp:20: initializer list being treated as compound expression
prog_21.cpp:22: implicit declaration of function `int setw(...)'
prog_21.cpp: At top level:
prog_21.cpp:35: parse error before `{'
prog_21.cpp:37: ANSI C++ forbids declaration `m' with no type
prog_21.cpp:38: parse error before `for'
prog_21.cpp:38: parse error before `;'
prog_21.cpp:38: syntax error before `++'
My question is how can you make a boolean function and how do you pass the varriables?
The Ski
-
Try this:
Code:
#include <iostream.h>
#include <iomanip>
#include <math>
using std::cout;
using std::cin;
using std::endl;
const int n = 4;
bool checkrows (int thesquare[][n], int row_num, int magicvalue);
int main()
{
int a[n][n],j,x,y;
for ( int b = 0; b < n; b++ )
{
cout << "Enter the 4 values for row " << b + 1 << ", separated by space: ";
for (int c = 0; c < n; c++)
cin >> a[b][c];
}
cout << "Hmmm.... is it a magic square? Let's see...." << endl;
cout << "Checking rows ... " << endl;
for ( int d = 0; d < n; d++ )
{
if ( checkrows( a, d, j) == false )
cout << setw( 8 ) << "Eek! Row " << d+1 << " does not add up to the magic value!";
}
for ( x = 0; x < n; x++ )
{
cout <<"Here is row " << x+1 << ":";
for ( y = 0; y < n; y++ )
cout << a[x][y] << " ";
cout << endl;
}
return 0;
}
bool checkrows (int thesquare[][n], int row_num, int magicvalue)
{
int l, m, o, p;
m = 0;
for ( l = 0; l < n; l++ )
{
m += thesquare[row_num][l];
if ( m != (n*(pow(n,2)+1)/2))
return false;
else
return true;
}
}
It doesn't appear you need the last parameter of function checkrows(), unless this is something you are adding later.
-
Well thanks for the feedback, but I was cutting close on time, just finished the program like 30 seconds ago and already turned it in. I was not required to use functions, but it makes things look better then 23 different "if then else" and "for" loops. Unfortunately, that was the road I took. Will not get points deducted but the program now works. I was going to be adding a LOT more stuff to the program and I did. Just gotta wait till next week to see how teacher does his program with these boolean functions.
Thanks again Swoopy
The Ski