LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2013, 08:25 PM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
CProgramming stuck on updating variable in loop


hey all. im working on a homework script so PLEASE do not give me exact code, just direction and explanations. Thank you.

I have a working bit of code that calculates the square root, yes i know there is a math.h that will do that, but this is the homework.

It properly calculates the root to 0.005, but it never stops. the loop just keeps going for ever. My issue is the updating of the variables. im a bit stuck. here is the code and ill explain what i want to happen, what is happening, and where im confused.

Code:
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<math.h>

int main(void)
{
	double N, NG, LG, *PLG, epsilon, root;	// setting all variables to type double

	epsilon = 0.005;			// setting error tolerance to 0.005
	LG = 1.0;				// setting initial guess value

	printf("\nPlease enter the number you wish to find the square root: ");
	scanf("%lf",&N);	// getting input from user
do	{
	NG = 0.5 * (LG + (N / LG));
	printf("%.2lf      %.3lf\n", N, NG);
	LG = NG;
	}	while (fabs(NG - LG) < epsilon);

	printf("\nThe square root of %.2lf is %.3lf\n", N, NG);
}
This works but never stops.

NG = next guess
LG = last guess with the initial value for the guess set to 1.

the formula NG = 0.5 * (LG + (N / LG)) works just fine. both on paper and in the computer.

My issue comes with making the result of NG become the new LG if the epsilon is not met:

NG - LG < 0.005 if this is false then set LG to the value calculated of NG, wash and repeat. This is the part im confused on. How to accomplish this section.

looking at http://www.cygnus-software.com/paper...ringfloats.htm

i read the following:

Quote:
Comparing with epsilon – absolute error
Since floating point calculations involve a bit of uncertainty we can try to allow for this by seeing if two numbers are ‘close’ to each other. If you decide – based on error analysis, testing, or a wild guess – that the result should always be within 0.00001 of the expected result then you can change your comparison to this:
if (fabs(result - expectedResult) < 0.00001)

The maximum error value is typically called epsilon.
i was just useing the following:

NG - LG <= epsilon this did not function the way i wanted either. Again i think it has to do with how to make LG the value of the results to the NG formula.

Thanks in advance from lost, dazed, and confused.
 
Old 07-18-2013, 10:25 PM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
while (fabs(NG - LG) < epsilon);

Continue the computation while the difference is greater than epsilon.
In fact you have more problems than just that in the program - for instance (NG - LG) after LG=NG is always 0.

Last edited by linosaurusroot; 07-18-2013 at 10:30 PM.
 
1 members found this post helpful.
Old 07-18-2013, 11:18 PM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by linosaurusroot View Post
while (fabs(NG - LG) < epsilon);

Continue the computation while the difference is greater than epsilon.
In fact you have more problems than just that in the program - for instance (NG - LG) after LG=NG is always 0.
oh that 0 is going to be an issue. that is probably why my loop continues indefinitely. not good.

would you be so kind as to point me in a direction for updating the value of LG with the results of NG in a way as to not to cause the test to fail... please not exact code.
 
Old 07-18-2013, 11:30 PM   #4
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
while (1) {
   calc

   if (test_something) break;

   update LG
}
 
1 members found this post helpful.
Old 07-19-2013, 02:37 PM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Thank you I figured out today after class and talking with both prof and an other student what i needed to fix. here is the working completed code.

1. to solve my issue of updating LG with the value of NG i used something we went over in class with pointers for swapping values of variables.

2. As I am using a do while loop my test was backwards. I forgot that in a do while will continue until the results is false, not true like in a for and an if statement.

Code:
#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<math.h>

//user defined function prototype
void swap(double *a, double *b);

int main(void)
{
	double N, NG, LG, *a, *b, epsilon;	// setting all variables to type double

	epsilon = 0.005;			// setting error tolerance to 0.005
	LG = 1.0;				// setting initial guess value

	printf("\nPlease enter the number you wish to find the approximate square root: ");
	scanf("%lf",&N);	// getting input from user

	NG = 0.5 * (LG + (N / LG));	// initialize NG for test in do while loop

do	{
	swap(&NG, &LG);
	NG = 0.5 * (LG + (N / LG));
	printf("\n%.2lf      %.3lf\n", N, NG);
	}	
	while (fabs(LG - NG) >= epsilon);

	printf("\nThe approximate square root of %.2lf is %.3lf\n", N, NG);
}

// using this to swap the values of LG and NG for my do while loop to allow for the update
// of LG from the calculation of NG = foo

void swap(double *a, double *b)
{
	double temp;
	temp = *a;
	*a = *b;
	*b = temp;
	printf("swap number %.2lf", temp);	// used this for testing and troubleshooing logic in do while loop
}
tah dah, it works.

Thank you for the great help.
 
Old 07-19-2013, 03:58 PM   #6
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
If your prof wrote the school's finance s/w pay a negative amount next year.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
Stuck in Bash for loop devn Programming 15 09-29-2007 11:41 AM
Stuck in a loop......... jbrush Fedora - Installation 0 11-16-2006 09:16 PM
Another ? from a newbie - stuck in a while loop azucarmom Programming 2 12-05-2004 08:41 PM
Stuck in a loggin loop boilinthebagboy Linux - Software 5 07-09-2003 04:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration