Question: fact.c code : #include /* for lab on gdb * Computes n factorial (n!) using a recursive * algorithm. This program contains an intentional error.

 fact.c code : #include /* for lab on gdb * Computes

fact.c code :

#include

/* for lab on gdb * Computes n factorial (n!) using a recursive * algorithm. This program contains an intentional error. */

/* prototype for "factorial" */ int factorial(int);

/* our "main" function, where execution begins */ /* gets number from command line or input file and prints the factorial*/ int main (int argc, char *argv[]) { /* variable for reading number into */ int n;

/* if reading number from keyboard, print prompt onto screen */ if (isatty(0)) fprintf(stderr, "please type a number: ");

/* read the number */ scanf("%d", &n);

/* if number is out of range, exit with an error status, otherwise, compute and print the result */ if (n 12) { fprintf(stderr, "Number out of range. "); return 1; } else { printf("%d! is %d ", n, factorial(n)); } }

/* recursive factorial function */ int factorial(int n) { if ((n = 1) || (n = 0)) { // if n is zero or one, return a 1 return 1; } else { // otherwise, multiply n by the result of (n-1)! return n * factorial(n-1); } }

===================================================

five.txt contents

5

Part 11: Fix the bug (switch driver) 1. Hit q to exit gdb. Open fact.c in emacs. Carefully examine the offending line of code. Fix the problem by editing fact.c. Save it. Then recompile the program, limaking sure you use ine . g sHlch. 2. Now run the program with input redirection (using fact five. txt) and with console input (just running fact). Hopefully, the correct value for the factorial will be printed. If not, keep debugging

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!