C++ bool function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thando7
    New Member
    • Apr 2007
    • 3

    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

    --------------------------------------------------------------------------------
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by thando7
    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

    Comment

    • thando7
      New Member
      • Apr 2007
      • 3

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

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

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

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

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

          Comment

          • nmadct
            Recognized Expert New Member
            • Jan 2007
            • 83

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

            Comment

            Working...