0

I created an array of ints, user is prompted to pick 2 numbers, I am trying to return the sequence of fibonacci from those 2 numbers

#include <stdio.h>

int main ()
{
  int a, b;

  int nums[48];

  for (int i = 0; i < 47; i++)

    {
      printf ("Pick a number between 1 - 47\n");
      scanf ("%d", &a);

      printf ("Pick a number between 1 - 47\n");
      scanf ("%d", &b);

      if (a >= 47 || a <= 1)
    {
      printf ("Out of range pick another number between 1 - 47\n");
      scanf ("%d", &a);
    }

      if (b >= 47 || b <= 1)
    {
      printf ("Out of range pick another number between 1 - 47\n");
      scanf ("%d", &b);
    }

      nums[i] = a;
      nums[i + 1] = b;

      int c = a + b;

      printf ("The sequence is: %d\n", c);
    }
  return 0;
}

Return up to 47 numbers of the fibonacci sequence

dbush
  • 205,898
  • 23
  • 218
  • 273
P.M.T
  • 19
  • 1
  • 3
  • Hi. I have read your question, but can you add what the problem you facing and what you intend to do more clearly? Thanks – avijit Feb 12 '19 at 03:14
  • So the user is prompted to choose 2 numbers, let's say that person chooses a = 4 and b = 2. The first number of the sequence would be 2 and 4 and then it would be 6 because 4 + 2 = 6. Then it would be 2 + 6 = 8. Then 6 + 8 = 14. It is just taking the numbers the person chose and using those numbers as the first of the sequence. – P.M.T Feb 12 '19 at 03:25
  • Then I think my answer is ok for what are you asking. Please check the answer. – avijit Feb 12 '19 at 03:29
  • @avijitbhattacharjee please stop soliciting. Answering a question containing no stated problem by providing code with no explanation only encourages exactly the types of questions that are likely to be of no use to future visitors. Stack Overflow is not a code writing service. – paddy Feb 12 '19 at 03:40
  • Sorry for that. Actually he/she is a first time contributor. That is why I am being nice to him. P.M.T please add the clarification of the problem in the question statement. @paddy sorry for the inconvenience. thanks – avijit Feb 12 '19 at 03:42
  • 2
    It's better to do that in a way that encourages quality questions. "Being nice" does not explain your two separate requests for them to accept your code-only answer -- that seems to be entirely self-motivated. Each of you should review the help center: [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) / [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – paddy Feb 12 '19 at 03:48

1 Answers1

0

I have modified your code, assuming what your are expecting. If you want a recursion of version of this code that is also possible.

int main ()
{
  int a, b;

  int nums[48];
    //input two numbers once
    printf ("Pick a number between 1 - 47\n");
    scanf ("%d", &a);

    printf ("Pick a number between 1 - 47\n");
    scanf ("%d", &b);

    if (a >= 47 || a <= 1)
    {
      printf ("Out of range pick another number between 1 - 47\n");
      scanf ("%d", &a);
    }

    if (b >= 47 || b <= 1)
    {
      printf ("Out of range pick another number between 1 - 47\n");
      scanf ("%d", &b);
    }

    nums[0] = a;
    nums[1] = b;
    
   //calculate the fibonnaci series
   for (int i = 2; i <= 47; i++)
   {
      nums[i] = nums[i-1] + nums[i-2];
      
   }
   //Then print the series
    print("Fibonnacci series for a = %d and b = %d is ", a, b);

    for(int i = 0; i <= 47; i++)
        print("%d ", nums[i]);
  return 0;
}
avijit
  • 805
  • 2
  • 12
  • 21