i am a nb on this portal and i require assistance with the foll:
I have to write a C++ boolean function, that determines whether an integer parameter is a square or not, and returns the answer to main( ).
could you plz assist me with that. I have tried using the square root function but it does not seem to work
--------------------------------------------------------------------------------
5 6018
i am a nb on this portal and i require assistance with the foll:
I have to write a C++ boolean function, that determines whether an integer parameter is a square or not, and returns the answer to main( ).
could you plz assist me with that. I have tried using the square root function but it does not seem to work
--------------------------------------------------------------------------------
Show us what you've tried and maybe we can help you.
kind regards,
Jos
- bool IsSquare(int Num)
-
{
-
bool Flag = true;
-
for (int I = 2; I < (sqrt(I) / 2); I++)
-
if ((Num % I) == 0)
-
{
-
Flag = false;
-
break;
-
}
-
return Flag;
-
}
i have tried something of this sort, but the only problem is that
the square root function is a type double and what confuses me is how to find
out whether a number is a square root or not
Hi
your for loop will never execute because sqrt(2)=1.4142.../2=0.705706...
and that is way more smaller then 2.
(And please don't double post your question.Just post in same thread again to
give it a push up.)
Savage
As Savage said, please don't double post your question. The threads have been merged, and your repeat post has been deleted.
I think you should be using sqrt(Num) (the number you are testing for square-ness) instead of sqrt(l) (the index variable).
You could use the sqrt function to compute the square root of a given number, but since it uses a double-precision floating point value, your results may be slightly off from the true answer. You will probably want to test whether the result is *almost* an integer, i.e. less than 0.0000001 off from an integer value or something like that. If the square root is an integer (or "close enough"), then the number you started with is a perfect square.
Another way to do it, more accurate but probably slower, would be to try squaring a sequence of integers, counting up monotonically until the result either hits the target number (it is a perfect square) or passes it up (it's not a perfect square). I hope that makes sense.
The advantage of the second approach is that you do everything with integers so you can be sure of the correctness of your results.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Don |
last post by:
Hi NG.
I am using some C code in my CPP code, like:
extern "C" {
#include "myC_Code.h"
}
|
by: Nomak |
last post by:
Hello,
With this code:
$ cat -n ifs.cc
1 #include <vector>
2 #include <iostream>
3
4 using std::vector;
5 using std::cin;
|
by: Fred |
last post by:
Why was the bool type introduced into c++?
What does it provide that int does not and are the two entirely
interchangable in conditional expressions?
Thanks
Fred
|
by: Jon Shemitz |
last post by:
How come I can write code like "if (L)" but NOT code like "if (L ==
true)" when L is a class with an implicit operator bool?
///////////
List L = new List();
public class List
{
private...
|
by: zl2k |
last post by:
hi, there
I am using a big, sparse binary array (size of 256^3). The size may be
changed in run time. I first thought about using the bitset but found
its size is unchangeable. If I use the...
|
by: 2005 |
last post by:
Hi
I have a little trouble with this code.
There is a calss with
Class xxx{
public:
----
----
|
by: aaragon |
last post by:
Hi All,
I'm trying to print boolean values and after I searched the entire web,
I couldn't find anything that does this =/ The code that I ended up
using is as follows:
string...
|
by: varusnyc |
last post by:
Hello,
I've been struggling with this assignment for very long now. I'm not even sure how to start the code right, as well as the structure of it. My code is a complete mess, and Im not sure what...
|
by: Lycan. Mao.. |
last post by:
Hello, I'm trying to write a function adapter object, but it fails
with the above information. Can you help me.
template <typename _Predicate>
struct Unary_negate {
typedef typename...
|
by: Thorsten Dittmar |
last post by:
Hi there,
we have this C++ DLL we wrote some time ago to do some Win32 native
stuff for us.
The function we're calling in the DLL returns a boolean value (type
bool) indicating success of the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |