Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming C style file I/O and compute Fibonacci numbers There is a main fun

ID: 664076 • Letter: C

Question

C programming

C style file I/O and compute Fibonacci numbers

There is a main function, and you should complete thoes part:

fill in fibonacciRecursive() with a RECURSIVE function which computes and returns the nth Fibonacci number, for n > 0.

fill in fibonacciIterative() with an ITERATIVE function which does the same thing as above.

fill in testRecursiveFibonacci() with a function that will open a file (filename is supplied in the function arguments), then writes Fibonacci numbers 1-n into the file, using the fibonacciRecursive() function.

fill in testIterativeFibonacci() with a function that does the same as testRecursiveFibonacci, but uses fibonacciIterative.

the file created will be named "iterative.txt"

fill in testSummation() with a function that will open a file, and read a sequence of newline separated numbers and computes their sum.

//main.function

Explanation / Answer

//main.function

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

enum Mode {
        TEST_RECURSIVE_FIBONACCI = 0,
        TEST_ITERATIVE_FIBONACCI,
        TEST_SUMMATION,
        TEST_ALL
};

/* global variables which we will use to control what gets tested, and how much */
enum Mode programMode = TEST_RECURSIVE_FIBONACCI;
int globalMaxFibonacciNumToCompute = 10;

/* forward declarations */
int fibonacciRecursive(int n);
int fibonacciIterative(int n);

void testRecursiveFibonacci(char* filename, int maxFibonacciNumToCompute);
void testIterativeFibonacci(char* filename, int maxFibonacciNumToCompute);
void testSummation(char* filename);

/* This function will process the command line arguments and bind the global variables
* appropriately. If something is amiss, it will print the usage of the program
*/
void parseArgs(int argc, char** argv)
{
        int i;

        if(argc < 2)
                return;
        for(i = 1; i < argc; ++i)
        {
                if(strcmp(argv[i],"-r") == 0)
                        programMode = TEST_RECURSIVE_FIBONACCI;
                else if(strcmp(argv[i], "-i") == 0)
                        programMode = TEST_ITERATIVE_FIBONACCI;

                else if(strcmp(argv[i], "-s") == 0)
                        programMode = TEST_SUMMATION;

                else if(strcmp(argv[i], "-a") == 0)
                        programMode = TEST_ALL;

                else if(strcmp(argv[i], "-n") == 0 && argc > i)
                        globalMaxFibonacciNumToCompute = atoi(argv[++i]);
                else
                {
                        printf(" Usage: ./prog [-r] [-i] [-n INTEGER] [-s] [-a] ");
                        printf(" -r => set the program to test recursive fibonacci ");
                        printf(" -i => set the program to test iterative fibonacci ");

                        printf(" -n => set the number of fibonacci numbers to compute, i.e. -n 40 ");
                        printf(" -s => set the program to test summation and file reading ");
                        printf(" -a => set the program to test everything ");
                }
        }
}

int main (int argc, char** argv)
{
        clock_t before, after;

        parseArgs(argc, argv);

        /* test recursive fibonacci */
        if(programMode == TEST_RECURSIVE_FIBONACCI || programMode == TEST_ALL)
        {
                /* this call will get the number of clock ticks since the program launched */
                before = clock();
                testRecursiveFibonacci("d: ecursive.txt", globalMaxFibonacciNumToCompute);
                after = clock();
                /* the difference of after and before gives us the time of the stuff in between */
                printf("recursive version done in %f seconds ", (float)(after-before)/(float)CLOCKS_PER_SEC);
        }

        /* test the iterative version */
        if(programMode == TEST_ITERATIVE_FIBONACCI || programMode == TEST_ALL)
        {
                before = clock();
                testIterativeFibonacci("d:\iterative.txt", globalMaxFibonacciNumToCompute);
                after = clock();
                printf("iterative version done in %f seconds ", (float)(after-before)/(float)CLOCKS_PER_SEC);
        }

        /* test file reading and summation */
        if(programMode == TEST_SUMMATION || programMode == TEST_ALL)
                testSummation("numbers.txt");

        return 0;
}

/* This function computes fibonacci numbers
* param        n               index of the number to be computed
* pre          n>0
* ret          the nth fibonacci number
*/
int fibonacciRecursive(int n)
{
        /* FIXME you will write this function */
        if ( n == 0 )
            return 0;
        else if ( n == 1 )
            return 1;
        else
            return ( fibonacciRecursive(n-1) + fibonacciRecursive(n-2) );
}

/* This function computes fibonacci numbers
* param        n               index of the number to be computed
* pre          n>0
* ret          the nth fibonacci number
*/
int fibonacciIterative(int n)
{
        int result;
        /* this will allocate an array of size n which you can fill in (position i - 1 should store the ith fibonacci number) */
        int* fibArray = (int*) malloc(n*sizeof(int));
        if(!fibArray)
        {
                fprintf(stderr, "*****malloc failed in iterative");
                return -1;
        }

        /* FIXME you will finish this function */
        int i=2,f1=0,f2=1,f3=0;
        fibArray[0]=0;
        fibArray[1]=1;
        if(n==0){
            return 0;
        }
        if(n==1){
            return 1;
        }
        for(i=2;i<=n;i++){
                f3=f1+f2;
                f1=f2;
                f2=f3;
                *(fibArray+i)=f3;
        }
        result=f3;
        /* clean up the memory we allocated */
        free(fibArray);
        return result;
}

/* This function should call the fibonacci function a number of times, and write the
* results to the file.
* param        filename                                                name of the file to be opened
* param        maxFibonacciNumToCompute                index of the max number to be computed
* pre          maxFibonacciNumToCompute>0
* post         a file exists, with fibonacci numbers 1-Max written in it.
*/
void testRecursiveFibonacci(char* filename, int maxFibonacciNumToCompute)
{
        /* FIXME you will write this function */
        FILE *fptr;
        fptr=fopen(filename,"w");
        if(fptr==NULL){
           printf("Error!");
           exit(1);
        }
        int i=0;
        for(i=0;i<maxFibonacciNumToCompute;i++){
            fprintf(fptr,"%d ",fibonacciRecursive(i));
        }
        fclose(fptr);
}

/* This function should call the fibonacci function a number of times, and write the
* results to the file.
* param        filename                                                name of the file to be opened
* param        maxFibonacciNumToCompute                index of the max number to be computed
* pre          maxFibonacciNumToCompute>0
* post         a file exists, with fibonacci numbers 1-Max written in it.
*/
void testIterativeFibonacci(char* filename, int maxFibonacciNumToCompute)
{
        /* FIXME you will write this function */
        FILE *fptr;
        fptr=fopen(filename,"w");
        if(fptr==NULL){
           printf("Error!");
           exit(1);
        }
        int i=0;
        for(i=0;i<maxFibonacciNumToCompute;i++){
            fprintf(fptr,"%d ",fibonacciIterative(i));
        }
        fclose(fptr);
}

/* This function should open a file, and read newline separated numbers from it.
* As it does this, it should compute their running sum, then print it.
* param        filename                                                name of the file to be opened
* pre          a file by the nme of filename is on the disk, containing newline separted #s
*/
void testSummation(char* filename)
{
        /* FIXME you will write this function */
        FILE *fptr;
        fptr=fopen(filename,"r");
        if(fptr==NULL){
           printf("Error!");
           exit(1);
        }
        int num,sum=0;
        while(!feof(fptr)){
            fscanf(fptr,"%d",&num);
            sum+=num;
        }
        printf(" Summation: %d",sum);
        fclose(fptr);
}