IF Statements

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • soccerboy77
    New Member
    • Sep 2012
    • 4

    IF Statements

    Heres a partial view of my code.
    Code:
    cout << "sqrt(x*x + y*y) = " << "sqrt(" << x*x << " + " << y*y << ") = " << sqrt(x*x+y*y) <<  endl; 
    	if(/*test for perfect square*/) 
    		cout << true;
    	else cout << false;
    	return 0;
    }
    I need help on what command to put where it says (/*test for perfect square) of the sums of X's and Y's. The numbers have to be between 1-20. Thanks in advance.
    Last edited by Rabbit; Sep 6 '12, 08:44 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Please use code tags when posting code.

    There's only 7 combinations that result in a perfect square. You can either test for each case or you can take the sqaure rooted result, multiply by 100 and take the mod of that. If the mod is 0, then it's a perfect square.

    Note that the second method, while simpler, is only accurate within the confines of the parameters that you laid out.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Please describe your understanding of what a perfect square is and how you might recognize one. Just express this in words -- we'll worry about software later.

      Comment

      • soccerboy77
        New Member
        • Sep 2012
        • 4

        #4
        What do you mean code tags. I never used this site before. Sorry.

        Comment

        • soccerboy77
          New Member
          • Sep 2012
          • 4

          #5
          I know how to find perfect squares I just don't know how to write an if statement that can check the sums of x and y, and produce either a false or true statement.
          This is what I need to do: I have to write a program that gets two numbers between 1-20, square both of them to 2nd power, and say whether there sums is a perfect square.
          For example 3^2+4^2= 25. 25 is a perfect square so that would be true.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            There are basically two approaches: test if the value of the square root is an integer or compare the sum of the squares to a list of perfect squares.

            Inaccuracies inherent in floating-point arithmetic (refer to what every computer scientist should know about floating-point arithmetic) make it nontrivial to tell for sure if a floating-point number has an integral value; but you can do it if you're careful. This is the basis of Rabbit's suggestion; but you should understand how his multiply-and-mod trick deals with floating-point inaccuracies before you rely on it.

            For the second approach, how big does the list of perfect squares need to be? The largest sum you could get is 20^2 + 20^2 = 800. The square root of 800 is 28.3. Thus, the list needs to hold the values of 0^2 through 28^2. You may find it convenient to include 29^2 in the list as a sentinel value -- then you only need two search termination conditions: list value equals the sum or list value is greater than the sum.

            Comment

            • johny10151981
              Top Contributor
              • Jan 2010
              • 1059

              #7
              well I would suggest one more way

              Code:
              if((int)pow((int)sqrt(x),2)==x) // this wont work if x is float
              {
               cout << "Perfect square or whatever it is";
              }

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                Soccerboy77:
                Why do you need to write this program?
                -z

                Comment

                • johny10151981
                  Top Contributor
                  • Jan 2010
                  • 1059

                  #9
                  homework i guess....

                  Comment

                  • soccerboy77
                    New Member
                    • Sep 2012
                    • 4

                    #10
                    @donbock
                    I only need to go to up to the integer 20.
                    @zmbd
                    Its a class assignment

                    Comment

                    • zmbd
                      Recognized Expert Moderator Expert
                      • Mar 2012
                      • 5501

                      #11
                      That's what I thought.

                      I had the exact same assignment in our FORTRAN class some twenty plus years ago.

                      Rabbit and the others have done your homework.......
                      you should have enough information to complete the assigment.

                      Now, go review your notes and read the textbook.

                      -z

                      Comment

                      Working...