user1789951
user1789951

Reputation: 661

Simple Java Fibonacci code issue

import java.util.Scanner;

public class Fibonacci
{
    public static void main(String[] args)
    {
        int count;
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter number");

        count = in.nextInt();

        int[] fib = new int [count];
        fib[0] = 1;
        fib[1] = 1;

        for (int i=2; i<count; i++)
        {
            fib[i] = fib[i-1] + fib[i-2];
        }

        for(int i=0; i<count; i++)
        {
            System.out.print(fib[i] + " ");

        }
    }
}

This is my very simple Fib program, what i cant figure out is why it always stops one number short. For example:

run: Please enter number 6 1 1 2 3 5 8 BUILD SUCCESSFUL (total time: 5 seconds)

run: Please enter number 7 1 1 2 3 5 8 13 BUILD SUCCESSFUL (total time: 5 seconds)

I thought in my FOR loops it should be "(int i=2; i <= count;"

but when i put in greater than or equal to in both, or either FOR loop it gives me an error

Any suggestions? i know its something easy i'm overlooking

Upvotes: 0

Views: 1766

Answers (5)

ChriskOlson
ChriskOlson

Reputation: 513

public class Fibonacci
{
 private int [] fibArray;

public Fibonacci()
{
}

public void Fibonacci()
{
    fibArray = new int[0];
}

public void setFibonnaci(int size)
{
    fibArray = new int[size];

    if(fibArray.length == 1)
    {
        fibArray [0] = 0;
    }

    else if(fibArray.length == 2)
    {
        fibArray[0] = 0;
        fibArray[1] = 1;
        fibArray[2] = 2;
    }
    else 
    {
        fibArray[1] = 1;
        fibArray[0] = 0;

        for(int x = 2; x < fibArray.length; x++)
        {
            fibArray [x] = fibArray[x-1] + fibArray[x-2];
        }
    } 
}

public int getSequence(int number)
{
    if(number -1 < fibArray.length)
    {
        return fibArray[number - 1];
    }
    return -1;
}

//check the test case for getFibo
public String toString()
{
    String output = "";
    for (int x = 0; x < fibArray.length; x++)
    {
        output += x + " - " + fibArray[x];
    }
    return output;
}

}

Late response but new to site and just trying to help. This fib class works 100%

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533442

A simpler solution is to avoid needing an array in the first place and you don't need to get the size right.

public static void main(String[] args) {
    System.out.println("Please enter a number");
    Scanner in = new Scanner(System.in);
    int count = in.nextInt();

    long a = 1, b = 1;
    for(int i = 0; i < count; i++) {
        System.out.print(a + " ");
        long c = a + b;
        a = b;
        b = c;
    }
    System.out.println();
}

Upvotes: 1

Jan D&#246;rrenhaus
Jan D&#246;rrenhaus

Reputation: 6717

Arrays are zero-based. This means, that (assuming count = 5) if you have the following array:

int[] fib = new int[5];

then you can access fib[0], fib[1], fib[2], fib[3] and fib[4]. So

for (int i = 0; i < 5; i++) {
    System.out.print(fib[i] + " ");
}

would be fine. As it would access everything in fib, starting with index 0, and stopping with the last index smaller than 5, which is 4. However, if you do:

for (int i = 0; i <= 5; i++) {
    System.out.print(fib[i] + " ");
}

then you will access the last index smaller than OR EQUAL TO 5, which is 5. But, as stated before, fib[5] is invalid. That's what gives you your error.

Upvotes: 1

lulyon
lulyon

Reputation: 7225

There should be one more array element space for int fib[], thus the fib[count] could be stored.

import java.util.Scanner;

public class Fibonacci
{
    public static void main(String[] args)
    {
        int count;
        Scanner in = new Scanner(System.in);
        System.out.println("Please enter number");

        count = in.nextInt();

        int[] fib = new int [count + 1];
        fib[0] = 1;
        fib[1] = 1;

        for (int i=2; i <= count; i++)
        {
            fib[i] = fib[i-1] + fib[i-2];
        }

        for(int i = 0; i<= count; i++)
        {
            System.out.print(fib[i] + " ");

        }
    }
}

Upvotes: 0

Arpit Tyagi
Arpit Tyagi

Reputation: 183

Your code is giving correct output. but still if you need one more element try to initialize array with count + 1 and then have your loop running for i <= count

public static void main(String[] args) {


int count;
Scanner in = new Scanner(System.in);
System.out.println("Please enter number");

count = in.nextInt();

        int[] fib = new int [count+1];
        fib[0] = 1;
        fib[1] = 1;

        for (int i=2; i <= count; i++){
            fib[i] = fib[i-1] + fib[i-2];
        }

         for(int i=0; i <= count; i++){
             System.out.print(fib[i] + " ");

         }
            }
        }

Upvotes: 1

Related Questions