Solved

How Do I Get This Fibonacci Program to Accept User Input?

Posted on 2008-10-14
4
215 Views
Last Modified: 2008-10-14
I am trying to develop a program that takes in a user input of a number and returns as an output that number in the fibonacci series.  For example, if the user types in 5, the 5th number is displayed; 23 and the 23rd number is displayed.  So far I have this, but it only spits out the fibonacci series for 100 numbers and does not stop at the number put in by the user.  Please help!

#include <stdio.h>

char line[1000];

int main()
{

  int fib[1000];
  int i;

  printf("Fibonacci\n");
  fgets(line, sizeof(line), stdin);
  sscanf(line," %d", &i);

  if (i<0) return 0;

  fib[0] = 0;
  fib[1] = 1;

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

  for (i = 0; i<1000; i++)
    {
      printf("%d %d\n", i, fib[i]);
    }

  return (0);

}

0
Comment
Question by:spngegirl01
4 Comments
 

Author Comment

by:spngegirl01
Comment Utility
Hi thank you that fixed the problem of having the output end after the term, however, the series now starts at 0 in the output, so when I enter Fibonacci 3, I really only see the series up to the second term (assuming the first term is 1).  I added this below, but it won't work.  Any ideas?


fib[0] ='\0';
  fib[1] = 1;
  fib[2] = 1;

0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 500 total points
Comment Utility
spngegirl01,

Please disregard the code posted by shasunder and work on improving your own code which is good and can be easily improved.

>So far I have this, but it only spits out the fibonacci series for 100 numbers and does not stop at the number put in by the user

That happens because of this loop in which you are printing
  for (i = 0; i<1000; i++)
    {
      printf("%d %d\n", i, fib[i]);
    }
You are printing all values from 0 to 999 ... while you need to stop at the specific number. Edit your loop to iterate upto j times - that is the number of values you want printed - instead of 1000.

Next improvement that you can make is to calculate only j values instead of 1000 values. To do that - again make similar modifications to the loop in which you are calculating the values.

cheers!
sunnycoder
0
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
Comment Utility
Sorry sunnycoder,

I was not aware that it was homework problem
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now