Thread: Boolean and functions

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    34

    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
    http://members.ebay.com/aboutme/the_ski/

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    34
    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
    http://members.ebay.com/aboutme/the_ski/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloaded Functions
    By quinn.snyder in forum C++ Programming
    Replies: 5
    Last Post: 10-06-2007, 11:25 PM
  2. error LNK2019: unresolved external symbol
    By Opel_Corsa in forum C++ Programming
    Replies: 3
    Last Post: 11-16-2006, 12:12 PM
  3. Boolean data type
    By NewGuy100 in forum C Programming
    Replies: 5
    Last Post: 07-28-2005, 07:00 PM
  4. help with classes and member functions
    By syner in forum C++ Programming
    Replies: 4
    Last Post: 07-19-2002, 08:45 PM
  5. boolean functions
    By Dummies102 in forum C++ Programming
    Replies: 2
    Last Post: 02-25-2002, 12:29 PM