user2785675
user2785675

Reputation: 57

Sqrt function errors incorporating

How would I incorporate the equation into my program? Basically adding a new column of info when it compiles:

relative_error_per_cent = 100 *((my_sqrt_1(n) – sqrt(n)) / sqrt(n)

I know that it is suppose to go inside the for loop but what else is missing? Im getting errors. Been trying at this for a while

#include <iostream> 
#include <math.h> 
using namespace std; 

double my_sqrt_1(double n) 
{
    double x = 1;
    for(int i = 1; i < 10; ++i) 
        x = (x+n/x)/2;
    return x; 
} 

int main() 
{ 
    for(auto k : { -100,-10,-1,0,1,10,100})
    {
        double relative_error_per_cent = 100*((my_sqrt_1(n) – sqrt(n)) / sqrt(n))
        double n=3.14159 * pow (10.0,k);
        cout << n << sqrt(n) <<  my_sqrt_1(n) << relative_error_per_cent;
    }

    return 0; 
} 

Upvotes: 2

Views: 207

Answers (2)

P0W
P0W

Reputation: 47784

                                                    +----its a minus*
                                                    |
double n = pow (10.0,k)*3.14159;         <----------|-------------------------+
double relative_error_per_cent = 100*((my_sqrt_1(n) - sqrt(n)) / sqrt(n));    |
//double n = pow (10.0,k)*3.14159;                                    ---|----+
cout << n << sqrt(n) <<  my_sqrt_1(n) << relative_error_per_cent;        |
                                                                     semi-colon 

Use following to compile:

g++ -o file file.cpp -std=c++0x

OR

g++ -o file file.cpp -std=c++11

*minus not stray character \226, this happens when you copy-paste codes from web pages, doc or pdf files.

Upvotes: 3

lapk
lapk

Reputation: 3918

Three immediate things (among others):

First, declare (and initialize) n before relative_error_per_cent initialization.

Second, add ; in the end of relative_error_per_cent initialization.

Third, change to - in relative_error_per_cent initialization.

double n=3.14159 * pow(10.0,k);
double relative_error_per_cent = 100*((my_sqrt_1(n) - sqrt(n)) / sqrt(n));

Code on ideone.com.

Upvotes: 1

Related Questions