954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cube Root Calculator

I am a complete newbie in C++ programming. After a very basic introductory class I am now reading a little more advanced book and trying to teach myself and I am slowly learning. Anyway, the book mentions a method for calculating the cube root of a number but it doesn't give any actual examples. It left me a little confused. Is there a website that any of you are familiar with where it shows the code for this and other basic programs for me to quickly refer to when I get stuck on a particular problem? I googled "cube root calculator" but it didn't return much. I don't want to continue reading forward until I get the hang of this problem.

mdbrock7
Newbie Poster
17 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

IIRC:

pow ( abs ( val ), 1.0 / 3.0 )
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks for your help but with me being such a C++ idiot it would greatly help me to see the entire program from start to finish. I'm still getting the hang of loops and it would be nice to see the while loops referred to in the book. I'm trying to write it out myself but I'm not having much luck. I'll keep searching. Thanks again

mdbrock7
Newbie Poster
17 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

>it would greatly help me to see the entire program from start to finish

#include <cstdlib>
#include <cmath>
#include <iostream>

int main()
{
  int val;

  std::cout<<"Enter a number: ";
  
  if ( std::cin>> val ) {
    std::cout<<"Cube root of "<< val <<": "
      << std::pow ( std::abs ( val ), 1.0 / 3.0 ) <<std::endl;
  }
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Holy cow! That one is more advanced than I am. I was trying to follow the book and mine looks like this. It doesn't work yet but I'm still working on it. Can you tell me if I am even close?

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
  double x;
  double a = 1;
  double b;
  char c;

  c = ' '; 

  while (c != 'q')
  {
    cout << "Cube Root Calculator" << endl;
    cout << endl;
    cout << "Enter a number: ";
    cin >> x;

    while (x - (a)(a)(a)) < 0.001)
    {
      a = b;
    }

    cout << "Answer is: " << fixed << showpoint << setprecision(5) << a << endl;
    cout << endl;
    cout << "Press c to continue, q to quit" << endl;
    cin >> c;
  }

  cout << "Exiting Cube Root Calculator" << endl;
  cout << endl;

  return 0;
}

Code reformatted and tags added. -Narue

mdbrock7
Newbie Poster
17 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

>That one is more advanced than I am
Aroo? And then you post a manual attempt at it? :eek:

>while (x - (a)(a)(a)) < 0.001)
(a)(a)(a) likely doesn't do what you think it does. ;) Perhaps you were trying to do:

while (x - (a * a * a)) < 0.001)

>a = b;
b doesn't have a value yet. You'll probbly get strange results.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
b doesn't have a value yet. You'll probbly get strange results.

What should i do with b?

mdbrock7
Newbie Poster
17 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

>What should i do with b?
How should I know, it's your program. ;) You put b there for a reason, right?

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I guess I need to come up with a formula for computing b.
This could take a while :-|

mdbrock7
Newbie Poster
17 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

Does my code make sense to you at all?
It has really got me confused. Can you make any suggestions on how to finish it?

mdbrock7
Newbie Poster
17 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

I think I got it.

#include <iostream>
#include <iomanip>
#include <math.h>

using namespace std;

int main()
{
	double x;
	double a;
	char c;

	c = ' '; 

	while (c != 'q')
	{
		cout << "Cube Root Calculator" << endl;
		cout << endl;
		cout << "Enter a number: ";
		cin >> x;

		a = 1;
		while (fabs(x - (a*a*a)) >= 0.001)
		{
			b = (2*a+x/(a*a))/3;
			a = b;
		}

		cout << "Answer is: " << fixed << showpoint << setprecision(5) << a 
<< endl;
		cout << endl;
		cout << "Press c to continue, q to quit" << endl;
		cin >> c;
	}

	cout << "Exiting Cube Root Calculator" << endl;
	cout << endl;

	return 0;
}


<< moderator edit: added [code][/code] tags >>

mdbrock7
Newbie Poster
17 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

I decided to name the variables to make it easier to understand if I want to look back at this sometime in the future and to help my brother understand it. I named x (userNumber) and c (programRepeat). What would be a good name for a and b?

int main()
{
double userNumber;
double a;
double b;
char programRepeat;

c = ' ';

while (programRepeat != 'q')
{
cout << "Cube Root Calculator" << endl;
cout << endl;
cout << "Enter a number: ";
cin >> userNumber;

a = 1;
while (fabs(x - (a*a*a)) >= 0.001)
{
b = (2*a+userNumber/(a*a))/3;
a = b;
}

mdbrock7
Newbie Poster
17 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

I changed b to calCubeRoot. Would these variable names make sense to you if you were trying to understand this program?
I'm also still trying to come up with a good name for a.

mdbrock7
Newbie Poster
17 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You