944,054 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 17626
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 25th, 2005
0

Cube Root Calculator

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mdbrock7 is offline Offline
17 posts
since Apr 2005
Apr 25th, 2005
0

Re: Cube Root Calculator

IIRC:
C++ Syntax (Toggle Plain Text)
  1. pow ( abs ( val ), 1.0 / 3.0 )
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 25th, 2005
0

Re: Cube Root Calculator

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mdbrock7 is offline Offline
17 posts
since Apr 2005
Apr 25th, 2005
0

Re: Cube Root Calculator

Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Apr 25th, 2005
0

Re: Cube Root Calculator

>it would greatly help me to see the entire program from start to finish
C++ Syntax (Toggle Plain Text)
  1. #include <cstdlib>
  2. #include <cmath>
  3. #include <iostream>
  4.  
  5. int main()
  6. {
  7. int val;
  8.  
  9. std::cout<<"Enter a number: ";
  10.  
  11. if ( std::cin>> val ) {
  12. std::cout<<"Cube root of "<< val <<": "
  13. << std::pow ( std::abs ( val ), 1.0 / 3.0 ) <<std::endl;
  14. }
  15. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 25th, 2005
0

Re: Cube Root Calculator

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?
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. double x;
  10. double a = 1;
  11. double b;
  12. char c;
  13.  
  14. c = ' ';
  15.  
  16. while (c != 'q')
  17. {
  18. cout << "Cube Root Calculator" << endl;
  19. cout << endl;
  20. cout << "Enter a number: ";
  21. cin >> x;
  22.  
  23. while (x - (a)(a)(a)) < 0.001)
  24. {
  25. a = b;
  26. }
  27.  
  28. cout << "Answer is: " << fixed << showpoint << setprecision(5) << a << endl;
  29. cout << endl;
  30. cout << "Press c to continue, q to quit" << endl;
  31. cin >> c;
  32. }
  33.  
  34. cout << "Exiting Cube Root Calculator" << endl;
  35. cout << endl;
  36.  
  37. return 0;
  38. }
Code reformatted and tags added. -Narue
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mdbrock7 is offline Offline
17 posts
since Apr 2005
Apr 25th, 2005
0

Re: Cube Root Calculator

>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:
C++ Syntax (Toggle Plain Text)
  1. while (x - (a * a * a)) < 0.001)
>a = b;
b doesn't have a value yet. You'll probbly get strange results.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 25th, 2005
0

Re: Cube Root Calculator

Quote originally posted by Narue ...
b doesn't have a value yet. You'll probbly get strange results.
What should i do with b?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mdbrock7 is offline Offline
17 posts
since Apr 2005
Apr 25th, 2005
0

Re: Cube Root Calculator

>What should i do with b?
How should I know, it's your program. You put b there for a reason, right?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 25th, 2005
0

Re: Cube Root Calculator

I guess I need to come up with a formula for computing b.
This could take a while :-|
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mdbrock7 is offline Offline
17 posts
since Apr 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: VB's Left, Right, Mid Functions in C++?
Next Thread in C++ Forum Timeline: big numbers in C++ 5.4455e07





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC