've created this code to find Sqrt without math.h but gives me wrong values ! So what is wrong ??

#include <iostream.h>
#include <conio.h>

main()
{
double n,end,start=0,mid;

cin>>n;
end=n/2;

while(start<end)
{
mid=(start+end)/2;

if ((n-(mid*mid)<=0.001) && (n-(mid*mid)>=0))

{cout<<mid;break; }

if(n>mid*mid)
{start=mid+0.001 ;
end=end;}

if(n<mid*mid)
{end=mid-0.001;
start=start;}


}

getch();
}

what is wrong ??

#include <iostream.h>

Back in 1998, the C++ standard came in and iostream.h definitely went out. If your compiler accepts this, throw it away and get one from this century.

#include <conio.h>
This is non-standard in so many ways, and many C++ compiler installations don't have it at all. Don't use it.

main()
That's not C++. Use int main()

if(n-(mid*mid)<=0.001)
This is just wrong. Here, your code is basically testing that n is less than mid*mid. That's not what you care about. You want to know when they're basically the same value. You should be testing that n and mid*mid are within 0.001 of each other.

end=end;
This is pointless. Why is it here?

start=start;
This is pointless. Why is it here?

getch();
Non-standard. There are much better ways to keep the console window from closing. The easiest way is to actually run your program from the command line.

firstly main() is C++ but i've decleared of my variables in the next line :) ..secondly s=s and e=e is not important but not the Problem ,also getch(); not our problem here .. i wanna know what is the wrong in calcu Sqrt ??

So you know there's something wrong, could you tell us what is wrong?

firstly main() is C++

No, it is not. In C++, main() returns an int. Always.

i wanna know what is the wrong in calcu Sqrt ??

Move your cout << mid to the end (line 29). Where it is right now makes no sense and it may never be reached. Did you edit your code already?

firstly main() is C++

Actually, no, it isn't, at least not without the return type. In C++, you cannot define a function without explicitly defining it's return type. Since the standard is for main() to return an int value to the operating system shell, the correct forms are either int main() or int main(int argc, char* argv[]). Since you aren't taking any command line argumwents, you should use int main().

So, let's re-cast the program in modern C++, shall we?

#include <iostream>
using namespace std;

int main()
{
    double n,end,start=0,mid;

    cin>>n;
    end=n/2;

    while(start<end)
    {
        mid=(start+end)/2;

        if ((n-(mid*mid)<=0.001) && (n-(mid*mid)>=0))
        {
            cout<<mid;break;
        }

        if(n>mid*mid)
        {
            start=mid+0.001;
            end=end;
        }

        if(n<mid*mid)
        {
            end=mid-0.001;
            start=start;
        }
    }

    return 0;
}

Now that we can actually read and make sense of it, what problems are you having with it?

The test in the code is only that n is greater than mid*mid by up to 0.001, when it should be that it's greater or less than mid*mid by up to 0.001

For this reason, line 17 in Schol's code can be skipped and the program calcualtes the answer and never displays it.

@Schol-R-LEA my Problem is int this condition ..

if ((n-(mid*mid)<=0.001) && (n-(mid*mid)>=0))

How many times do we have to tell you what you're doing wrong? Do we have to actually write out the complete code for you?

Your code does actually work. It does actually calcualte a square root to within the error you have set. It works. You just aren't outputting the answer.

am sorry but can't understand me and what am trying to say .. u told me that the wrong in

main()

and it should be written

int main()

but u didn't see that am not writing in the last line

return0; 

so i don't have to wrire it and i've tried this way many times with different prog and there is no wrong with it ..

but when i tried this prog .. it doesn't give me true results .. so i wonder what is wrong ?? and how can i make it right ?

so i don't have to wrire it and i've tried this way many times with different prog and there is no wrong with it ..

Only because you are using an outdated and non-compliant compiler. A modern compiler that follows the standard wouldn't allow it. It isn't the cause of the problem you are experiencing, that's true; but it will trip you up eventually if you don't follow the standard.

but when i tried this prog .. it doesn't give me true results .. so i wonder what is wrong ?? and how can i make it right ?

The problem is in where you have the output, and more subtly, with some minor flaws in both the code and the algorithm. The primary problem is that you need to have an output statement for the case where the while() loop exits out.

(As it happens, there is a much faster and simpler algorithm around anyway. But that's another issue entirely.)

@Schol-R-LEA
thank u 4 Helping

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.