square root finder without using sqrt

I'm trying to write a function but it keeps telling me at the end of the function "control may reach end of non void function" what can i do to fix this?

[code]
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<iomanip>
using namespace std;
const double epsilon=1e-10;
double squareroot (double n) {
if(n<0) {
cout<<sqrt(n)<<endl;
}
double low, high;
if (n > 1)
{
low = 1;
high = n;
}
else
{ low = 0;
high = 1;}
double mid = low + (high - low)/2;
while (low <= high){
cout<<fixed;
cout<<setprecision(8)<<low<<"\t"<<setprecision(8)<<high<<"\t"<<setprecision(8)<<mid<<"\t"<<setprecision(8)<<mid*mid<<"\t"<<setprecision(8)<<(mid*mid-n)<<endl;
if (fabs(mid*mid-n)<epsilon){
return mid;}

else if (mid*mid < n){
low = mid;}
else {
high=mid;}
}
}
Hello aduco15,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

It makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



The function has one return statement buried in the function. This gave me only a warning which would still let the program run, but you might consider returning some just before the closing brace of the function.

The only error I received was about the missing "main" function.

Also a few blank lines would help readability.

I also noticed that your use of the {}s are not all the same. Be consistent it helps.

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
49
50
51
52
53
54
55
56
57
58
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<iomanip>

using namespace std;  // <--- Best not to use.

const double epsilon = 1e-10;

double squareroot(double n)
{
	if (n < 0)
	{
		cout << sqrt(n) << endl;
	}

	double low, high;

	if (n > 1)
	{
		low = 1;
		high = n;
	}
	else
	{
		low = 0;
		high = 1;
	}

	double mid = low + (high - low) / 2;

	while (low <= high)
	{
		//std::cout << std::fixed << std::showpoint << std::setprecision(8);// <--- You may fine this works better.
		// <--- above only needs to be done once.

		cout << fixed;
		// <--- Setpresicion not need before every variable unless the size changes.
		cout << setprecision(8) << low << "\t" << setprecision(8) << high << "\t" << setprecision(8) << mid << "\t" << setprecision(8) << mid * mid << "\t" << setprecision(8) << (mid*mid - n) << endl;

		if (fabs(mid*mid - n) < epsilon)
			return mid;

		else if (mid*mid < n)
			low = mid;
		else
			high = mid;
	}
}

int main()
{
	double n{ 9 };

	double aVariable = squareroot(n);

	return 0;
}

Look at the while loop in the function. "mid" is set before the loop, but never changes in the loop. This causes an endless loop.

The only return statement is not likely to be reached and the whole function as is does not achieve the square root of "n".

I need to do some research on the square root part. I will let you know what I find.

Hope that helps,

Andy

the smart * in me says
pow(x, 0.5);
:P

otherwise there is enough here
https://en.wikipedia.org/wiki/Methods_of_computing_square_roots
to manage it. The digit by digit one "looks" on paper like it would yield some cute, tight code for your teacher's amusement.
Last edited on
Topic archived. No new replies allowed.