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

C programming C style file IO and compute Fibonacci numbers There is a main func

ID: 664027 • Letter: C

Question

C programming

C style file IO and compute Fibonacci numbers

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

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);

  
   if(programMode == TEST_RECURSIVE_FIBONACCI || programMode == TEST_ALL)
   {
      
       before = clock();
       testRecursiveFibonacci("recursive.txt", globalMaxFibonacciNumToCompute);
       after = clock();
       printf("recursive version done in %f seconds ", (float)(after-before)/(float)CLOCKS_PER_SEC);
   }
  
  
   if(programMode == TEST_ITERATIVE_FIBONACCI || programMode == TEST_ALL)
   {
       before = clock();
       testIterativeFibonacci("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)
{
   /* Need complete this function */
   return 1;
}

/* 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;
   int* fibArray = (int*) malloc(n*sizeof(int));
   if(!fibArray)  
   {
       fprintf(stderr, "*****malloc failed in iterative");
       return -1;
   }

   /* Need complete this function */
  
  
   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)
{
   /* Need complete this function */
}

/* 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)
{
   /* Need complete this function */
}

/* 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)
{
   /* Need complete this function */
  
}

Explanation / Answer

#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);

  

if(programMode == TEST_RECURSIVE_FIBONACCI || programMode == TEST_ALL)

{

  

before = clock();

testRecursiveFibonacci("recursive.txt", globalMaxFibonacciNumToCompute);

after = clock();

printf("recursive version done in %f seconds ", (float)(after-before)/(float)CLOCKS_PER_SEC);

}

  

  

if(programMode == TEST_ITERATIVE_FIBONACCI || programMode == TEST_ALL)

{

before = clock();

testIterativeFibonacci("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)

{

if(n == 1)

return 0;

if(n == 2)

return 1;

  

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, i, temp;

int* fibArray = (int*) malloc(n*sizeof(int));

if(!fibArray)

{

fprintf(stderr, "*****malloc failed in iterative");

return -1;

}

/* Need complete this function */

if(n==1)

result = 0;

else if(n==2)

result = 1;

else

{

temp = 0;

result = 1;

for (i=2; i<=n; i++) {

temp = result;

result = result + temp;

}

}

  

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)

{

int i;

FILE *f = fopen(filename, "w");

if (f == NULL)

{

printf("Error opening file! ");

exit(1);

}

for(i=1; i<=maxFibonacciNumToCompute; i++)

{

fprintf(f, "%d ", fibonacciRecursive(i));

}

fclose(f);

/* Need complete this function */

}

/* 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)

{

int i;

FILE *f = fopen(filename, "w");

if (f == NULL)

{

printf("Error opening file! ");

exit(1);

}

for(i=1; i<=maxFibonacciNumToCompute; i++)

{

fprintf(f, "%d ", fibonacciIterative(i));

}

fclose(f);

/* Need complete this function */

}

/* 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)

{

int sum=0, i;

FILE *f = fopen(filename, "r");

if (f == NULL)

{

printf("Error opening file! ");

exit(1);

}

while (!feof(f)) {

fscanf(f, "%d", &i);

sum+=i;

}

  

printf("Running summation is: %d", sum);

  

}