SQRT Function without math.h header C?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 955 Views - Last Post: 07 October 2013 - 09:01 PM Rate Topic: -----

#1 DroppedPJK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-October 13

SQRT Function without math.h header C?

Posted 07 October 2013 - 12:09 AM

I honestly thought my homework would be easy but I have a huge issue.

http://en.wikipedia...._root_algorithm

I do not remotely understand how to implement Newton's Method for finding Nth SQRT,
because I have a lack of understanding of it and I do not know how to turn a concept in C coding.

I am in serious need of help, PLEASE NOTE that the problem is that I do not know where to start and I am beginning C student in a class. (Which means I have a lack of knowledge for many functions of C except the ones that were given to me.)

#include <stdio.h>

int main()
{
	int n, CHECK;
	double A, root;

	printf("Enter real number and integer (A n): ");
	CHECK = scanf("%lf %d", &A, &n);

	if ( !(A >= 0) || !(n >= 2) || !(CHECK == 2)){
		printf ("There is an error in your input for A or n \n");	
	}

	else {

		root = 



That is how far I've gotten, I cannot go any further because I do NOT understand how the hell to translate Newton's Method to code. Theoretically I could do the work on paper but
THAT DOES NOT MEAN I UNDERSTAND WHAT MY MATH ON PAPER MEANS IN C CODE because I just don;t, there isn't a sort of book on how to do that either.

Is This A Good Question/Topic? 0
  • +

Replies To: SQRT Function without math.h header C?

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 12:51 AM

I can't help you much, because I did most of my drinking at home ;)/>/>, but I looked at senor Newton's method.

It's iterative - so you will use one or more loops to continually make progress toward the answer, until you've reached the accuracy you want.

Think while or do while loop, or for loop, instead of a single if statement, without a loop.

The (dumb) way I've done it in the past, is to simply start with one . Then enter the loop, and use your if statement in there.

For example:

N=100.00
sqroot=1

while(sqroot * sqroot < N)
sqroot+=1.0;

Which quickly moves up near the value. Then repeat with > root and -0.001 to fine tune it closer to the real sqr root. (The value when it leaves the while loop above, will be too high.)

But that's not Newton's method - more like Caveman's method.

This post has been edited by Adak: 07 October 2013 - 01:19 AM

Was This Post Helpful? 0
  • +
  • -

#3 DroppedPJK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-October 13

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 01:08 AM

View PostAdak, on 07 October 2013 - 12:51 AM, said:

I can't help you much, because I did most of my drinking at home ;)/>, but I looked at senor Newton's method.

It's iterative - so you will use one or more loops to continually make progress toward the answer, until you've reached the accuracy you want.

Think while or do while loop, or for loop, instead of a single if statement, without a loop.

The (dumb) way I've done it in the past, is to simply start with a guess, maybe N/10. Then enter the loop, and use your if statement in there.

For example:

N=100.00
sqroot=1

while(sqroot * sqroot < root)
sqroot+=1.0;

Which quickly moves up near the value. Then repeat with > root and -0.001 to fine tune it closer to the real sqr root. (The value when it leaves the while loop above, will be too high.)

But that's not Newton's method - more like Caveman's method.


I'm looking at your example and I'm abit confused about the N/10? Is that what root is equal to? If it isn't when would it leave the loop?

Also, I understand loop functions and do-whiles and what not, just this method is kicking me so hard in the ass because I just can't translate it to C code.
Was This Post Helpful? 0
  • +
  • -

#4 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 01:17 AM

That was something that the Newton method did - start with a guess.

Poor guy never had a computer! No need for it. Just start with 1 and you'll be up there near the square root, in the blink of an eye.

I've edited that part of my earlier post.

Sorry Newton, you're good, but obsolete.

That isn't C code, but it's VERY close to it. I was trying to get you to see that "root" was really N, but that's just too Caveman confusing.

So I edited that, also.

Think of what a square root IS, and you'll get it, pronto.

This post has been edited by Adak: 07 October 2013 - 01:21 AM

Was This Post Helpful? 0
  • +
  • -

#5 DroppedPJK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-October 13

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 01:23 AM

View PostAdak, on 07 October 2013 - 01:17 AM, said:

That was something that the Newton method did - start with a guess.

Poor guy never had a computer! No need for it. Just start with 1 and you'll be up there near the square root, in the blink of an eye.

I've edited that part of my earlier post.

Sorry Newton, you're good, but obsolete.

That isn't C code, but it's VERY close to it.

I'm on a little of a BREAK THROUGH right now but I am stuck on how to write a specific thing in C.

Remember where it says A / X (N - 1) (K), where N -1 = upper script and K = subscript.
How do I write that in C?

I'm so close to actually writing this equation but I don;t know how to write that particular part in C because I know xk is a variable and can be whatever but I can't express power of n-1 in C.
Was This Post Helpful? 0
  • +
  • -

#6 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 01:28 AM

What does power MEAN?

What is X squared? X cubed?

Now what is
X to the power of n-1?

Remember, you're able to LOOP, LOOP, LOOP, in C.
Was This Post Helpful? 0
  • +
  • -

#7 DroppedPJK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-October 13

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 01:32 AM

View PostAdak, on 07 October 2013 - 01:28 AM, said:

What does power MEAN?

What is X squared? X cubed?

Now what is
X to the power of n-1?

Remember, you're able to LOOP, LOOP, LOOP, in C.


Now I'm braindead, yes I understand that I must loop this until it meets a required precision of some sort.

However I cannot begin to loop if I don't have a working equation for Newton's Method.
I LITERALLY do not know how to write X to the power of n -1 in C, serious I really don't. I have NEVER EVER in my life used exponents once on C. I'm not sure if it is as simple as writing x ^ n -1 in C.
Was This Post Helpful? 0
  • +
  • -

#8 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 01:41 AM

Could you write out X squared in C? Could you write out X cubed in C?

Let me know when to stop.
Was This Post Helpful? 0
  • +
  • -

#9 DroppedPJK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-October 13

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 01:47 AM

View PostAdak, on 07 October 2013 - 01:41 AM, said:

Could you write out X squared in C? Could you write out X cubed in C?

Let me know when to stop.


I don't think I could.

I mean if persay x^3, then I could say X * X * X. This makes me feel like I actually have to write out the calculation for exponents.

Since I cannot just write x^(n-1) in C, I would have to be like

if expression = ^ then
total = X *.... where ..... = (n-1) and that would correspond to how many X's to multiply out.

but that seems like a crap ton of work just for the bottom part of one fraction.
Was This Post Helpful? 0
  • +
  • -

#10 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 02:01 AM

Quote

I mean if persay x^3, then I could say X * X * X. This makes me feel like I actually have to write out the calculation for exponents.


Ding! Ding! Ding!

It's two lines of code, in a for loop. Without using math.h, you pretty much have to iterate through these things manually, don't you?
Was This Post Helpful? 1
  • +
  • -

#11 DroppedPJK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-October 13

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 02:19 AM

View PostAdak, on 07 October 2013 - 02:01 AM, said:

Quote

I mean if persay x^3, then I could say X * X * X. This makes me feel like I actually have to write out the calculation for exponents.


Ding! Ding! Ding!

It's two lines of code, in a for loop. Without using math.h, you pretty much have to iterate through these things manually, don't you?


 else {
		i = 2; //Initialize POWER
		x0 = 1; // Initial Guess
		x1 = x0;
		do { 
			POWER = x1;
			for(i=2; i<=n; i++) {
				POWER = x1*x1
			} 



That is just a part of it, but would that work? This is only a part of the guess but generally I want it to say i is already set to 2, therefore for some number n, that loop will run until i = n and therefore change POWER by multiply by x1 everytime.
Was This Post Helpful? 0
  • +
  • -

#12 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 02:39 AM

You want i<n, not i<=n.

Not variable = x * x - that would give you the square of x only.

You want:

Powervariable *= variable

2 * 2
4 * 2
8 * 2

for 2 cubed.
Was This Post Helpful? 1
  • +
  • -

#13 DroppedPJK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-October 13

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 03:00 AM

View PostAdak, on 07 October 2013 - 02:39 AM, said:

You want i<n, not i<=n.

Not variable = x * x - that would give you the square of x only.

You want:

Powervariable *= variable

2 * 2
4 * 2
8 * 2

for 2 cubed.


POWER = x1;
			for(i=2; i<n; i++) {
				POWER = POWER*x1;
			}


Right right I fixed it when I noticed the two issues myself!
My teacher threw an odd ball at me, we have to check for the Absolute value of the new value minus the old value but tells us we cannot use the ABS function (Because god knows why)

Anyways this is what everything looks like so far,
		i = 2; //Initialize POWER
		x0 = 1; // Initial Guess
		x1 = x0;
		PR = 0.000001;
		
		POWER = x1;
		for(i=2; i<n; i++) {
				POWER = POWER*x1;
			}

		do { 
			NewX = 1/n * ((n-1)*x1 + A/POWER);
			ABZ = (NewX - x1);
			if ( ABZ < PR){
				break;}
			else
				x1 = NewX;
		} while (ABZ < PR);



I don't know how to check the ABS value of NewX - x1, withotu the ABS function to be honest.
Was This Post Helpful? 0
  • +
  • -

#14 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 03:33 AM

Quote

(Because god knows why)


ROFL!

To find the ABS, difference between two values, just set the two values to positive values, (-N * -1 then), and subtract the smaller of the two values, from the larger of them. ABS values are always real numbers, always positive.

This post has been edited by Adak: 07 October 2013 - 03:34 AM

Was This Post Helpful? 0
  • +
  • -

#15 DroppedPJK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 06-October 13

Re: SQRT Function without math.h header C?

Posted 07 October 2013 - 03:47 AM

View PostAdak, on 07 October 2013 - 03:33 AM, said:

Quote

(Because god knows why)


ROFL!

To find the ABS, difference between two values, just set the two values to positive values, (-N * -1 then), and subtract the smaller of the two values, from the larger of them. ABS values are always real numbers, always positive.


Ok I'm not sure if the ABS part if the reason why this won't work properly. Could you check if my coding is right for Newton's method?

else {
		i = 2; //Initialize POWER
		x0 = 1; // Initial GUESS
		x1 = x0;
		PR = 0.000001;
		
		POWER = x1;
		for(i=2; i<n; i++) {
				POWER = POWER*x1;
			}

		do { 
			NewX = 1/n*((n-1)*x1+A/POWER);
			ABZ = (NewX-x1);
			if (ABZ < PR){
				break;}
			else
				x1 = NewX;
		}while (ABZ > PR);

	printf("Given A = %.2lf and n = %d, root = %.2lf \n", A, n, NewX);
	}



No matter what I input, NewX = 0.00 and that clearly means that either the ABS function is necessary or my code for newton's method is inherently wrong.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2