Question: Please fix my code according to directions. I MUST USE THE # define N 1024 LIBRARY DIRECTIONS: Write a program fib.c that implements three C
Please fix my code according to directions.
I MUST USE THE # define N 1024 LIBRARY
DIRECTIONS:
Write a program fib.c that implements three C functions and calls them in the appropriate order to compute and print Fibonacci numbers. The function prototypes are shown below: void initArray ( int n ); // initializes an array of size n by writing 0 into each field int computeFib ( int n ); // compute the Fibonacci numbers up to n (n including if n is a Fibonacci number); // store them in the global array the function returns the number of Fibonacci numbers that // were computed and stored in the array void printArray ( int n ); // print values stored in the array up
The first function initializes a globally defined array of integers with values of 0. The second function implements the Fibonacci function and the third function prints the results to the screen.
The Fibonacci function must compute the Fibonacci numbers up to a certain value n specified by the paraemter. Your implementation must use the global array that stores computed Fibonacci numbers starting with 1 up to the cut-off value of n. The total number of Fibonacci numbers computed is returned.
The Fibonacci numbers are computed as follows: Fib(n) = Fib(n-1) + Fib(n-2). Fib(0) = 0, Fib(1) = 1.
The print function must display all the numbers in the global array. The parameter specifies how many in the array need to be printed starting from 1. Print the numbers on a horizontal line. Finish printing with a new line.
Write a main program that prompts the user for a number k (k < n, the dimension of your array)
Save, compile and run the file to test and evaluate your output.
#########################################################################################
my code:
#include
int fibNumbers[11]; int count,i;
void initArray(int n) { //initializing array elements to zero 1 - user defined number for(count = 1; count <= n; count++) { fibNumbers[count] = 0; } /// INITIALIZATION CHECK /* for(count = 1; count <= n; count++) { printf("Array: %d ", fibNumbers[count]); } printf("Array 1 + 5 = %d", fibNumbers[1]+2); } */ } int computeFib(int n) { for(count = 2; count <= n; count++) { fibNumbers[count] = fibNumbers[count-2] + fibNumbers[count-1]; } }
void printArray(int n) { for(count = 0; count <= n; ++count) for(i = 0; i < fibNumbers[count]; i++) printf("%d ", fibNumbers[count]);
}
int main(void){ int k; // how large the array is going to be
printf("Please enter a number less than 10 "); scanf("%d", &k); while(k > 10) { printf("Please re-enter a number less than or equal to 10 "); scanf("%d", &k); }
initArray(k);
for(count = 1; count<= k; count++) { fibNumbers[count-1] = computeFib(count); } printArray(k);
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
