Write code to complete DoublePennies()’s base case. Sample output for below program:

Write code to complete DoublePennies()'s base case. Sample output for below program:

Number of pennies after 10 days: 1024

  1. #include <stdio.h>
  2. // Returns number of pennies if pennies are doubled numDays times
  3. long long DoublePennies(long long numPennies, int numDays){
  4. long long totalPennies = 0;
  5. /* Your solution goes here */
  6. else {
  7. totalPennies = DoublePennies((numPennies * 2), numDays - 1);
  8. }
  9. return totalPennies;
  10. }
  11. // Program computes pennies if you have 1 penny today,
  12. // 2 pennies after one day, 4 after two days, and so on
  13. int main(void) {
  14. long long startingPennies = 0;
  15. int userDays = 0;
  16. startingPennies = 1;
  17. userDays = 10;
  18. printf("Number of pennies after %d days: %lld\n", userDays, DoublePennies(startingPennies, userDays));
  19. return 0;
  20. }

Answer

  1. #include <stdio.h>
  2. // Returns number of pennies if pennies are doubled numDays times
  3. long long DoublePennies(long long numPennies, int numDays){
  4. long long totalPennies = 0;
  5. /* Your solution goes here */
  6. if(numDays==0)return numPennies;
  7. else {
  8. totalPennies = DoublePennies((numPennies * 2), numDays - 1);
  9. }
  10. return totalPennies;
  11. }
  12. // Program computes pennies if you have 1 penny today,
  13. // 2 pennies after one day, 4 after two days, and so on
  14. int main(void) {
  15. long long startingPennies = 0;
  16. int userDays = 0;
  17. startingPennies = 1;
  18. userDays = 10;
  19. printf("Number of pennies after %d days: %lld\n", userDays, DoublePennies(startingPennies, userDays));
  20. return 0;
  21. }

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts