Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Issues In Database Management A Refernce For The Thinking Practitioner

Authors: Fabian Pascal

1st Edition

0201485559, 978-0201485554

More Books

Students also viewed these Databases questions

Question

What did they do? What did they say?

Answered: 1 week ago

Question

2. Do you find change a. invigorating? b. stressful? _______

Answered: 1 week ago