0

Iam Beginner in programming and i have a question. how make algorithm for cube root finding in C++ without using functions like pow(). The user enters the number and the number of decimal places.

My code: My do while isn't working

double number;
cout << "Enter number = ";
cin >> number;
int x;
int y = number / 3.;
int i;
cout << "Enter i = ";
cin >> i;
do {
    x = y;
    y = (2. * x + number / (x * x)) / 3.;
} while (abs(x - y) >= i);
 
scohe001
  • 15,110
  • 2
  • 31
  • 51
Fred
  • 35
  • 5
  • what is the meaning of "not working" ? Do you know the forumla and have problems to write the code for it or are you also looking for the right formula? – 463035818_is_not_an_ai Aug 26 '20 at 14:21
  • 3
    First, shouldn't all your variables be of type `double`, or at the very least, a floating point type? – PaulMcKenzie Aug 26 '20 at 14:21
  • 3
    Without mathematical formula, binary_search between `1` and `x` until `upper - lower < 10**-i`? – Jarod42 Aug 26 '20 at 14:21
  • You need to be more specific of what you want. Are you just interested in integer solutions to x^3 = y? Do you want a good floating point approximation? Do you want an approximation with exact error bounds? – Unlikus Aug 26 '20 at 14:26
  • The easy way is to cheat: search the internet for "c++ find cube root". In modern times, search the internet first, there is usually an existing algorithm or example residing on the internet. – Thomas Matthews Aug 26 '20 at 14:47

1 Answers1

2

Your algorithm is nearly fine. You just need to change your variables to float/double. Here is the edited code :

#include <iomanip>
#include <iostream>
#include <limits>
using namespace std;

int main() {
    double number;
    cout << "Enter number = ";
    cin >> number;
    double x;
    double y = number / 3.;
    double i;
    cout << "Enter i = ";
    cin >> i;
    do {
        x = y;
        y = (2. * x + number / (x * x)) / 3.;
    } while (abs(x - y) >= numeric_limits<double>::epsilon());
    cout << fixed << setprecision(i) << y;
}

Sample Run :

Enter number = 10
Enter i = 2
2.15


A little add up :

As pointed out by chux - Reinstate Monica, abs(x - y) >= numeric_limits<double>::epsilon() is not a good condition to check equality. You can go through this thread for more knowledge : What is the most effective way for float and double comparison?

Another one : Why is “using namespace std;” considered bad practice?

brc-dd
  • 10,788
  • 3
  • 47
  • 67
  • But how to implement in the code so that the user himself indicates how many characters should be displayed after the point? – Fred Aug 26 '20 at 14:39
  • it's like number = 5; i = 2; output : 5.11 – Fred Aug 26 '20 at 14:41
  • 2
    [`std::setprecision`](https://en.cppreference.com/w/cpp/io/manip/setprecision) to limit display. – Jarod42 Aug 26 '20 at 14:44
  • @Fred As Jarod42 mentioned, [use `std::fixed` followed by `std::setprecision`](https://stackoverflow.com/questions/5907031/printing-the-correct-number-of-decimal-points-with-cout). [Here](https://wandbox.org/permlink/32tV6pdlSYm8qFEK) is an example usage. Reflected the edit in the answer. :) – brc-dd Aug 26 '20 at 15:47
  • @Fred How can the output be `5.11`?? For `5 2` as input, shouldn't the output be `1.71`? – brc-dd Aug 26 '20 at 15:53
  • `abs(x - y) >= numeric_limits::epsilon()` is a poor terminating condition for very large and very small `x`. That takes the float out of floating point. – chux - Reinstate Monica Aug 26 '20 at 16:22