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 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 debuggingStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started