Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 sqrt ..
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CHUTHAN20




PostPosted: 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...


code:

#include <iostream.h>
#include <math.h>
int main()
{
        double i, r[25], x=0;
        for (i=0; i<25; i++)
        {
        x= (sqrt(i)=0);
        r[i]=x;
        cout <<r<<endl;
        }
        return 0;
}
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Sat Oct 21, 2006 8:29 pm   Post subject: (No 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.
wtd




PostPosted: Sat Oct 21, 2006 9:31 pm   Post subject: Re: sqrt ..

CHUTHAN20 wrote:
code:

#include <iostream.h>
#include <math.h>
int main()
{
        double i, r[25], x=0;
        for (i=0; i<25; i++)
        {
        x= (sqrt(i)=0);
        r[i]=x;
        cout <<r<<endl;
        }
        return 0;
}


Lots of old deprecated C++ and bad C-inspired scoping here. Also, double is not a valid type to index an array with.

Also...

code:
        x= (sqrt(i)=0);
        r[i]=x;


You do not need the intermediate assignment.

Also...

code:
sqrt(i) = 0


Is meaningless. Did you mean?

code:
sqrt(i) == 0


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.
CHUTHAN20




PostPosted: Sun Oct 22, 2006 11:16 am   Post subject: (No subject)

hmm... wen i put..

code:

sqrt(i)==1;


it just giving me... wen sqrt (i) equals 1 then it gives me 1.. nd others r zero...
wtd




PostPosted: Sun Oct 22, 2006 11:34 am   Post subject: (No subject)

In C++ there can be implicit conversions between values of different types.

code:
sqrt(i) == 1


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.
r.3volved




PostPosted: Sun Oct 22, 2006 12:54 pm   Post subject: (No subject)

Not too sure what you mean, but is it something like this that you're looking for??

code:

#include <iostream>

int main()
{
    int x = 0;
    while( x*x < 25 )
    {
        std::cout << x << std::endl;
        ++x;
    }
    return 0;
}
md




PostPosted: Sun Oct 22, 2006 1:44 pm   Post subject: (No subject)

r.3volved wrote:
Not too sure what you mean, but is it something like this that you're looking for??

code:

#include <iostream>

int main()
{
    int x = 0;
    while( x*x < 25 )
    {
        std::cout << x << std::endl;
        ++x;
    }
    return 0;
}


We were trying to avoid giving him the solution Wink
r.3volved




PostPosted: Sun Oct 22, 2006 3:49 pm   Post subject: (No 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.)
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Sun Oct 22, 2006 4:20 pm   Post subject: (No 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).
CHUTHAN20




PostPosted: Sun Oct 22, 2006 4:32 pm   Post subject: (No subject)

oo.. i will be right back after i figured it out... Cool (hopefully)
wtd




PostPosted: Sun Oct 22, 2006 6:40 pm   Post subject: (No 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.
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: