Question

This code is based on creating a recursive funciton. You cannot alter the code besides adding...

This code is based on creating a recursive funciton. You cannot alter the code besides adding your solution in the particular area.

Write code to complete PrintFactorial()'s recursive case. Sample output if userVal is 5:

5! = 5 * 4 * 3 * 2 * 1 = 120

#include

void PrintFactorial(int factCounter, int factValue){
   int nextCounter = 0;
   int nextValue = 0;

   if (factCounter == 0) {            // Base case: 0! = 1
      printf("1\n");
   }
   else if (factCounter == 1) {       // Base case: print 1 and result
      printf("%d = %d\n", factCounter, factValue);
   }
   else {                             // Recursive case
      printf("%d * ", factCounter);
      nextCounter = factCounter - 1;
      nextValue = nextCounter * factValue;

      /* YOUR SOLUTION GOES HERE*/

   }
}

int main(void) {
   int userVal = 0;

   userVal = 5;
   printf("%d! = ", userVal);
   PrintFactorial(userVal, userVal);

   return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

sh-4 . 3# sh-4.3# g++ -o main *.cpp sh-4.3t main 55 4321 120 sh-4·34 9

#include<stdio.h>
void PrintFactorial(int factCounter, int factValue){
   int nextCounter = 0;
   int nextValue = 0;
   if (factCounter == 0) {            // Base case: 0! = 1
      printf("1\n");
   }
   else if (factCounter == 1) {       // Base case: print 1 and result
      printf("%d = %d\n", factCounter, factValue);
   }
   else {                             // Recursive case
      printf("%d * ", factCounter);
      nextCounter = factCounter - 1;
      nextValue = nextCounter * factValue;
      PrintFactorial(nextCounter,nextValue);
   }
}
int main(void) {
   int userVal = 0;
   userVal = 5;
   printf("%d! = ", userVal);
   PrintFactorial(userVal, userVal);
   return 0;
}

Add a comment
Answer #2

#include <stdio.h>

void PrintFactorial(int factCounter, int factValue){
int nextCounter = 0;
int nextValue = 0;

if (factCounter == 0) { // Base case: 0! = 1
printf("1\n");
}
else if (factCounter == 1) { // Base case: print 1 and result
printf("%d = %d\n", factCounter, factValue);
}
else { // Recursive case
printf("%d * ", factCounter);
nextCounter = factCounter - 1;
nextValue = nextCounter * factValue;

/* YOUR SOLUTION GOES HERE*/
return PrintFactorial(nextCounter, nextValue);

}
}

int main(void) {
int userVal = 0;

userVal = 5;
printf("%d! = ", userVal);
PrintFactorial(userVal, userVal);

return 0;
}

Output :

Add a comment
Know the answer?
Add Answer to:
This code is based on creating a recursive funciton. You cannot alter the code besides adding...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • CHALLENGE ACTIVITY 6.4.2: Recursive function: Writing the recursive case. Write code to complete factorial_str()'s recursive case....

    CHALLENGE ACTIVITY 6.4.2: Recursive function: Writing the recursive case. Write code to complete factorial_str()'s recursive case. Sample output with input: 5 5! = 5 * 4 * 3 * 2 * 1 = 120 1 test passed 4 6 All tests 1 passed 8 9 1 def factorial_str(fact_counter, fact_value): 2 output_string = 3 if fact_counter == 0: # Base case: 0! = 1 5 output_string += '1' elif fact counter == 1: # Base case: print 1 and result 7...

  • PROBLEM: Write a recursive method named Addition that takes two integers X and Y returns their...

    PROBLEM: Write a recursive method named Addition that takes two integers X and Y returns their sum. Please comment on every line of code. EXISTING CODE: int AddToN(int n) { if (n == 0) { // base case return 0; } else { // recursive case return n + AddToN(n-1); //make recursive call } } void printStars (int n) { if (n==0 ) { //base case cout <<""; } else{ printStars(n-1); cout << ""; } } } int sumRange (...

  • PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the...

    PROBLEM: Write a recursive method named Division that takes two integers X and Y returns the result of integer division (i.e., 8 / 3 = 2). Please comment on every line of code. EXISITNG CODE: int Exponentiation(int X, int Y) { if (Y == 0) // base case return 1; else // recursive case return X * Exponentiation(X, Y - 1); //make recursive call } int Multiply(int X, int Y){ if(Y == 0){ return 0; } else{ return X +...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • PROBLEM: Write a recursive method named AddToN that adds up all the numbers between 1 and...

    PROBLEM: Write a recursive method named AddToN that adds up all the numbers between 1 and an integer N. Please add comments to every line of code. EXISTING CODE: void printStars (int n) { if (n==0 ) { //base case cout <<""; } else{ printStars(n-1); cout << ""; } } } int sumRange ( int min, int max) { if (min == max){ //base case return min; } else{ //recursive case return max + sumRange(min,max-1); } } } int sumDigits(int...

  • PROBLEM: Write a recursive method named SumEvens that takes an integer X and returns the sum...

    PROBLEM: Write a recursive method named SumEvens that takes an integer X and returns the sum of even digits in X. Please comment on every line of code. EXISTING CODE: int numTwos(int n) { int right = n % 10; int remain = n / 10; if(n==0) { return 0 ; } else if(right ==2) { //rightmost digit is 2 return 1 + numTwos(remain); else { return 0 + numTwos(remain); } } int SumDigits(int X) { if (X == 0)...

  • Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h>...

    Combine two codes (code 1) to get names with(code 2) to get info: Code 1: #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<dirent.h> #include<stdio.h> #include<stdlib.h> void do_ls(char []); int main(int argc,char *argv[]) { if(argc == 1) do_ls("."); else while(--argc){ printf("%s:\n",*++argv); do_ls(*argv); } } void do_ls(char dirname[]) { DIR *dir_ptr; struct dirent *direntp; if((dir_ptr = opendir(dirname)) == NULL) fprintf(stderr,"ls1:cannot open %s\n",dirname); else { while((direntp = readdir(dir_ptr)) != NULL) printf("%s\n",direntp->d_name); closedir(dir_ptr); } } ____________________________ code 2: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> void show_stat_info(char *,...

  • Code goes like: #include <stdio.h> /* * askGrade(): * Asks the student for a letter grade...

    Code goes like: #include <stdio.h> /* * askGrade(): * Asks the student for a letter grade * (A = 90-100, B = 80-89, etc.) * then gives the appropriate letter grade in upper case. */ void askGrade() { int grade;    printf("Enter your grade:"); scanf("%d", &grade);    printf("Your grade is: ");    // BEGIN YOUR CODE    // TODO: Use if-else statements to print the right letter grade.    // END YOUR CODE    printf("\n"); } /* * dayOfTheWeek(): *...

  • Read the following code, threaded recursive calculation of Fibonacci number of n: #include <stdio.h> #include <stdlib.h>...

    Read the following code, threaded recursive calculation of Fibonacci number of n: #include <stdio.h> #include <stdlib.h> #include <pthread.h> void *fib(void *arg); int main(int argc, char **argv){     int n = atoi(argv[1]);     printf("%d\n", (int)fib(n)); } void *fib(void *arg){     int n;     pthread_t thread1;     pthread_t thread2;     void *a;     void *b;     int c;     n = (int)arg;     if (n <= 0) return 0;     if (n == 1) return 1;     pthread_create(&thread1, NULL, fib, n-1);    ...

  • In a recursive solution, the base case terminates the recursive processing. In the below code, which...

    In a recursive solution, the base case terminates the recursive processing. In the below code, which of the following calls to recurse() would stop further recursive calls? #include <iostream> void recurse(int x, int y) { if (y > 0) { x++; y--; std::cout << x << " " << y << " "; recurse(x, y); std::cout << x << " " << y << std::endl; } } 1recurse(1, 2); 2recurse(1, 0); 3recurse(1, 1); 4recurse(0, 1);

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT