107k views
4 votes
Write code to complete DoublePennies()'s base case. Sample output for below program:Number of pennies after 10 days: 1024#include // Returns number of pennies if pennies are doubled numDays timeslong 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 onint main(void) {long long startingPennies = 0;int userDays = 0;startingPennies = 1;userDays = 10;printf("Number of pennies after %d days: %lld\\", userDays, DoublePennies(startingPennies, userDays));return 0;}

User Keran
by
3.9k points

1 Answer

0 votes

Answer:

The complete code along with output and comments for explanation are given below.

Step-by-step explanation:

#include <stdio.h>

// function DoublePennies starts here

// The function DoublePennies returns number of pennies if pennies are doubled numDays times

// this is an example of recursive function which basically calls itself

long long DoublePennies(long long numPennies, int numDays){

long long totalPennies = 0;

\\ here we implemented the base case when number of days are zero then return the number of pennies

if(numDays == 0)

return numPennies;

// if the base case is not executed then this else condition will be executed that doubles the number of pennies for each successive day.

else

{

totalPennies = DoublePennies((numPennies * 2), numDays - 1);

}

return totalPennies;

}

// driver code starts here

// Program computes pennies if you have 1 penny today,

// 2 pennies after one day, 4 after two days, and so on

int main(void)

{

// initialize starting pennies and number of days

long long startingPennies = 0;

int userDays = 0;

// input starting pennies and number of days

startingPennies = 1;

userDays = 10;

// print number of pennies and number of days

printf("Number of pennies after %d days: %lld\\", userDays, DoublePennies(startingPennies, userDays));

return 0;

}

Output:

Test 1:

Number of pennies after 10 days: 1024

Test 2:

Number of pennies after 2 days: 4

Test 3:

Number of pennies after 0 days: 1

Write code to complete DoublePennies()'s base case. Sample output for below program-example-1
Write code to complete DoublePennies()'s base case. Sample output for below program-example-2
User Gtludwig
by
4.6k points