473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

C++ bool function

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

--------------------------------------------------------------------------------
Apr 11 '07 #1
5 6018
JosAH
11,448 Expert 8TB
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
Apr 11 '07 #2
Expand|Select|Wrap|Line Numbers
  1. bool IsSquare(int Num)
  2. {
  3.     bool Flag = true;
  4.     for (int I = 2; I < (sqrt(I) / 2); I++)
  5.         if ((Num % I) == 0)
  6.         {
  7.             Flag = false;
  8.             break;
  9.         }
  10.     return Flag;
  11. }    

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
Apr 11 '07 #3
Savage
1,764 Expert 1GB
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
Apr 11 '07 #4
Ganon11
3,652 Expert 2GB
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).
Apr 11 '07 #5
nmadct
83 Expert
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.
Apr 12 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Don | last post by:
Hi NG. I am using some C code in my CPP code, like: extern "C" { #include "myC_Code.h" }
4
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;
12
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
2
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...
6
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...
9
by: 2005 | last post by:
Hi I have a little trouble with this code. There is a calss with Class xxx{ public: ---- ----
12
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...
2
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...
4
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.