-1

I'm in the process of creating a Fibonacci sequence generator in C for a school lab, but it can't be the regular way of 0 1 1 2 3 5 etc... the program is supposed to ask the user for a limit, and 2 numbers and generate the sequence from there.

Here is what I have so far:

#include <stdio.h>
#include <stdlib.h>
int main(void) 
{
    int num1, num2, num3, limit;
    int Fibarray[10];
    //Ask the user for a limit to the number of values displayed, checks to see if value is greater
    // than 10 or less than 0, if so displays an error message  
    printf("Please enter limit of values generated, the limit must be 10 or less, but greater than 0\n");
    scanf ("%i", &limit);
    if (limit > 10)
    {
        printf("Please enter a valid integer greater than 0, and less than or equal to 10");
    }
    else    
        printf("Please enter 2 numbers separated by a space, the second number must be larger than the first"); 
    scanf ("%i", &num1, &num2);
    if (num1>num2||num1<0)
    {
        puts("Please re enter your numbers, make sure they are in ascending order & the second number is greater than 0");
        return(15); 
    }
    else
    {
        // ...
    }
}

What I'm trying to figure out is, how would I have the two values added together, stored in the array, and added again etc. to the limit.

I believe this problem is different from How to generate Fibonacci series in C because in this program I need to have it accept values from the user, not preset values.

I've been looking through my book on this but it's not very helpful.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
zohan6934
  • 29
  • 3
  • 1
    You don't need an array (unless you want to print the results later). As with the usual Fibonacci sequence, you need three variables which you shuffle in each loop. The only difference is the two starting values. – Weather Vane Feb 21 '18 at 20:01
  • Not even a warning? – Weather Vane Feb 21 '18 at 20:02
  • @WeatherVane Nothing, compiles with no issues, and when I run it it goes as far as I have it written with no issues. Also, the results do need to be printed on the screen for the user to see, I figured I'd need an array for that but I'm not entirely sure how it'd be implimented – zohan6934 Feb 21 '18 at 20:04
  • 1
    Possible duplicate of [How to generate Fibonacci series in C](https://stackoverflow.com/questions/17229707/how-to-generate-fibonacci-series-in-c) – Matthew Kerian Feb 21 '18 at 20:05
  • 1
    MSVC gives 2 warnings about `scanf ("%i", &num1, &num2);` — *warning C4474: 'scanf' : too many arguments passed for format string*, and *note: placeholders and their parameters expect 1 variadic arguments* – Weather Vane Feb 21 '18 at 20:05
  • 1
    Aside: it is unclear whether the limit asked for is the number of terms, or the value reached. The question is more about entering values, than generation the Fibonacci sequence, which you don't show. – Weather Vane Feb 21 '18 at 20:08
  • 1
    @coderredoc: it's not undefined; the extra argument is evaluated but otherwise ignored (7.21.6.2/2). – John Bode Feb 21 '18 at 20:19
  • 1
    @JohnBode.: *If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored.*. How can I miss? – user2736738 Feb 21 '18 at 20:20
  • @WeatherVane changed that to be more clear to the user, thanks – zohan6934 Feb 21 '18 at 20:20
  • 1
    @zohan6934.: Hey check [**this**](http://port70.net/~nsz/c/c11/n1570.html#7.21.6.2p2) standard ref. I was missing it somehow. It is not undefined behavior. *If the format is exhausted while arguments remain, the excess arguments are evaluated (as always) but are otherwise ignored.* – user2736738 Feb 21 '18 at 20:21
  • 1
    @zohan6934 that makes the fixed array of 10 elements awkward, I earlier suggested that no array is needed unless the sequence needs to be stored. – Weather Vane Feb 21 '18 at 20:23
  • 1
    @user3386109.: Did you see that part `scanf ("%i", &num1, &num2);`? – user2736738 Feb 21 '18 at 20:25
  • @WeatherVane ok so removing the array is an option, how could I go about having the values calculated then? – zohan6934 Feb 21 '18 at 20:27
  • 2
    @coderredoc My bad, I've already deleted the comment. There still is undefined behavior, because `num2` is never initialized. – user3386109 Feb 21 '18 at 20:28
  • @user3386109.: It may lead to UB. – user2736738 Feb 21 '18 at 20:28
  • 1
    It does lead to UB because the `if` statement uses the uninitialized `num2`. – user3386109 Feb 21 '18 at 20:29
  • 1
    @user3386109.: The value is indeterminate but it will be UB if the value happens to be a trap representation for the type. – user2736738 Feb 21 '18 at 20:31
  • 1
    @user3386109 how is 'num2' not initaialized? on line 18 'if (num1>num2||num1<0) num2' is used – zohan6934 Feb 21 '18 at 20:32
  • I suggest you implement the standard Fibonacci first, in two ways. First for a fixed number of terms, second for a fixed value of the term. When you have played around with that, move on to entering those limits. – Weather Vane Feb 21 '18 at 20:32
  • 1
    @user3386109.: If the lvalue designates an object of automatic storage duration ***that could have been declared with the register storage class*** (never had its address taken), and that object is uninitialized (not declared with an initializer and no assignment to it has been performed prior to use), the behavior is undefined. – user2736738 Feb 21 '18 at 20:36
  • @coderredoc Yup, that is an odd quirk in the standard. – user3386109 Feb 21 '18 at 20:38
  • @user3386109.: Suppose this `unsigned char x;` and `unsigned char y`; `y=x` This is not UB. No trap rep for `unsigned char`. – user2736738 Feb 21 '18 at 20:39
  • @coderredoc Interesting, what section of the specification says that `unsigned char` can't have a trap representation? – user3386109 Feb 21 '18 at 20:45
  • @user3386109.: Check [this](http://port70.net/~nsz/c/c11/n1570.html#6.2.6.2p1). The [footnote](http://port70.net/~nsz/c/c11/n1570.html#note53) of it is more explicit. – user2736738 Feb 21 '18 at 20:52
  • @coderredoc Ok, thanks. All of this seems to imply that an unsigned character variable can never be a register variable. That's a rather odd restriction. – user3386109 Feb 21 '18 at 20:59
  • @zohan6934 Not initialized means that you never assigned a value to `num2`. You declared `num2` with `int num1, num2`, but that doesn't give it a value. You passed the address of `num2` to `scanf`, but `scanf` won't assign a value to `num2` because the format string doesn't have a conversion for `num2`. So when you use `num2` in the `if` statement, it doesn't have an assigned value. – user3386109 Feb 21 '18 at 21:12
  • are those "2 numbers" expected to be valid entries in the fibonacci sequence or are they to be the upper and lower limits so only a selected portion of the sequence is output? Please clarify – user3629249 Feb 21 '18 at 21:26
  • when compiling, always enable the warnings. (for `gcc`, at a minimum use: `-Wall -Wextra -Wconversion -pedantic -std=gnu11` ) (note the parameters are different for `visual studio`, etc – user3629249 Feb 21 '18 at 21:33
  • when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful. – user3629249 Feb 21 '18 at 21:36
  • when the code wants the user to enter/reenter some value, Strongly suggest implementing a loop that exits when the user has entered a valid value. I.E. replace: ` printf("... for brevity"); scanf ("%i", &limit); if (limit > 10) { printf("... for brevity"); }` with something similar to: `do { printf("... dots for brevity"); if( 1 != scanf ("%i", &limit) ) { fprintf( stderr, "scanf for limit failed\n"; exit( EXIT_FAILURE ); } } while( 0 – user3629249 Feb 21 '18 at 21:46
  • if would be better if the variables: `limit`, `num1` and `num2` were declared as `size_t` rather than `int`, Then the call(s) to `scanf()` would use input/format specifier(s) of `%lu` rather than `%i` – user3629249 Feb 21 '18 at 21:49

2 Answers2

0

regarding:

scanf ("%i", &num1, &num2); 

This will cause the compiler to output a warning message AND the variable num2 will never be set. Suggest:

size_t num1;
size_t num2;

....

if( 2 != scanf( "%lu %lu", &num1, &num2 ) ) 
{ 
    fprintf( stderr, "scanf for num1, num2 failed\n" );
    exit( EXIT_FAILURE ); 
} 
user3629249
  • 16,402
  • 1
  • 16
  • 17
0

To print the sequence, implement the following steps:

  • output a, the first number in the pair with printf
  • compute the next number c as the sum of a and b
  • update the pair: a = b, b = c.
  • iterate limit times.

Regarding your code, here is an improved version with stricter input checking:

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    int num1, num2, num3, limit;

    //Ask the user for a limit to the number of values displayed, checks to see if value is greater
    // than 10 or less than 0, if so displays an error message  
    printf("Please enter limit of values generated, the limit must be 10 or less, but greater than 0:\n");
    if (scanf("%i", &limit) != 1 || limit <= 0 || limit > 10) {
        printf("Please enter a valid integer greater than 0, and less than or equal to 10\n");
        return 1;
    }
    printf("Please enter 2 numbers separated by a space, the second number must be larger than the first:\n");
    if (scanf("%i%i", &num1, &num2) != 2 || num1 > num2) {
        puts("Please re enter your numbers, make sure they are in ascending order\n");
        return 2; 
    }
    // ...
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189