Question

In: Computer Science

In C++ In this assignment, we will implement a square root approximater and then an nth...

In C++

In this assignment, we will implement a square root approximater and then an nth root approximater. Recall that the nth root of x is the number when raised to the power n gives x. We can use the concepts of binary search to approximate the square root of a number, and we can continue this logic to approximate the nth square root. We're going to use the concepts of binary search to try and approximate ending the square root and ending the nth root.

You may not use any library functions for these question, except DecimalFormat.

Commenting your code is a graded requirement. You must detail each operation, loop, conditional statement and function with the desired outputs given input ranges, as well as state its purpose.

Solutions

Expert Solution

#include<bits/stdc++.h>
using namespace std;

void findRoot(double x, double n)
{

// Initialize boundary values
        double low, high;
        // if x is in between 0 and 1 then
        //the function x^n becomes monotonically decreasing
        if (x >= 0 and x <= 1)
        {
                low = x;
                high = 1;
        }
        else
        {
                low = 1;
                high = x;
        }

// used for taking approximations of the answer
        double epsilon = 0.00000001;

// Performing binary search
        double mid = (low + high) / 2;
        //loop until the difference becomes less than 0.00000001
        while (abs((pow(mid, n)) - x) >= epsilon)
        {
                // if result is greater make high as the computed result
                if (pow(mid, n) > x)
                {
                        high = mid ;
                }
                // if result is smaller or equal make low as the computed result
                else
                {
                        low = mid ;
                }
                mid = (low + high) / 2;
        }
        // Printing the result
        cout << fixed << setprecision(16) << mid;
}

// Driver code
int main()
{
        double x, n;
        cout << "Enter the value of n: ";
        cin >> n;
        cout << "Enter the value of x: ";
        cin >> x;
        // Function to compute nth root of x
        findRoot(x, n) ;
}

OUTPUT:


Related Solutions

In this assignment, we will implement a square root approximater and then an nth root approximater....
In this assignment, we will implement a square root approximater and then an nth root approximater. Recall that the nth root of x is the number when raised to the power n gives x. We learned in class that we can use the concepts of binary search to approx imate the square root of a number, and we can continue this logic to approximate the nth square root. Please look at Lecture Notes 03, section 4 for a little more...
In this problem, we will implement an nth root finder. Recall that the nth root of...
In this problem, we will implement an nth root finder. Recall that the nth root of x, written n√ x, is the number when raised to the power n gives x. In particular, please fill in the findNthRoot(int number, int n, int precision) method in the class NthRootFinder. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should fill in the answer with decimal...
PLEASE DO NOT CHANGE THE CODE BELOW! In this problem, we will implement an nth root...
PLEASE DO NOT CHANGE THE CODE BELOW! In this problem, we will implement an nth root finder. In particular, please fill in the findNthRoot(int number, int n, int precision) method in the class NthRootFinder. The method should return a string representing the nth root of number, rounded to the nearest precision decimal places. If your answer is exact, you should fill in the answer with decimal places (i.e. with input 41 and precision 5, we should return 41.00000.) You may...
write a function to determine the square root of a number. The square root of a...
write a function to determine the square root of a number. The square root of a number can be approximated by repeated calculation using the formula NG = 0.5(LG + N/LG) where NG stands for the next guess and LG stands for the last guess. The loop should repeat until the difference between NG and LG is less than 0.00001. Use an initial guess of 1.0. Write a driver program to test your square root function. I WANT THIS PROGRAM...
Writing an nth root program in C++ using only: #include using namespace std; The program must...
Writing an nth root program in C++ using only: #include using namespace std; The program must be very basic. Please don't use math sqrt or pow . For example, the 4th root of 16 is 2 because 2 * 2 * 2 * 2 = 16. The 4th root of 20 is 2 because 2 * 2 * 2 * 2 = 16 and 16 is less than 20, and 3 * 3 * 3 * 3 = 81, which...
An element a in a field F is called a primitive nth root of unity if...
An element a in a field F is called a primitive nth root of unity if n is the smallest positive integer such that an=1. For example, i is a primitive 4th root of unity in C, whereas -1 is not a primitive 4th root of unity (even though (-1)4=1). (a) Find all primitive 4th roots of unity in F5 (b) Find all primitive 3rd roots of unity in F7 (c) Find all primitive 6th roots of unity in F7...
Graph the square root of the following function. State the domain and range of the square root of the function.
Graph the square root of the following function. State the domain and range of the square root of the function.y = -2x + 4
The _____________ is the square root of the arithmetic average of the squared deviations from the mean. In other words, it is simply the square root of the ______________.
The _____________ is the square root of the arithmetic average of the squared deviations from the mean. In other words, it is simply the square root of the ______________.data spread; population standard deviationStandard deviation; data spreadpopulation standard deviation; variancevariance; standard deviation
(a) Let n be odd and ω a primitive nth root of 1 (means that its...
(a) Let n be odd and ω a primitive nth root of 1 (means that its order is n). Show this implies that −ω is a primitive 2nth root of 1. Prove the converse: Let n be odd and ω a primitive 2nth root of 1. Show −ω is a primitive nth root of 1. (b) Recall that the nth cyclotomic polynomial is defined as Φn(x) = Y gcd(k,n)=1 (x−ωk) where k ranges over 1,...,n−1 and ωk = e2πik/n is...
For this computer assignment, you are to write a C++ program to implement a class for...
For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. Most of the public member functions of the BinaryTree class call private member functions of the class (with the same name). These private member functions can be implemented as either recursive or non-recursive, but clearly, recursive versions of these functions are preferable because of their short and simple implementations...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT