Thread: Why wont this work? bool equal

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    82

    Why wont this work? bool equal

    Am i doing something majorly wrong? No matter what 2 integers i enter..its always true/Equal?


    Code:
      struct BigNum
      {
      unsigned int digit[MAX+1];
      };
    
    
       bool equal(const BigNum & value1, const BigNum & value2)
          {
         for (int i=0; i <=MAX; i++)
         {   if(value1.digit[i] == value2.digit[i])
         return true;
         }
         return false;

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    You are returning true if any of the digits match. You should return false if any of the digits don't match.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    that doesn't work

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Unfortunately you haven't initialized all of your array and your for loop keeps going even if the elements aren't initialized...You are probably running into the null terminating character which tells where your [edit]... er sorry technical difficulties lol anyways your if statement probably sees a '\0' and a '\0' character in each of the arrays so it says its true. likewise, if you change your loop to check whether the arrays aren't equal or not, then it will read past the end of the array and get random values (which arent likely to be equal)[/edit]
    Last edited by JaWiB; 09-11-2003 at 10:24 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    I have a init function but I'm not sure if im going the right way?
    It doesn't compile...
    Code:
    void init(BigNum & value)
    {
    for (int i=0; i <= MAX; i++)
       {
        value.digit[i] = 0;
       }
    }
    
     bool equal(const BigNum & value1, const BigNum & value2)
         {
         init(value1);
         init(value2);
    
         for (int i=0; i <=MAX; i++)
         {   if(value1.digit[i] == value2.digit[i])
         return false;
         }
         return true;
    }

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    for that to work you need to put the init definition below your equal function, or have a declaration at the top of the program. Also, i think 0 will be read as a null character in the array, so you might still need to change you loop so it stops if it reads a '\0' terminating character (however, I could be wrong)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    tried what you said..but still doesn't work...hummm...anyone else know?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Think about your logic here. First you returned true if you found A SINGLE MATCH, now you're returning false if that happens! Just loop thru the arrays, and if you find A SINGLE MISMATCH, return false.

    Second:

    for (int i=0; i <= MAX; i++)

    That should be:

    for (int i=0; i < MAX; i++)

    ie:

    if you declare:

    #define MAX 5
    char buff[MAX];

    Then you have 5 elements, which index from 0-4.

    So when i <= 5, you're out of bounds.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    u mean like this?

    Code:
     bool equal(const BigNum & value1, const BigNum & value2)
         {
    
         for (int i=0; i <MAX; i++)
           if(value1.digit[i] != value2.digit[i])
         return false;
    }

  10. #10
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    That's what I/we meant when saying to check for mismatch. I think Sebastiani didn't notice your first post where you make the array size MAX+1, so I think you can disregard his suggestion to use i < MAX. Also, make sure you return true if you make it out of the loop like you did before.

    Here is your original loop with the changes we suggested. Please try to understand why this logic works better than your original.

    Code:
    struct BigNum
    {
        unsigned int digit[MAX+1];
    };
    
    
    bool equal(const BigNum & value1, const BigNum & value2)
    {
        for (int i=0; i <=MAX; i++)
        {
            if(value1.digit[i] != value2.digit[i])
                return false;
        }
        return true;
    }
    And finally, this only work if your numbers always have MAX digits. If they might be smaller, you need more logic.

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    82
    thanks for your help...!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting a swf file in a windows application
    By face_master in forum Windows Programming
    Replies: 12
    Last Post: 05-03-2009, 11:29 AM
  2. passing params between managed c++ and unmanaged c++
    By cechen in forum C++ Programming
    Replies: 11
    Last Post: 02-03-2009, 08:46 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. Interesting: bool != BOOL
    By lightatdawn in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 01-09-2002, 11:09 AM