Re: is given # a perfect square?

Posted 10 July 2012 - 10:37 PM

The aim of the program is a little unclear. Does it aim to calculate the square root of a number, or does it exclusively aim to determine whether a number is a perfect square? Or does it want to do both? Your variable IsSquare's name implies that the variable should be assigned a true or false value, yet it's type is a double. Your function IsSquare implies it should return true or false, yet it returns a double. So I think the first step is to clarify and sort out what you want the program to do.

Re: is given # a perfect square?

Posted 10 July 2012 - 11:56 PM

just glancing over it quicky, your IsSquare is supposed to return a bool, but you have it returning a double. you should assign that equation to a bool variable and have it return the bool
^heh.. like zain said. and i'm pretty sure he just wants to determine if a number is a perfect square

Re: is given # a perfect square?

Posted 11 July 2012 - 12:24 AM

Also, as far as your second error...

MySquareRoot is only available in the scope of main. To access it in your function you would either need to pass it in as a parameter to the function, or (less favorable) declare it as a global variable outside of main

Re: is given # a perfect square?

Posted 11 July 2012 - 01:06 AM

Okay, so my first post was a little too rhetorical : P Anyway, I think the general agreement among us is that IsSquare needs to return a bool, a type that returns a 'true' or 'false'. So the function needs to take in a number and see if it's a "perfect square" (a number that yields a positive integer when the square root of that number is calculated ), and then return a "yes" (true) or "no" (false) based on the evaluation. At the moment, two solutions, for me anyway, come to mind. One way to see if it's a perfect square is by typecasting, but I think a logically simpler way to do it is by using a loop that checks perfect squares up until the number you entered.

Re: is given # a perfect square?

Posted 11 July 2012 - 01:17 AM

So really, you can take out the variable "MySquareRoot" and just use two variables in main(): a bool "isSquare" and a double "num". And I think a more appropriate output for the result when you display it main() would be something like:

Re: is given # a perfect square?

// You need this for cin and cout
#include <iostream>
// You need this for sqrt
#include <cmath>
// You need this for lots of stuff
using namespace std;
// Returns true if number is a perfect square
bool IsSquare (int num)
{
// Calculate the square root of num
// Casting to an int truncates any decimal parts
int sqrt_num = sqrt(num);
// If the previous line didn't truncate and digits
// then the number must be a perfect square.
// If the square root squared equals the original
// number it is a perfect square!
return sqrt_num*sqrt_num == num;
}
int main()
{
// Perfect squares only work on integers
int num;
// Loop while num
while(true){
// Ask for number
cout << "Enter number to determine if it is a perfect square: \n";
cin >> num;
// Stop if number is less than 1
if(num < 1) break;
// Number was a square
if (IsSquare(num)) cout << num << " is a square\n";
// Number wasn't a square
else cout << num << " is not a prefect square\n";
}
return 0;
}

Re: is given # a perfect square?

Posted 11 July 2012 - 05:04 PM

Also, a very clever solution from Jared! I had typecasting in mind, but what he has is a slightly more efficient version of the same idea. But are you supposed to give solutions like that outright? Here's another algorithm that doesn't typecast:

Re: is given # a perfect square?

Posted 11 July 2012 - 05:17 PM

yes, it is in a loop...not ending....idk what typecasting is yet, so i don't want to use anything that Prof. hasnt taught yet....thank you, let me apply this within my "function" program & see how it goes!

Re: is given # a perfect square?

Posted 11 July 2012 - 05:23 PM

I edited my comment to include another way to solve it without truncating decimals or typecasting (typecasting is just converting a type to another type). It checks all of the perfect squares up until the number you entered and sees if any of the squares match your number.