question archive 1)Write code to complete DoublePennies()'s base case
Subject:Computer SciencePrice:2.85 Bought3
1)Write code to complete DoublePennies()'s base case. Sample output for below program:
Number of pennies after 10 days: 1024
#include <stdio.h>
// Returns number of pennies if pennies are doubled numDays times
long long DoublePennies(long long numPennies, int numDays){
long long totalPennies = 0;
/* Your solution goes here */
else {
totalPennies = DoublePennies((numPennies * 2), numDays - 1);
}
return totalPennies;
}
// Program computes pennies if you have 1 penny today,
// 2 pennies after one day, 4 after two days, and so on
int main(void) {
long long startingPennies = 0;
int userDays = 0;
startingPennies = 1;
userDays = 10;
printf("Number of pennies after %d days: %lld\n", userDays, DoublePennies(startingPennies, userDays));
return 0;
}
2)
Write code to complete PrintFactorial()'s recursive case. Sample output if userVal is 5:
5! = 5 * 4 * 3 * 2 * 1 = 120 |
#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 */
}
}
int main(void) {
int userVal = 0;
userVal = 5;
printf("%d! = ", userVal);
PrintFactorial(userVal, userVal);
return 0;
}
Purchased 3 times