473,386 Members | 1,758 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,386 software developers and data experts.

IF Statements

Heres a partial view of my code.
Expand|Select|Wrap|Line Numbers
  1. cout << "sqrt(x*x + y*y) = " << "sqrt(" << x*x << " + " << y*y << ") = " << sqrt(x*x+y*y) <<  endl; 
  2.     if(/*test for perfect square*/) 
  3.         cout << true;
  4.     else cout << false;
  5.     return 0;
  6. }
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.
Sep 6 '12 #1
10 1864
Rabbit
12,516 Expert Mod 8TB
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.
Sep 6 '12 #2
donbock
2,426 Expert 2GB
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.
Sep 6 '12 #3
What do you mean code tags. I never used this site before. Sorry.
Sep 7 '12 #4
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.
Sep 7 '12 #5
donbock
2,426 Expert 2GB
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.
Sep 7 '12 #6
johny10151981
1,059 1GB
well I would suggest one more way

Expand|Select|Wrap|Line Numbers
  1. if((int)pow((int)sqrt(x),2)==x) // this wont work if x is float
  2. {
  3.  cout << "Perfect square or whatever it is";
  4. }
  5.  
Sep 8 '12 #7
zmbd
5,501 Expert Mod 4TB
Soccerboy77:
Why do you need to write this program?
-z
Sep 8 '12 #8
johny10151981
1,059 1GB
homework i guess....
Sep 9 '12 #9
@donbock
I only need to go to up to the integer 20.
@zmbd
Its a class assignment
Sep 9 '12 #10
zmbd
5,501 Expert Mod 4TB
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
Sep 9 '12 #11

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

Similar topics

6
by: Bart Nessux | last post by:
Should an if statement have a corresponding else statement? Or, is it OK to have an if statement by itself. For completeness, it seems the two should be together, but from experience I know that a...
9
by: Jaime Wyant | last post by:
I know I've seen this somewhere, but can't seem to google it. Is there a way to use an alternate statement separator, other than the default ';'? jw
1
by: Tom D | last post by:
I'm rewriting a database interface that our company currently has. Currently it's using the Pear::DB interface, but we found that that was introducing a bit too much overhead. I'm rewriting the...
39
by: slogging_away | last post by:
Hi - I'm running Python 2.4.2 (#67, Sep 28 2005, 12:41:11) on win32, and have a script that makes numerous checks on text files, (configuration files), so discrepancies can be reported. The script...
0
by: Fuzzyman | last post by:
Hello all, The following is a copy of a blog entry. It's asking a question about future statements and the built in compile function. I'd appreciate any pointers or comments about possible...
20
by: Neroku | last post by:
Hello, i would like to know what the serious definition of statements and expressions is: i know an expression are evaluated to a value, i.e: 1 == 2 5+7 foo( 1,2) and a statement is...
2
by: ojorus | last post by:
Hi! Some questions regarding the mysqli-extension (php5) 1) Prepared statements: If I understand things right, prepared statements will give better performance if you make several similar...
3
by: Dmitri | last post by:
Hello! I have a developer that is playing around with some SQL statements using VB.NET. He has a test table in a SQL 2000 database, and he has about 2000 generated INSERT statements. When the...
0
by: Gary Herron | last post by:
Ohad Frand wrote: There is no way you can consider 'elif', 'else', 'except', and 'from' statements. However, as someone pointed out, the kwlist from the keyword module is the closest thing we...
0
by: Ohad Frand | last post by:
Hi Thanks a lot for your reply I think the main uses for it is to study the language and to see that I didn't miss anything else or that something is changed from one version to another. The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.