3 Replies - 11956 Views - Last Post: 18 January 2010 - 12:48 PM Rate Topic: -----

#1 Nouf ~  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-December 09

return fibonacci number using array

Post icon  Posted 18 January 2010 - 11:12 AM

Hi

Question :

Write a program that reads an integer n, then declares an array of n elements. The program then calls a method that fills the array with the first n Fibonacci numbers. Then main reads a list of numbers less than n that denote the position of a number and calls another method that uses the array to return the Fibonacci number in that position. Main stops when a negative number is read.

my code :

import java.util.*;
public class FibonacciNumbers
{
static Scanner console=new Scanner (System.in);
static int i,pos,f,n;
static int [] fibo;

public static void main (String[]args)
{

fibo=new int [n];

System.out.println("Enter the length");
n=console.nextInt();


fill(fibo);

System.out.println(" Enter an integers less than the length  then enter -1 when finished");
pos=console.nextInt();
while( pos<n && pos!=-1)
  {
  System.out.print(" The fibonacci number at this position is : ");
position(pos);
pos=console.nextInt();
  }


}

 
 
 public static void fill(int[]fibo)
 {
 
 for (i=0;i<fibo.length;i++)
 fibo[i]=fibo[i-1]+fibo[i-2];
 
 }

 
 public static int position(int pos)
 {

 
 
 return fibo[pos];
 
 }
 
 
 
 
   





what's rong with it pleeeease !! :(

Is This A Good Question/Topic? 0
  • +

Replies To: return fibonacci number using array

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: return fibonacci number using array

Posted 18 January 2010 - 11:34 AM

View PostNouf ~, on 18 Jan, 2010 - 05:12 PM, said:

what's rong with it pleeeease !! :(



Several things. Most notably:

fibo=new int [n];

System.out.println("Enter the length");
n=console.nextInt();



You must define fibo after having read n on ths console, otherwise its size will always be 0.

	public static void fill(int[] fibo) {

	for (i = 0; i < fibo.length; i++)
		fibo[i] = fibo[i - 1] + fibo[i - 2];

	}



This doesn't cater for the 2 first values of the Fibonacci series... So how do you set fibo[0] and fibo[1]?
Was This Post Helpful? 0
  • +
  • -

#3 Gasper  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 66
  • Joined: 29-May 09

Re: return fibonacci number using array

Posted 18 January 2010 - 11:43 AM

...and printing results.
So it should look something like
import java.util.*;
public class FibonacciNumbers
{
	static Scanner console=new Scanner (System.in);
	static int i,pos,f,n;
	static int [] fibo;

	public static void main (String[]args)
	{
		System.out.println("Enter the length");
		n=console.nextInt();
		fibo=new int [n];

		fill(fibo);

		System.out.println(" Enter an integers less than the length  then enter -1 when finished");
		pos=console.nextInt();
		while( pos<n && pos!=-1)
		{
			System.out.print(" The fibonacci number at this position is : ");
			System.out.println(position(pos));
			pos=console.nextInt();
		}
	}

	public static void fill(int[]fibo)
	{
		fibo[0] = 1;
		fibo[1] = 1;
		
		for (i=2;i<fibo.length;i++)
			fibo[i]=fibo[i-1]+fibo[i-2]; 
	}

	public static int position(int pos)
	{
		return fibo[pos]; 
	}
 
}


Was This Post Helpful? 0
  • +
  • -

#4 Nouf ~  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 18-December 09

Re: return fibonacci number using array

Posted 18 January 2010 - 12:48 PM

many Thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1