You are not logged in.

#1 2007-05-05 09:22:45

pecan
Member
Registered: 2007-04-06
Posts: 93

Simple C++ help

I know I seem to be littering this forum with tedious simple questions but this will be the last for a while I promise. I'm trying to create a very simple little app that can find the square root of a number entered by a user.The code so far is

#include <iostream>
using namespace std;
int main()
{
float a, b, c, d;

cout << "enter a";
cin >> a;
b = 0;
while (a != c) {
d = b;
b = d + 1;

c = b * b;
}
cout << b;
}

That works great for whole numbers, but when I change "b = d + 1" to " b= d + 0.000001" to find the square root of 5.76 the application just gets stuck in a loop. It can't even find a whole number number like 9 after that piece of code has been changed. Now I know I'm probably doing something incredibly stupid, so feel free to point it out.

Also I know there is a square root function from the <math> but I'm doing this as a learning experience, I was just interested in a way of finding the square root of a number.
Thanks

Last edited by pecan (2007-05-05 09:23:34)

Offline

#2 2007-05-05 10:36:10

wu.soldier
Member
From: Poland
Registered: 2006-12-19
Posts: 22

Re: Simple C++ help

When you decrease step to 1e-7 it takes too much time to find square root, you can increase step and add tolerance (variable e).

Try this one:

#include <iostream>
using namespace std;

int main()
{
        float a, b, c, d;

        float e = 0.001;

        cout << "enter a\n";
        cin >> a;

        b = 0;

        while((c - a) < e || (a - c) > e)
        {
                d = b;
                b = d + 0.001;

                c = b * b;
        }

        cout  << b;

        return 0;
}

Offline

#3 2007-05-05 10:57:32

pecan
Member
Registered: 2007-04-06
Posts: 93

Re: Simple C++ help

After changing that to a do while loop thats solved the problem. Thanks for the help. What other methods could someone use to find a square root of a number?

Offline

#4 2007-05-05 12:12:20

pjeremy
Member
Registered: 2007-04-03
Posts: 66

Re: Simple C++ help

Why so complicated? Besides that, checking floating points for equality is bad, so is 'using namespace std'

#include <iostream>
#include <cmath> //well, if you use this, you could also use std::sqrt, so just implement pow+ln yourself

int main(){
    double a,b;
    const double e=2.7182818; //higher precision=>better result
    char c;
    do{
        std::cout << "enter a value: ";
        std::cin >> a;
        b=std::pow(e,0.5*std::log(a));
        std::cout << b << std::endl; //include iomanip to output more digits
        std::cout << "Quit? [q|Q]\nAny key to continue\n";
        std::cin >> c;
    }while(c!='q' && c!='Q');
}

Use gmp for arbitrary precision.

Offline

#5 2007-05-05 12:16:43

pecan
Member
Registered: 2007-04-06
Posts: 93

Re: Simple C++ help

I'm a beginner, why is using namespace std bad?

Offline

#6 2007-05-05 12:55:28

pjeremy
Member
Registered: 2007-04-03
Posts: 66

Re: Simple C++ help

Basically, it's bad (newbie) style and may lead to conflicts with other namespaces/classes/etc.. besides std.
Here some links with more, detailed explanation.
http://www.parashift.com/c++-faq-lite/c … l#faq-27.5
http://cboard.cprogramming.com/showthread.php?t=55269
Why checking floating points for equality is bad is explained here:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
http://www.cygnus-software.com/papers/c … floats.htm

Besides that, c wasn't initialized when you did a!=c, so c has a garbage value at the beginning.
The same error applies to wu.soldier's code.

Also, if you want the n-th root, replace 0.5 with 1/n.

Last edited by pjeremy (2007-05-05 13:19:25)

Offline

#7 2007-05-05 16:17:19

equadon
Member
Registered: 2007-04-12
Posts: 25

Re: Simple C++ help

It's neither bad nor a newbie style to use using namespace std. It certainly has its uses. The problem is that most people don't understand how and why to use it.

If you insist on never using it, you're missing out on a good feature in C++. Sometimes it's not necessary to mess with those stuff. At the same time, if you always use it, you might get conflicts. Knowledge is power.

pecan: In this example, there is no problem using using namespace std. If you decide to make the application bigger or more complex, you need to know how to use it properly. If you don't intend to read more about the "using" keyword, and namespaces (which I strongly suggest), stick with what pjeremy suggested.

Offline

#8 2007-05-05 16:41:47

gradgrind
Member
From: Germany
Registered: 2005-10-06
Posts: 921

Re: Simple C++ help

If you don't want to use the maths library functions I would suggest the 'Babylonian' method (see, e.g., http://en.wikipedia.org/wiki/Square_root ).

Offline

#9 2007-05-05 19:34:23

pjeremy
Member
Registered: 2007-04-03
Posts: 66

Re: Simple C++ help

I think using namespace xyz is okay as long as you keep its scope at a minimum, for example within a function, still I prefer to use xyz::abc, since there's no chance you get a conflict and you always know which namespace something belongs to.
Of course this is irrelevant in such an example, but starting with a good habit is easier than breaking one.

Offline

#10 2007-05-07 15:47:54

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Simple C++ help

'using namespace std' is poor form _only in header files_.

Do whatever you want in implementation files, though I prefer "std::"

Offline

#11 2007-06-12 09:28:07

d2v
Member
Registered: 2007-04-28
Posts: 15

Re: Simple C++ help

In the original code, variable 'c' is not initialized, and is compared with 'a'. You should initialize it to some useful value, so as to avoid (extremely rare) case of 'a' and junk value of 'c' being equal.

Offline

Board footer

Powered by FluxBB