2nd/3rd/4th Root calculator, using loops

I am working on a homework assignment for a Beginning C++ course. I'm using Microsoft Visual C++ 2008 (required software for the course).

I have to write a program that can take any number, and calculate the square, cube, or fourth root, accurate to three decimal places. I am not allowed to use #include <cmath>. It needs to be done using only loops (for or while) and switches. The instructor wants me to define the function in a header file, and call it in the main calling program.

He doesn't want us to use cin (even though I already know how); instead, he will insert whichever numbers he wants in our source code and then run the program.

I have a header file named "Roots.h" and the main calling program called "Project07.cpp"

Here is my code so far:

Roots.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "stdafx.h"
#include <iostream>
#include <iomanip>
using namespace std;

void RootCalc(double Number, int RootValue)
{
    double RootOfNumber;
            switch(RootValue)
	    {
	        case 2:
	         	    for (RootOfNumber = 0; RootOfNumber < Number; RootOfNumber += .001)
		            {
		                    if (RootOfNumber*RootOfNumber==Number)
			    	    {
					    cout << "The square root of " << Number 
                                                 << "accurate to three decimal places is " 
                                                 << RootOfNumber;
				    }
			    }
		break;
		case 3:
			    for (RootOfNumber = 0; RootOfNumber < Number; RootOfNumber += .001)
		            {
		                if (RootOfNumber*RootOfNumber*RootOfNumber==Number)
			        {
			            	    cout << "The cube root of " << Number 
                                                 << "accurate to three decimal places is " 
                                                 << RootOfNumber;
				}
		            }
		break;
		case 4:
			    for (RootOfNumber = 0; RootOfNumber < Number; RootOfNumber += .001)
		            {
			        if (RootOfNumber*RootOfNumber*RootOfNumber*RootOfNumber==Number)
			        {
					    cout << "The fourth root of " << Number 
                                                 << "accurate to three decimal places is " 
                                                 << RootOfNumber;
			        }
			    }
	        break;
		default:
		         cout << "Please enter valid parameters!!!";
			 break;
		}	
}


Program07.cpp

1
2
3
4
5
6
7
8
9
10
11
#include "stdafx.h"
#include "Roots.h"
#include <iostream>

using namespace std;

int main()
{
	RootCalc(5,2);
	return 0;
}



So far, I've got it down to the point where the compiler gives me no errors or warnings, but the output I see is "Press any key to continue ... "

I can't figure out why it won't output anything else. I guess it could either be that I didn't include a crucial header file, or maybe it's something much more advanced than I am familiar with so far.

I hope I did a sufficient job of describing the problem and putting in enough effort before asking for help. Thanks in advance for any tips/pointers/advice/help!
Last edited on
I think it's because you are doing a floating point comparison in your if statements. It is highly unlikely that the your test root squared/cubed/cetera will be exactly equal to the number you are taking the root of. Generally, when comparing doubles you will want to take the difference and see if it is larger or smaller than some larger small ("epsilon") value.
Dude, thanks, thanks and more thanks Zhuge! That was a very simple mistake on my part, and now that you pointed that out, I see the flaw in my logic.

I just got the square root part working by changing

if (RootOfNumber*RootOfNumber==Number)

to

1
2
if (RootOfNumber*RootOfNumber < Number + .001 
&& RootOfNumber*RootOfNumber > Number - .001)


I'll do the same for the cube and fourth roots and see if it works.

I'm not exactly sure what this means:
Generally, when comparing doubles you will want to take the difference and see if it is larger or smaller than some larger small ("epsilon") value.

Is that basically what I just did? Or is there a more effiecient way to do it?

Again, thanks so much. You really helped me out.

EDIT: Actaully, it's not working ... it worked for the square of 5, but not 6. But still, thanks.
Last edited on
Yes, that is basically what you did. Though I've seen it done more often this way:
1
2
3
4
const double epsilon = 0.001;
if(abs(var1-var2) <= epsilon) {
    // consider them equal
}
Topic archived. No new replies allowed.