Created
May 15, 2016 05:50
-
-
Save tiptopcoder/d03bbaa6f3999f4c4cc0978f81f53937 to your computer and use it in GitHub Desktop.
Fibonacci using Dynamic Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int main () | |
{ | |
double *fib; | |
int n, i; | |
printf("How many periods would you like? "); | |
scanf("%d", &n ); | |
fib = (double *) malloc ( n * sizeof(double) ); | |
if ( fib == NULL ) | |
{ | |
printf("Error\n"); | |
exit (-1); | |
} | |
for ( i = 0 ; i <= n ; i++ ) | |
{ | |
if ( i == 0 || i == 1 ) | |
{ | |
fib[i] = i; | |
} | |
else | |
{ | |
fib[i] = fib[i-1] + fib[i-2]; | |
} | |
printf("fibonacci[%02d] = %7.0f ", i, *(fib + i) ); | |
//Break line after 4 elements per row | |
if ( i % 3 == 2 ) | |
{ | |
printf("\n"); | |
} | |
} | |
printf("\n"); | |
printf("%.1lf", *(fib+n)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment