Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-13-2010, 09:49 PM   PM User | #1
Buzdugan
New Coder

 
Join Date: Mar 2010
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Buzdugan is an unknown quantity at this point
Where the error is ?

Hey !

I'm a fresh java programmer and doing some exercises from a book.
Got one that requires me to make a program that would read a number, and then give the coressponding value from Fibonacci's series (Without vectors/arrays), using the while function.

I've managed to get smt done, but the computer just waits for smt... I normaly would pursue the solution myself in order to learn smarter/better, but there is no error on compiling, or runtime.

A trained eye will spot the bug in a giff :

Code:
import java.io.*;

public class fibonacci {
	
	public static void main(String [] args){
		
		int fa = 1;
		int fb = 1;
		int fc = 0;
		int n = 0;
		
		BufferedReader tastatura = new BufferedReader(new InputStreamReader(System.in),1);
		String linie = "";
		
		try {
			
			System.out.println("Which Fibonnacci number shall it be?");
			linie = tastatura.readLine();
			n = Integer.valueOf(linie);
						
			int i=2;
			
			while (i <= n){
				
				fc = fa+fb;
				fb = fa;
				fa = fc;
			}
			System.out.println("the "n" number: " + n + "corresponds to: " + fc);
		}
		
		catch (IOException e)
		{
			System.out.println("error: " + e.toString());
		}	
	}
}
I know there is a smarter way to do this, but wanna try to get the juices out of my unevolved brain 1st.

The 'algorithm' was simple. Have 2 variables hold the initial values (global), outside the while loop, then just increment(change) each, based on the other's value(evolution).

Should work in theory....

I think it has something to do with using variables outside the loop, and writing values in variables outside the loop. I think it shouldn't be a problem (for me), but I'm sure that someone would give me a more than logical/reasonable explanation for not doing this.

Last edited by Buzdugan; 12-13-2010 at 09:52 PM..
Buzdugan is offline   Reply With Quote
Old 12-13-2010, 10:00 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
PHP Code:
            while (<= n){
                
                
fc fa+fb;
                
fb fa;
                
fa fc;
            } 
Neither i is incremented or n decremented. Because of this, the program infinitely loops since the condition is never fulfilled (eventually you'll run out of resources though and the program would crash ).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 12-14-2010, 12:32 AM   PM User | #3
renegadeandy
Regular Coder

 
Join Date: Feb 2008
Location: Edinburgh - Scotland
Posts: 107
Thanks: 0
Thanked 12 Times in 12 Posts
renegadeandy is an unknown quantity at this point
Yep he is right - it will continue run those 3 lines forever.

Try adding :

Code:
i++;
to the bottom of your while loop!
renegadeandy is offline   Reply With Quote
Old 12-14-2010, 09:53 AM   PM User | #4
Buzdugan
New Coder

 
Join Date: Mar 2010
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Buzdugan is an unknown quantity at this point
omg, I`m such a n00b. thanks guys !

I've even made it greater
Code:
fa=0;
fb=1;
fc=0;
			int i=1;
			
			while (i <= n){
				
				fc = fa+fb;
				fa = fb;
				fb = fc;
				i++;
			}

Last edited by Buzdugan; 12-14-2010 at 10:23 AM..
Buzdugan is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:27 AM.


Advertisement
Log in to turn off these ads.