How to do Square Roots in C++ - WITHOUT "SQRT" Command

Text-only Version: Click HERE to see this thread with all of the graphics, features, and links.



JKozzy
K, so this is what we have so far (CompSci class)


The first two parts seem to work fine, though the third part seems to display the integer4 from the first part of the program. I know I'm missing something, if anyone can help out that has any experience, I (and my group) would be most appreciative.

**We can't use the SQRT function, as we haven't 'learned' this yet.**

jerlark386
Originally posted by JKozzy
K, so this is what we have so far (CompSci class)


The first two parts seem to work fine, though the third part seems to display the integer4 from the first part of the program. I know I'm missing something, if anyone can help out that has any experience, I (and my group) would be most appreciative.

**We can't use the SQRT function, as we haven't 'learned' this yet.**

LoL! I think you have the wrong forums. Secondly, what is your program supposed to do? If its just body fat vs mass calculations, maybe you don't even need sqrt.

Its funny how a computer programmer can't even use google.

There's you're sqrt without sqrt, my friend. It should only require slight modification. I highly recommend you put it into its own class. Have fun.

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


void main(){
long double num,div,rem,tmp,ans,a;
int len,x,sng,x1;
long pw;
char ch;
num=0;
div=0;
tmp=0;
a=0;
ans=0;
rem=0;
len=0;
pw=0;
x=0;
x1=0;
sng=10;
ch=0;

cout << "Please enter the squared number \n";
cin >> num;
for(x1=0;(num*pow(10,x1))!=((long) (num*pow(10,x1)));x1++);
if(((float)x1/2.0)!=(int) (x1/2)) x1++;
num=num*pow(10,x1);
ch=x1/2;
cout << x1 << endl;



for(x1=0;sng!=0;x1++){

sng= (int) (num/(pow(10,x1)));
}
len=x1-1;

tmp=(float) len/(2.0);
if (tmp==(len/2))
pw=len-2;
else
pw=len-1;

div=(int) (num/pow(10,pw));
for(a=0;(div-(a*a))>=0;a++);
ans=a-1;
rem=div-(ans*ans);
a--;
cout << "First=" << a << "\n";
if(pw==0)
goto done;
Second:
tmp=(int) ((num/pow(10,pw)))*pow(10,2);
pw-=2;
div= (rem*100)+(int) (num/pow(10,pw)) - (tmp);
a=ans*20;
for(x=1;(div-(a*x)-(x*x))>=0;x++);
x--;
cout << "X=" << x << endl;
ans*=10;
ans+=x;
rem=div-(a*x)-(x*x);
a+=x;


if(pw==0) {
goto done;
}
goto Second;
done:
ans=ans*pow(10,0-ch);
pw=0-ch;
cout << "Sqr Root =" << ans << " with remainder =" << rem << endl;
if (rem==0)
goto perfect;
cout << "To how many digits of decimal do you wish the result to be?" << endl;
cin >> tmp;
cout << endl;
cout << "Quotient=" << ans;
if((ans*10)==((long) (ans*10))) cout << ".";


while (rem!=0){
pw--;
div=rem*100;
a=ans*20*pow(10,0-pw-1);;
for(x=1;(div-(a*x)-(x*x))>=0;x++);
x--;
cout << x;
ans+=(x*pow(10,pw));
rem=div-(x*a)-(x*x);
a+=x;
if(pw==(0-tmp))
goto enough;
}
goto perfect;
enough:
cout << endl << "Sqr Root =" << (long double) ans << endl;
perfect:
getch();
cout << "End" << endl;
}

Illustrious
Use the Code tags. That way they don't lose all their formatting.

JKozzy
A) This is the Computer/Video Games discussion, therefore this is the correct forum.

B) I've used google and all I've found for the square root is stuff that we haven't learned, thus is rendered useless, since we're not allowed to use it, much like the rest of what you've posted. I'm talking simple simple C++. The program's split into three parts. Part 1 calculates the average grade. Part 2 calculates the body mass index. Part 3 is supposed to act as the Pythagorean Theorem.

C) KMC doesn't have any code tags that I'm aware of.

Mainly, this is what I have an issue with:



since integer3=(integer4*integer4);//finds hypotenuse doesn't seem to work.

jerlark386
If this is Pythagorean's Theorem should'nt it be

cout <<"The hypotenuse of the right triangle is "<<integer3<<endl;


instead of


cout <<"The hypotenuse of the right triangle is "<<integer4<<endl;


If integer3 is storing the return c^2 is'nt that the value you want printed?

To validate my previous about you having the wrong forum.

Video games make up a majority of discussion here. The odds of you finding a helpful programmer in a video game forum on a site thats dedicated to movies is'nt very high. In fact this is probably the second and probably last topic ever about C++ for some years to come. Just look at the context.

Thats how you solve your problems.

You think I know even a drop of C++ code?

hotsauce6548
What does this code do, exactly?

Lana
The forum description says it's for discussion of all types of video games and computers. Computer discussion used to be very common in here, it isn't so much anymore but that doesn't mean a thing. This is indeed the right forum.

jerlark386
Originally posted by Lana
The forum description says it's for discussion of all types of video games and computers. Computer discussion used to be very common in here. This is indeed the right forum.

Not really. Ofcourse its description says 'and Computer discussion' but that doesn't change the fact that this forum is a sore-thumb, an eye sore. The rules technically allow it, but it really doesn't belong here. Its out of place. This is just some guy trying to impress us or strain our eyes looking at this block. Bah, I say let his own damn homework. Who comes to this forum looking for C++ code, poorly written code at that.

First of all,

what better way to confuse yourself than name a variable 'integer' thats actually a float?


float integer1, integer2, integer3, integer4, integer5, integer6




integer6=integer1+integer2+integer3+integer4+inte


What is this stray, half done variable, I see? Inte?!

Are you hoping your compiler has spell check?

laughing @ you.

What is with using the same variable for two completely different functions? Speaking of functions, have you ever heard of them?



cout<<"Enter fourth grade: \n";//prompt for fourth grade
cin>>integer4


and now, all of a sudden


integer3=(integer4*integer4);//finds hypotenuse
cout <<"The hypotenuse of the right triangle is "


all of this in the same block of code?
wtf?

You want us to suffer don't you , Jkozzy? Is that why you posted this? On behalf of C++, I feel embarrassed. And I thought I was terrible.

Not to slay your hopes or anything, but there's enough C++ programmers out there that can write better programs for you and you don't have to get in nitty, gritty of things.

My best advice to you is to quit programming and start cheating. Which you are already doing, to a degree.

JKozzy
Whoa. I asked for a simple answer, not for a flame no expression

This was originally the Computer Discussion Forum, until Video Games were tacked on. I don't need spellcheck, as you can see the formatting split the word up. I've been in the course for maybe, say, four days?

integer6=integer1+integer2+integer3+integer4+inte
ger5;//assignment for sum of all grades

Hey, look, it continued on the next line because of pasted formatting. I don't need spellcheck, you need glasses.

The same variable can easily be used, as long as it is restated. As you say you haven't had a drop of C++, you're hardly one to criticize.

The variable 'integer' was pasted over from a previous program, and I didn't bother changing it, sorry if it confused you, but I'm fine with it.

I'm under the circumstances of this being a school assignment and I'm merely being told to do it in one huge program, not my choice.

I would sure hope there were better C++ programmers, because with not even a week's experience, the world wouldn't be very far at all, would it?

Asking advice isn't cheating. We're free to copy each other's programs; much simpler and productive. You obviously haven't taken a coding course, so I'm not sure why you bothered responding in the first place.

My best advice to you would be to understand the parameters of the situation before posting, or just don't post. You're wasting your and my time.

jerlark386
Originally posted by JKozzy

My best advice to you would be to understand the parameters of the situation before posting, or just don't post. You're wasting your and my time.

You still have no answer and you're posting your question in the least helpful place on the web. Don't tell me I need glasses just because I can't read your techno garble.

You're the one wasting your time. You'll find no answers here. And noone wants to waste their time doing your homework for you.

JKozzy
Originally posted by jerlark386
You still have no answer and you're posting your question in the least helpful place on the web. Don't tell me I need glasses just because I can't read your techno garble.

You're the one wasting your time. You'll find no answers here. And noone wants to waste their time doing your homework for you. Yeah, you do need glasses. If you're gonna say you read the techno garble, well, then read the damn thing, and don't leave half of it out in a quote, criticizing that it's incomplelte when it clearly wasn't. Seriously, why do you keep coming back? Do you really not have a life that you need to think up dribble to put people down on the internet? Seems those glasses really would help you; I asked you that if you can't help, to not post. This is a forum, not an all-out flame war on a subject you clearly don't understand.

Originally posted by jerlark386

You think I know even a drop of C++ code? No, so go back to the OTF.

jerlark386
You think this is flame war? I was just commenting on your block. Its not like, I did a full-out assault on you. Just relax, play some video-games or something. You don't really have to quit just because I told you.

If it makes you feel any better, I think you're the best programmer in the whole of KMC.

Congragulations!

JKozzy
No, but you certainly seem to want to make it that. You're not contributing an answer, so I'd appreciate it if you would let other people post. Originally posted by jerlark386
Congragulations! And learn to spell.

jerlark386
Originally posted by JKozzy
No, but you certainly seem to want to make it that. You're not contributing an answer, so I'd appreciate it if you would let other people post. And learn to spell.

Originally posted by JKozzy
Yeah, you do need glasses. If you're gonna say you read the techno garble, well, then read the damn thing, and don't leave half of it out in a quote, criticizing that it's incomplelte when it clearly wasn't. Seriously, why do you keep coming back? Do you really not have a life that you need to think up dribble to put people down on the internet? Seems those glasses really would help you; I asked you that if you can't help, to not post. This is a forum, not an all-out flame war on a subject you clearly don't understand.

No, so go back to the OTF.

Why don't YOU learn how to spell?

If any thing I'm helping this silly thread, because otherwise it would probably be pushed to the second page(or nearly) by now. Don't you know when someone is helping you? I bet you have'nt even tried out any of my suggestions yet.

JKozzy
Originally posted by jerlark386
Who comes to this forum looking for C++ code, poorly written code at that.

what better way to confuse yourself than name a variable 'integer' thats actually a float?


What is this stray, half done variable, I see? Inte?!

Are you hoping your compiler has spell check?

laughing @ you.

What is with using the same variable for two completely different functions? Speaking of functions, have you ever heard of them?

all of this in the same block of code?
wtf?

You want us to suffer don't you , Jkozzy? Is that why you posted this? On behalf of C++, I feel embarrassed. And I thought I was terrible.

Not to slay your hopes or anything, but there's enough C++ programmers out there that can write better programs for you and you don't have to get in nitty, gritty of things.

My best advice to you is to quit programming and start cheating. Which you are already doing, to a degree. Originally posted by jerlark386
Why don't YOU learn how to spell?

If any thing I'm helping this silly thread, because otherwise it would probably be pushed to the second page(or nearly) by now. Don't you know when someone is helping you? Yeah, I know how to spell, though. You can keep that advice, you'll need it. And, wow, uh, gee, thanks for all that... help. Yeah, laughing @ you. That definately did quite a doozy on the program. You know, maybe I'm wrong. You've been such a valuable asset in your vast knowledge of the C++ language. The sarcasm really gave it a nice touch, too. Yeah. Actually, I do know when someone's helping me. And, uh, you don't really fit that description.

ArthasKnight
Originally posted by jerlark386
Why don't YOU learn how to spell?

If any thing I'm helping this silly thread, because otherwise it would probably be pushed to the second page(or nearly) by now. Don't you know when someone is helping you? I bet you have'nt even tried out any of my suggestions yet.

Hey, why don't you stop being an ass? You've done nothing but troll around in this thread and your so-called 'help' that you keep referring to has yet to be located. He asked a question and if you don't know the answer, instead of being a smartass, why don't you just keep your mouth shut?

Oi.

Anyway, I have no idea man. I tried a mess of different ways to work around it and couldn't find a way to do it.

jerlark386
Originally posted by ArthasKnight
Hey, why don't you stop being an ass? You've done nothing but troll around in this thread and your so-called 'help' that you keep referring to has yet to be located. He asked a question and if you don't know the answer, instead of being a smartass, why don't you just keep your mouth shut?

Oi.

Anyway, I have no idea man. I tried a mess of different ways to work around it and couldn't find a way to do it.

Well I guess we have something in common then.

We both don't have the answer and theres nothing but love in our posts here so far.

But I actually did post one helpful suggestion.


cout <<"The hypotenuse of the right triangle is "<<integer4<<endl;


seems more like it should be


cout <<"The hypotenuse of the right triangle is "<<integer3<<endl;


I just look at it and thought that since their are no actual calculations are being stored in integer4 that maybe, just maybe integer3 was intended instead. Maybe. But if I'm wrong, feel free to shoot me down.

Storm
jerlark386, if you don' t have any interest in the topic, nor have anything worthwhile to contribute to the discussion, don' t post for the sake of it and simply don' t participate.

jerlark386
I'm sorry. I was just messing with Jkozzy's mind.

cout <<"Enter the first side of the right triangle\n";//assign first side
cin >> integer1;//redefine integer1
cout <<"Enter the second side\n";//assign second side
cin >> integer2;//redefine integer2
integer3=(integer1*integer1)+(integer2*integer2);
//finds hypotenuse squared
integer3=(integer4*integer4);//finds hypotenuse
cout <<"The hypotenuse of the right triangle is "<<integer4<<endl;

return(0);//indicate that the program has ended successfully
}



Its a really minor error. You can fix it with this.


cout <<"Enter the first side of the right triangle\n";//assign first side
cin >> integer1;//redefine integer1
cout <<"Enter the second side\n";//assign second side
cin >> integer2;//redefine integer2
integer3=(integer1*integer1)+(integer2*integer2);
//finds hypotenuse squared
//integer3=(integer4*integer4);//finds hypotenuse//get rid of this line
cout <<"The hypotenuse of the right triangle is the square root of"<<integer3<<endl;

return(0);//indicate that the program has ended successfully

}


So you can't use the sqrt or the breakdown version in my other post. Then don't. Without those, you're pretty much out of luck anyway. Besides your teach would'nt liked all those gotos anyway. If you give an answer that sounds good, then it'll probably be better than nothing. Your teacher will laugh, but your answer will be 'valid'.

Who does'nt love a good linguistic hack.

Happy Dance

hotsauce6548
I would help you, but alas, I can't. This is all German/French/Chinese/Japanese/Spanish to me. erm

JKozzy
Originally posted by jerlark386
I'm sorry. I was just messing with Jkozzy's mind.

cout <<"Enter the first side of the right triangle\n";//assign first side
cin >> integer1;//redefine integer1
cout <<"Enter the second side\n";//assign second side
cin >> integer2;//redefine integer2
integer3=(integer1*integer1)+(integer2*integer2);
//finds hypotenuse squared
integer3=(integer4*integer4);//finds hypotenuse
cout <<"The hypotenuse of the right triangle is "<<integer4<<endl;

return(0);//indicate that the program has ended successfully
}



Its a really minor error. You can fix it with this.


cout <<"Enter the first side of the right triangle\n";//assign first side
cin >> integer1;//redefine integer1
cout <<"Enter the second side\n";//assign second side
cin >> integer2;//redefine integer2
integer3=(integer1*integer1)+(integer2*integer2);
//finds hypotenuse squared
//integer3=(integer4*integer4);//finds hypotenuse//get rid of this line
cout <<"The hypotenuse of the right triangle is the square root of"<<integer3<<endl;

return(0);//indicate that the program has ended successfully

}


So you can't use the sqrt or the breakdown version in my other post. Then don't. Without those, you're pretty much out of luck anyway. Besides your teach would'nt liked all those gotos anyway. If you give an answer that sounds good, then it'll probably be better than nothing. Your teacher will laugh, but your answer will be 'valid'.

Who does'nt love a good linguistic hack.

Happy Dance integer3 is still C squared though.
Pythagoran Theorem is a squared plus b squared equals c squared. a and b are already squared, and the answer you show is c squared.... the square root of c needs to be found for the program to be complete erm

jerlark386
smile

Oh, is that all you wanted?


Simple math then

a squared + b squared = c squared

Then your program has show the root of c square.

My question is why square the first two values in the first place when,

a * b = your anwser that you want to square

So in that case, all you have to do is this


cout <<"Enter the first side of the right triangle\n";//assign first side
cin >> integer1;//redefine integer1
cout <<"Enter the second side\n";//assign second side
cin >> integer2;//redefine integer2
integer3=integer1 * integer2;
//finds hypotenuse squared
cout <<"The hypotenuse of the right triangle is"<<integer3<<endl;
return(0);//indicate that the program has ended successfully


Thats saves you alot of unneccessary coding. And the best part is it still generates the answer you want.

JKozzy
That wouldn't work. Take the 3-4-5 triangle.

3squared + 4squared = 5squared
9+16=25

If I input 3 as A and 4 as B, I want the program to spit out 5 as the hypotenuse.

What you have there is
3*4=5, which doesn't work, because 3*4=12.

Lana
Yeah, square roots don't work like that.

Hmmm. I cannot think of anyway to bypass that off the top of my head...

jerlark386
Originally posted by JKozzy
That wouldn't work. Take the 3-4-5 triangle.

3squared + 4squared = 5squared
9+16=25

If I input 3 as A and 4 as B, I want the program to spit out 5 as the hypotenuse.

What you have there is
3*4=5, which doesn't work, because 3*4=12.

Oh, too bad. I thought you were working with just right triangles. Then it looks like you will have to do a little extra writing. Just write a sub to check inputs for 3 and 4.

Text-only Version: Click HERE to see this thread with all of the graphics, features, and links.