Question

Question: Define a class to find the third root of your B[15]. Please write this in...

Question: Define a class to find the third root of your B[15].

Please write this in programming C or C++ language and please explain what we are doing in each step.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:

#include <iostream>
#include <string>
using namespace std;


class ThirdRoot{
public:

   // Returns the absolute value of n-mid*mid*mid
   double diff(double n,double mid)
   {
       if (n > (mid*mid*mid))
           return (n-(mid*mid*mid));
       else
           return ((mid*mid*mid) - n);
   }

   // Returns cube root of a no n
   double findThirdRoot(double n)
   {
       // Set start and end for binary search
       double start = 0, end = n;

       // Set precision
       double e = 0.0000001;

       while (true)
       {
           double mid = (start + end)/2;
           double error = diff(n, mid);

           // If error is less than e then mid is
           // our answer so return mid
           if (error <= e)
               return mid;

           // If mid*mid*mid is greater than n set
           // end = mid
           if ((mid*mid*mid) > n)
               end = mid;

           // If mid*mid*mid is less than n set
           // start = mid
           else
               start = mid;
       }
   }
};

int main(){
   cout<<"Enter the number : ";
   double num;
   cin>>num;
   ThirdRoot rootFinder;
   cout<<"Third root of number "<<num<<" is : "<<rootFinder.findThirdRoot(num);
   return 0;
}

output:

Add a comment
Know the answer?
Add Answer to:
Question: Define a class to find the third root of your B[15]. Please write this in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT