Missing Conditional

This is a discussion on Missing Conditional within the C++ Programming forums, part of the General Programming Boards category; Hi. I have the following code which runs perfectly for every case except a negative number. This program outputs 1 ...

  1. #1
    diligentStudent()
    Join Date
    Apr 2002
    Posts
    79

    Missing Conditional

    Hi. I have the following code which runs perfectly for every case except a negative number. This program outputs 1 if the argument in perfectSquare() is an fact a perfect square (and 0 otherwise). Can someone give me a conditional statement which will get the negative number scenario going? Thanks. Steve
    Code:
    #include<iostream>
    
    using namespace std;
    
    bool perfectSquare(int);
    
    int main()
    {
    	cout<<perfectSquare(0)<<endl;
    	return 0;
    }
    
    bool perfectSquare(int x)
    {
          int y;
          for(y=0;y<=x;y++)
          {
              if(x==(y*y))
              return true;
          }
          return false;
    }

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Square roots of negative numbers are unreal. Unless you want to make it positive and then append an "i" to everything, you could just add a conditional for a negative number at the beginning, and if it returns true, end the function with an error message or something.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Code:
    #include <cmath>
     
    bool perfectSquare(int x)
    {
       if(x < 0)
    	 return false; //assuming negatives cannot be perfect squares
     
       double root = sqrt(x);
       int truncated = root;
     
       return ((double)truncated == root);
    }
    Note that your original function was returning 1 and 0, while the return type of the function is bool.
    Last edited by Hunter2; 09-12-2004 at 04:53 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Or if you want to automatically change negative numbers to positive and continue...
    Code:
      if(x < 0) x = x * -1;
    -d-
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>if(x < 0) x = x * -1;
    Code:
    #include <cmath>
    x = abs(x);
    -or-
    x = (x >=0) ? x:-x;
    -or-
    if(x < 0) x = -x;
    Do squares of unreal numbers really count as perfect squares though?
    Last edited by Hunter2; 09-12-2004 at 07:24 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    >>if(x < 0) x = x * -1;
    >#include <cmath>
    >x = abs(x);

    I vote for one fewer include.

    >Do squares of unreal numbers really count as perfect squares?

    A program can react in different ways when it encounters "bad" input

    "You entered a negative number (-234) did you mean 234?"

    -d-
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    > if(x < 0) x = -x;

    Ok, you win.
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>I vote for one fewer include.
    There are two other alternatives I posted. At least they don't involve direct multiplication

    >>A program can react in different ways when it encounters "bad" input
    A negative number is not bad input, unless the coder so designates it - and if it is 'bad' ("please enter a positive number"), then the coder should filter the input before passing it to a function. The point was, if a negative number CANNOT be a perfect square, then obviously perfectSquare() should return false when a negative is encountered. Your example would simply ignore the fact that it is not a perfect square (again, assuming that negatives are by definition not perfect squares - hence my question) and potentially give incorrect output.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    119
    Quote Originally Posted by Hunter2
    >>I vote for one fewer include.
    There are two other alternatives I posted. At least they don't involve direct multiplication
    Sorry, I replied before your edit adding the 2nd and 3rd alternatives.


    Quote Originally Posted by Hunter2
    A negative number is not bad input, unless the coder so designates it
    Hence my quotes around the word bad.


    Quote Originally Posted by Hunter2
    ...if a negative number CANNOT be a perfect square, then obviously perfectSquare() should return false when a negative is encountered. Your example would simply ignore the fact that it is not a perfect square (again, assuming that negatives are by definition not perfect squares - hence my question) and potentially give incorrect output.
    I cannot argue here.

    Truce

    -d-
    If you speak or are learning Spanish, check out this Spanish and English Dictionary, it is a handy online resource.
    What happens is not as important as how you react to what happens. -Thaddeus Golas

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Truce Heh, and we have yet to hear from the OP...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 09:26 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21