Find square root of given floating point number

Hi. I'm obviously new to C++. I thank you in advance for your help. I know this is a very simple program but I can't figure out what I'm doing wrong. A user will input a number in order to find the square root of the floating point number. I have to keep track of the iterations and print out the square root and the number of iterations it took. I'm also trying to implement a pass by reference. Could someone please point me in the right direction?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  
#include <iostream>
#include <cmath>
#include <iostream>

using namespace std;


int main ( )

{
float A, X;

cout << "Let's compute the square root of any number you choose!" << endl;
cout << "Make sure the number you choose is a positive number!" << endl;

        cout << "Enter your number: " << endl;
        cin >> A;
        while (A <= 0)
        {
                cout << "Invalid Input, must be greater than 0" << endl;
                cout << "Enter your number: " << endl;
                cin >> A;
        }

const double EPSILON = 1e-10;
double xnew = A;
double xold;
double sqrt(A, int& iter);

        iter = 0;
        while (fabs(xnew - xold) > EPSILON && iter < 100);
                {
                        ++iter;
                        xold = xnew;
                        xnew = (xold + A / xold) / 2;
                }

return xnew

cout << endl << "The sqare root is: ";
cout << fixed << showpoint << setprecision << xnew << endl;
cout << endl << "It took " << iter << " times." << endl;

}
line 39 should be at line 44 and end with ';'
In order to implement pass by reference you need to declare a function. For example,

1
2
3
4
void square_root(float &find_root)
{
...
}


Put your algorithm to find the square root inside this function (lines 26 to 37).
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cmath>
#include <stdio.h>      /* printf */

using namespace std;

const double EPSILON = 1e-6;
void squareroot(double, int& );

int main ( )

{
    double A;
    int cycles = 0;

    cout << "Let's compute the square root of any number you choose!" << endl;
    cout << "Make sure the number you choose is a positive number!" << endl;

    cout << "Enter your number: ";
    cin >> A;

    double answer = squareroot(A, cycles);

    cout << endl << "It took " << cycles << " times." << endl;

    return 0;
}

double squareroot(double A, int &c)
{
    // put you code here including that c = iter or just change 'iter' to 'c' everywhere.

    return xnew;
}
Thanks kemort. The comment you have for line 31, was that referring to my code for the lines that I had at 27 - 39? If so, I received the errors below. If not, then I used the wrong part of my code. :(

wishonp2x.cpp: In function ‘int main()’:
wishonp2x.cpp:23:32: error: void value not ignored as it ought to be
wishonp2x.cpp: In function ‘double sqrt(double, int&)’:
wishonp2x.cpp:30:29: error: new declaration ‘double sqrt(double, int&)’
wishonp2x.cpp:9:6: error: ambiguates old declaration ‘void sqrt(double, int&)’
Thank you soranz and squished18!
I've changed my code and the first part of Main runs (data input and data validation) but I can't get what I thought was the function to run. I understand the concept of passing by reference...I just can't completely understand the logic yet.

// This program will compute the square root of a given floating point number.


using namespace std;


void sqrt(double, int&);

int main ( )

{
double A;
int cycles = 0;

cout << "Let's compute the square root of any number you choose!" << en$
cout << "Make sure the number you choose is a positive number!" << endl;
cout << "Enter your number: " << endl;

cin >> A;
while (A <= 0)
{
cout << "Invalid Input, must be greater than 0" << endl;
cout << "Enter your number: " << endl;
cin >> A;

return A;
}




{
const double EPSILON = 1e-10;
double sqrt(double A, int &c);
double xnew = A;
double xold;

int c = 0;
while (fabs(xnew - xold) > EPSILON && c < 100);
{
++c;
xold = xnew;
xnew = (xold + A / xold) / 2.0;
}


cout << endl << "The sqare root is: ";
cout << fixed << showpoint << setprecision << xnew << endl;
cout << endl << "It took " << c << " times." << endl;

return xnew;
}
}
closed account (48T7M4Gy)
Wishon, the layout of your code needs to be very similar to mine. You have two SEPARATE functions - main() and squareroot(....)

Show us the code that produced the errors and please use the code tags. You also need to tidy up your formatting - proper indents, remove unnecessary blank lines etc. at the moment it's far too messy.

Line 8 in my code replace void with double.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cmath>
#include <stdio.h>


using namespace std;

const double EPSILON = 1e-6;
double sqrt(double, int& );

int main()

{
        double A;
        int cycles = 0;

        cout << "Let's compute the square root of any number  you choose!" << e$
        cout << "Make sure the nmber you choose is a positive number!" << endl;

        cout << "Enter your number: ";
        cin >> A;

        double answer = sqrt(A, cycles);

        cout << endl << "It took " << cycles << " times. " << endl;

        return 0;
}

double sqrt(double A, int &c)

{
double xnew = A;
double xold;

        c = 0;
        while (fabs(xnew - xold) > EPSILON && c < 100);
                {
                        ++c;
                        xold = xnew;
                        xnew = (xold + A / xold) / 2;
                }

return xnew;

}
Hi kemort. After I compile the code and run it, I enter the number I wish to find a square root for and then nothing. I don't think it's caught in an infinite loop but I don't know. I enter '8' below and it ends line and I can enter another number or anything. Any ideas? I appreciate your help!

Let's compute the square root of any number you choose!
Make sure the nmber you choose is a positive number!
Enter your number: 8
closed account (48T7M4Gy)
what are you doing with "<< e$" at the end of line 17?
that's just an "endl;" truncated....the code says endl;....not e$
closed account (48T7M4Gy)
what are you doing with a ';' in the wrong place somewhere between lines 30 an 46 inclusive. Look VERY carefully, line by line, and you'll see why you have an infinite loop.
Got it. Thank you kemort! Thanks everyone.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const double EPSILON = 1e-10;
double sqrt(double, int & );

int main()
{
        double A;
        int cycles = 0;

        cout << "Let's compute the square root of any number you choose!" << endl;
        cout << "Make sure the nmber you choose is a positive number!" << endl;

        cout << "Enter your number: ";
        cin >> A;

        while (A <= 0)
        {
        cout << "Invalid input.  Your number must be greater than zero." << endl;
        cout << "Enter your number: ";
        cin >> A;
        }

        double answer = sqrt(A, cycles);

        cout << endl << "The sqare root of " << A << " is " << answer << "." << endl;
        cout << endl << "It took " << cycles << " cycles. " << endl;

        return 0;
}

double sqrt(double A, int &cycles)

{

double xnew = A;
double xold;
cycles = 0;
while (fabs(xnew - xold) > EPSILON && cycles < 100)
        {
        ++cycles;
        xold = xnew;
        xnew = (xold + A / xold) / 2;
        }
        return xnew;
}
closed account (48T7M4Gy)
Looks like you got there W. Dont forget the #include etc

Cheers
oh yeah...I forgot to paste those. haha. It took a bit but it finally compiled. I had to do some research about the errors I was getting and things finally fell into place. But I appreciate your input and direction. It helped tremendously.

cheers from the U.S.A.!
Topic archived. No new replies allowed.