Question: C Programming: Modify your Fibonacci computation program to accept a command line argument specifying how many elements to compute. Now, instead of using an array
C Programming:
Modify your Fibonacci computation program to accept a command line argument specifying how many elements to compute. Now, instead of using an array stored on the stack to store the Fibonacci elements, dynamically allocate the necessary memory for storing the solution on the heap. Additionally, your program should print usage information to the screen if the user runs your program without specifying how many Fibonacci elements to compute.
mymath.h:
#ifndef _mymath_h_ #define _mymath_h_
void compute_fibs (unsigned long* fibs, unsigned int* N);
#endif /*__mymath_h_ */
mymath.c:
#include "mymath.h" #include
void compute_fibs(unsigned long* fibs, unsigned int* N) { unsigned int i; unsigned long a,b,c; a = 0; b = 1; fibs[0] = 0; for(i = 1; i < *N; i++) { if(a > ULONG_MAX - b){ *N = i+1; fibs[i] = b; } else { c = a; a = b; b = b + c; fibs[i] = a; } } return; }
main.c:
#include
int main (int argc, char** argv) {
unsigned int i; unsigned int elements = 1000; unsigned long fibs[elements]; compute_fibs(fibs, &elements); for (i=0; i
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
