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...

  • 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 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 +...

  • 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 *,...

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

  • Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with...

    Language is in C. Need help troubleshooting my code Goal of code: * Replace strings with a new string. * Append an s to the end of your input string if it's not a keyword. * Insert a period at the end, if you have an empty input string do not insert a period Problems: //Why does my code stop at only 2 input strings //If I insert a character my empty string case works but it's still bugged where...

  • /* •   The following code consists of 5 parts. •   Find the errors for each part...

    /* •   The following code consists of 5 parts. •   Find the errors for each part and fix them. •   Explain the errors using comments on top of each part. */ #include <stdio.h> #include <string.h> int main( ) { ////////////////////////////////////////////////////////////////////////// //////////////        Part A. (5 points)                ////////////////// int g(void) { printf("%s", Inside function g\ n " ); int h(void) {       printf(" % s ", Inside function h\ n "); } } printf("Part A: \n "); g(); printf(" \n"); ////////////////////////////////////////////////////////////////////////// //////////////       ...

  • 10 What does the last printf in this code print to the screen? a)n-7 b)n- 0...

    10 What does the last printf in this code print to the screen? a)n-7 b)n- 0 c)n--1 d) None of the above 鬐include< stdio. h> int main) int n-0 for (n-7: n<O; n-- printf (n") printf ("n *%d", n); - return 11-What does the code print to the screen? 鬐include< stdio.h> void fun (int) int main) b) 8 d) None of the above fun (3) return void fun (int a) int x-10 while(a !_ 3 x > s} if a...

  • 6) Show the output of the following program. You mus trace the code to show how...

    6) Show the output of the following program. You mus trace the code to show how you reached the answer. (5 points) #include <stdio.h> int x 18; int y = 5; int z ·e; void my first function) void my_second_function); int main() int ys y = x; if (z) my_first functionO else my-second-function(); x++ : if (z) my_first_function(); else my_second_function(); return e; void my_first_function) x+y); is %d\n", printf("The value of x+y in my-first-function() void my_second_function) ( x=100; is %d\n", x);...

  • PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs...

    PLEASE USE VERY BASIC REGISTERS AND CODE TO DO THE FOLLOWING Objectives: -write assembly language programs to:             -define a recursive procedure/function and call it.             -use syscall operations to display integers and strings on the console window             -use syscall operations to read integers from the keyboard. Assignment Description: Implement a MIPS assembly language program that defines "main", and "function1" procedures. The function1 is recursive and should be defined as: function1(n) = (2*n)+9                      if n <= 5              =...

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
ADVERTISEMENT