1. 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. 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. 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.

4. Or if you want to automatically change negative numbers to positive and continue...
Code:
`  if(x < 0) x = x * -1;`
-d-

5. >>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?

6. >>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-

7. > if(x < 0) x = -x;

Ok, you win.

8. >>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.

9. 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.

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

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-

10. Truce Heh, and we have yet to hear from the OP...