Heres a partial view of my 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.
10 1864
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.
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.
What do you mean code tags. I never used this site before. Sorry.
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.
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.
well I would suggest one more way -
if((int)pow((int)sqrt(x),2)==x) // this wont work if x is float
-
{
-
cout << "Perfect square or whatever it is";
-
}
-
zmbd 5,501
Expert Mod 4TB
Soccerboy77:
Why do you need to write this program?
-z
@donbock
I only need to go to up to the integer 20.
@zmbd
Its a class assignment
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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: 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...
|
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...
| |