Static Local Persistence

Static Local Persistence

Hard C Functions 32 views
Explanation Complexity

Problem Statement

Create a function that generates the Fibonacci sequence. Store the previous two values ​​in static variables. Return the next Fibonacci number on every call.

Input Format

No input parameters.
The function is called repeatedly.

Output Format

Each function call returns the next Fibonacci number in the sequence.

Example

Function calls:
call 1, call 2, call 3, call 4, call 5
0, 1, 1, 2, 3

Constraints

• Uses static variables

• Sequence must continue across function calls

Concept Explanation

The Fibonacci sequence depends on the previous two values.
Using static variables allows the function to remember values between calls.

Step-by-Step Explanation

1.Inside the function, declare two static variables to store the previous two Fibonacci numbers.

2.Initialize them only once (first function call):

• First value = 0

• Second value = 1

3.On each function call:

4.Store the current first value as the result to return.

5.Calculate the next Fibonacci number by adding the two stored values.

6.Update the static variables:

• First variable becomes the second

• Second variable becomes the newly calculated value

7.Return the stored result.

8.On the next call, the function continues from where it left off.

Concept Explanation

The Fibonacci sequence depends on the previous two values.
Using static variables allows the function to remember values between calls.

Step-by-Step Explanation

1.Inside the function, declare two static variables to store the previous two Fibonacci numbers.

2.Initialize them only once (first function call):

• First value = 0

• Second value = 1

3.On each function call:

4.Store the current first value as the result to return.

5.Calculate the next Fibonacci number by adding the two stored values.

6.Update the static variables:

• First variable becomes the second

• Second variable becomes the newly calculated value

7.Return the stored result.

8.On the next call, the function continues from where it left off.

Input / Output Format

Input Format
No input parameters.
The function is called repeatedly.
Output Format
Each function call returns the next Fibonacci number in the sequence.
Constraints
• Uses static variables

• Sequence must continue across function calls

Examples

Input:
Function calls: call 1, call 2, call 3, call 4, call 5
Output:
0, 1, 1, 2, 3

Example Solution (Public)

C
#include <stdio.h>

/* Function to generate next Fibonacci number on each call */
int nextFibonacci() {
    static int first = 0;    // previous Fibonacci number F(n-2)
    static int second = 1;   // previous Fibonacci number F(n-1)
    static int count = 0;    // count of numbers returned

    int next;

    if (count == 0) {
        count++;
        return 0;   // First Fibonacci number
    } else if (count == 1) {
        count++;
        return 1;   // Second Fibonacci number
    } else {
        next = first + second;
        first = second;
        second = next;
        count++;
        return next;
    }
}

int main() {
    int n, i;

    printf("How many Fibonacci numbers do you want? ");
    scanf("%d", &n);

    printf("Fibonacci sequence:n");

    for (i = 0; i < n; i++) {
        printf("%d ", nextFibonacci());
    }

    printf("n");
    return 0;
}

Official Solution Code

#include <stdio.h>

/* Function to generate next Fibonacci number on each call */
int nextFibonacci() {
    static int first = 0;    // previous Fibonacci number F(n-2)
    static int second = 1;   // previous Fibonacci number F(n-1)
    static int count = 0;    // count of numbers returned

    int next;

    if (count == 0) {
        count++;
        return 0;   // First Fibonacci number
    } else if (count == 1) {
        count++;
        return 1;   // Second Fibonacci number
    } else {
        next = first + second;
        first = second;
        second = next;
        count++;
        return next;
    }
}

int main() {
    int n, i;

    printf("How many Fibonacci numbers do you want? ");
    scanf("%d", &n);

    printf("Fibonacci sequence:n");

    for (i = 0; i < n; i++) {
        printf("%d ", nextFibonacci());
    }

    printf("n");
    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.