0

I want to get the fibonacci sequence entered by the user in array. The task given to me was "Ask the user for 2 integer input which will be taken for first and second array elements of size 10 array."

Here is my code.

int limit = 10;
    int[] fib = new int[limit];
    fib[0] = 0;
    fib[1] = 1;
  for (int j = 1; j < 2; j++) 
  {
    System.out.print("Enter number " + "[" + j + "]: ");
    num[j] = reader.nextInt();
    num[j] = fib[j+1] + fib[j+2];
    System.out.println("");
  }
    System.out.print("Result: ");
    for(int j = 0; j < limit; j++ ) 
    {
      System.out.print(fib[j] + " ");
      System.out.print("");
    }

I badly need help for this one, been searching for solution for hours and still don't get it.

5
  • The question you quote doesn't mention the Fibonacci sequence at all. What problem are you having with your code? Commented Nov 18, 2020 at 23:57
  • Fibonacci is adding the previous 2 numbers to the current one. If you look at your code, you accept input into num[j] by usinf reader.nextInt(). but then you modify that value again in the next line. That is where you are going wrong. Think about it this way. Get the first number in num[0]. Don't do any calculation yet. Get the second number in num[1]. Now from 2 on wards, add the previous two values together. That should give you the answer you are expecting. Commented Nov 18, 2020 at 23:58
  • It isn't clear what you're problem is but you never initiate the num array before using it which could be causing problems. Commented Nov 19, 2020 at 0:00
  • The problem is I don't get what is the task means. But as far as I know the first 2 integer will be used as first and second for a size 10 array that will display the result. Example : First Integer : 2 Second Integer : 3 the Result would be 2 3 5 8 13... Commented Nov 19, 2020 at 0:00
  • Also, what the goal of the user input? It seems like it going to break the sequence. Commented Nov 19, 2020 at 0:02

2 Answers 2

1

I'll just make some corrections to your code and explain them:

int limit = 10;
int[] fib = new int[limit];
// fib[0] = 0;
// fib[1] = 1;
// The two lines above are wrong. Even though the real fibonacci sequence starts
// with 0 and 1, the question asks for the first two terms to come from user
// inputs. Instead, you can initialize them below:

// In your old code, you had "j = 1; j < 2; j++". However, that only loops once.
// So, have your condition to be j <= 2 instead: (I'm assuming that you want 1
// and 2 and not zero-based because it should print out "Enter number [1]:" and
// "Enter number [2]:"
for (int j = 0; j < 2; j++) // Not "j < 2"
{
  System.out.print("Enter number " + "[" + j + "]: ");
  fib[j] = reader.nextInt(); // not num[j] = ..., it's fib[j] = ...
  // num[j] = fib[j+1] + fib[j+2];
  // You don't need this ^^^
  System.out.println("");
}

// Now you need to fill in the array:
for (int j = 2; j < limit; j++)
{
  fib[j] = fib[j - 1] + fib[j - 2];
}

System.out.print("Result: ");
for(int j = 0; j < limit; j++) 
{
  System.out.print(fib[j] + " ");
  System.out.print("");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Now I get it. Sorry just a newbie in using array
0

You have a number of mistakes here. Try to answer the following things:

  1. What is the purpose of num variable here? You are using it to take input (num[j] = reader.nextInt();), then you are using it to store the Fibonacci sequence as well (num[j] = fib[j+1] + fib[j+2];)!!!

  2. The first loop is only running one time. Assuming that your calculations are correct (which is not in this case!), it will have values for the first fibonacci number only.

  3. Finally when you are printing the Fibonacci numbers, you are not using the num variable! Why?

Anyway, here is a solution to your problem. But, it will not help you unless you understand the logic behind it and you know which line is doing what!!!

    int limit = 10;
    int[] fib = new int[limit];
    for (int j = 0; j < 2; j++) 
    {
        System.out.print("Enter number " + "[" + j + 1 + "]: ");
        fib[j] = reader.nextInt(); // These are the first two fibonacci numbers provided by the user
        System.out.println("");
    }
    System.out.print("Result: ");
    for(int j = 0; j < limit; j++ ) 
    {
        if( j > 1 ) // You only calculate from the third Fibonacci, as the first two were given by user
        {
           fib[j] = fib[j-1] + fib[j-2];
        }
        System.out.print(fib[j] + " ");
        System.out.print("");
    }

2 Comments

Sorry, I'm just a newbie in using array. That is why I was having so much mistakes.
No worries! It happens in the beginning. All the best!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.