Computer Science Canada sqrt .. |
Author: | CHUTHAN20 [ Sat Oct 21, 2006 8:23 pm ] | ||
Post subject: | sqrt .. | ||
Hey, help plz. wat i am trying to do is to display all the numbers that are below 25 and whose square root is a whole number. For example 16 has a whole square root (4), but I don't want 4 is to be printed out.. I want 16 to be printed out.. thanks in advance nd.. here is the code...
|
Author: | md [ Sat Oct 21, 2006 8:29 pm ] |
Post subject: | |
instead of looping from 1 to 25, why not loop from one to X, and for every number display it's square. stop when the square is > 25. |
Author: | wtd [ Sat Oct 21, 2006 9:31 pm ] | ||||||||
Post subject: | Re: sqrt .. | ||||||||
CHUTHAN20 wrote:
Lots of old deprecated C++ and bad C-inspired scoping here. Also, double is not a valid type to index an array with. Also...
You do not need the intermediate assignment. Also...
Is meaningless. Did you mean?
If so, that returns either true or false. This is a value of type "bool", which indicates to me that your array "r" should be of type "bool". Assigning this to a double variable means that true becomes 1.0 and false becomes 0.0. Just store the true or false. It makes life much easier. |
Author: | CHUTHAN20 [ Sun Oct 22, 2006 11:16 am ] | ||
Post subject: | |||
hmm... wen i put..
it just giving me... wen sqrt (i) equals 1 then it gives me 1.. nd others r zero... |
Author: | wtd [ Sun Oct 22, 2006 11:34 am ] | ||
Post subject: | |||
In C++ there can be implicit conversions between values of different types.
The above returns a value of type bool. This is either true or false. In C (but not C++), however, there is no proper boolean type. Instead the number zero represents false, and anything else represents true. Typically 1 is used to represent true. When C++ implicitly converts a bool value to a double value, it takes this into account. Thus "sqrt(1) == 1", when converted to a double value, gives you 1.0. Any other value for i and you get 0.0. I would postulate that this implicit conversion is not what you really want to be doing, and that you mean to be storing the double value whose square root you are testing. |
Author: | r.3volved [ Sun Oct 22, 2006 12:54 pm ] | ||
Post subject: | |||
Not too sure what you mean, but is it something like this that you're looking for??
|
Author: | md [ Sun Oct 22, 2006 1:44 pm ] | ||
Post subject: | |||
r.3volved wrote: Not too sure what you mean, but is it something like this that you're looking for??
We were trying to avoid giving him the solution |
Author: | r.3volved [ Sun Oct 22, 2006 3:49 pm ] |
Post subject: | |
Sometimes giving a solution to a small roadblock can spur the interest of coding. See how it's done compared to how you thought it should be done. As good as it is to get people to just do it on their own, sometimes (specially with newbie programmers, it's more of a hindrance than a help to just hint about things they don't understand yet.) |
Author: | md [ Sun Oct 22, 2006 4:20 pm ] |
Post subject: | |
r.3volved wrote: Sometimes giving a solution to a small roadblock can spur the interest of coding. See how it's done compared to how you thought it should be done.
As good as it is to get people to just do it on their own, sometimes (specially with newbie programmers, it's more of a hindrance than a help to just hint about things they don't understand yet.) Hence why I told him how to do it without giving code in my first reply. When people always give you code you never have to think or learn, when people tell you how to solve toe problem and let you write the code then you learn; especially if they will then help you with problems in your new code (as we will here). |
Author: | CHUTHAN20 [ Sun Oct 22, 2006 4:32 pm ] |
Post subject: | |
oo.. i will be right back after i figured it out... (hopefully) |
Author: | wtd [ Sun Oct 22, 2006 6:40 pm ] |
Post subject: | |
md wrote: When people always give you code you never have to think or learn, when people tell you how to solve toe problem and let you write the code then you learn; especially if they will then help you with problems in your new code (as we will here).
Lest this be taken the wrong way, this is not intended to suggest that the original poster in this thread was simply looking for an easy answer. |