# is given # a perfect square?

• (3 Pages)
• 1
• 2
• 3

## 41 Replies - 10125 Views - Last Post: 12 July 2012 - 06:07 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=285430&amp;s=4ca164a561a55861bdccff9f1ebbbc1f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 trendygirl

Reputation: 0
• Posts: 30
• Joined: 05-July 12

# is given # a perfect square?

Posted 10 July 2012 - 07:37 PM

Hello, been all over DIC site to find help & I did find some... I am trying to write function that determines wheter a given # is a perfect square, teacher provided
```bool IsSquare (int);
```
so I am trying to use it & the
```mySquareRoot
```
that I found here, but I am doing something wrong. See error below...Suggestions??
You all are a great help! Thank you!

```#include<iostream>
#include<cmath>
#include<string>

using namespace std;

//****function prototypes****************************************************
//1st -- this section describes the type/format of the functions

//return type: bool
//parameters: double
//purpose:  this function determines whether a given number is a perfect square.

bool IsSquare (double num);

//2nd -- this section calls the function needed

int main()
{

//declare variables
double num;
double IsSquare;
double float mySquareRoot;

//call IsSquare function =  bool IsSquare(double num);
//prompt for number

do
{
cout<<"Enter number to determine if it is a perfect square: \n";
cin>>num;

if (num >= 1)
{
mySquareRoot = sqrt(num);
cout<< "The square root is " <<mySquareRoot<<endl;
}
}while (num >= 1);

return 0;
}

//function declarations
//3rd --  this section performs the action

//return type: bool
//parameters:  double
//purpose:  this function determines whether a given number is a perfect square.

bool IsSquare (double num)
{
return mySquareRoot = sqrt(num);
}
```

Errors:
```square.cpp: In function ‘bool IsSquare(double)’:
square.cpp:62: error: ‘mySquareRoot’ was not declared in this scope
```

Is This A Good Question/Topic? 0

## Replies To: is given # a perfect square?

### #2 GunnerInc

• "Hurry up and wait"

Reputation: 910
• Posts: 2,357
• Joined: 28-March 11

## Re: is given # a perfect square?

Posted 10 July 2012 - 07:43 PM

The C/C++ Challenge section is not for code help. I will move it somewhere... Oh yeah, to C/C++ :-)

### #3 trendygirl

Reputation: 0
• Posts: 30
• Joined: 05-July 12

## Re: is given # a perfect square?

Posted 10 July 2012 - 07:52 PM

thank you, Hall Monitor! Sorry for duplicate entry, browser was acting up!

### #4 zainsiddiqui14

Reputation: 15
• Posts: 30
• Joined: 23-November 10

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

### #5 totgeburt

Reputation: 57
• Posts: 227
• Joined: 03-August 11

## 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

This post has been edited by totgeburt: 10 July 2012 - 11:58 PM

### #6 Huskerfan

Reputation: 2
• Posts: 16
• Joined: 31-January 12

## 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

### #7 zainsiddiqui14

Reputation: 15
• Posts: 30
• Joined: 23-November 10

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

### #8 zainsiddiqui14

Reputation: 15
• Posts: 30
• Joined: 23-November 10

## 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:
```pseudo code
if is square
display num + "is a square"
else
display num+"is not a prefect square"
```

Of course this is assuming that all user inputs are actually valid.

This post has been edited by zainsiddiqui14: 11 July 2012 - 01:18 AM

### #9 trendygirl

Reputation: 0
• Posts: 30
• Joined: 05-July 12

## Re: is given # a perfect square?

Posted 11 July 2012 - 04:02 PM

Hey there **ZAIN**

I got rid of lines 34-38 above & I put the above code in. Is that where it should go? I did also get rid of
```mysquareroot
```
, no validation is necessary...Thank you!!

```bool IsSquare (double num);

//***main********************************************************************
//2nd -- this section calls the function needed

int main()
{

//declare variables
double num;
double IsSquare;

//call IsSquare function =  bool IsSquare(double num);
//prompt for number

do
{
cout<<"Enter number to determine if it is a perfect square: \n";
cin>>num;
{
if (is square)

display num + "is a square"

else

display num +"is not a prefect square"

}
}while (num >= 1);

return 0;
}

//function declarations
//3rd --  this section performs the action

//return type: bool
//parameters:  double
//purpose:  this function determines whether a given number is a perfect square.

bool IsSquare (double num)
{
return  bool IsSquare (double num);
}
```

```square.cpp: In function ‘int main()’:
square.cpp:40: error: ‘is’ was not declared in this scope
square.cpp:40: error: expected `)' before ‘square’
square.cpp:42: error: ‘display’ was not declared in this scope
square.cpp:42: error: expected `;' before ‘num’
square.cpp: In function ‘bool IsSquare(double)’:
square.cpp:65: error: expected primary-expression before ‘bool’
square.cpp:65: error: expected ‘;’ before ‘bool’
```

### #10 Skydiver

• Code herder

Reputation: 4766
• Posts: 15,745
• Joined: 05-May 12

## Re: is given # a perfect square?

Posted 11 July 2012 - 04:09 PM

Zain told you specifically that he was showing you pseudo code. It's your job to translate that pseudo code into real code.

### #11 jared.deckard

Reputation: 18
• Posts: 46
• Joined: 11-July 12

## Re: is given # a perfect square?

Posted 11 July 2012 - 04:46 PM

You are getting there.

Try this: http://ideone.com/Q4Dv0
```// 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){
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;
}

```

### #12 zainsiddiqui14

Reputation: 15
• Posts: 30
• Joined: 23-November 10

## Re: is given # a perfect square?

Posted 11 July 2012 - 04:56 PM

^What Skydiver said. Also, your new function IsSquare seems like an infinite recursion.

### #13 zainsiddiqui14

Reputation: 15
• Posts: 30
• Joined: 23-November 10

## 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:

```for (int i = 0; i*i <= num; ++i){
if (i*i==num)
return true;
}
return false;

```

This post has been edited by zainsiddiqui14: 11 July 2012 - 05:18 PM

### #14 trendygirl

Reputation: 0
• Posts: 30
• Joined: 05-July 12

## 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!

### #15 zainsiddiqui14

Reputation: 15
• Posts: 30
• Joined: 23-November 10

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