Completing DoublePennies(): Solving the Infinite Loop

  • Context: MHB 
  • Thread starter Thread starter ksepe
  • Start date Start date
  • Tags Tags
    Infinite Loop
Click For Summary

Discussion Overview

The discussion revolves around completing the base case for a recursive function, DoublePennies(), which calculates the number of pennies after a specified number of days. Participants explore the use of recursion versus iteration, specifically addressing an infinite loop issue encountered in the original code implementation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks assistance in completing the base case of the recursive function, noting that the current implementation leads to an infinite loop.
  • Another participant questions the necessity of recursion, suggesting that a simple for loop could achieve the same result.
  • A participant identifies that the infinite loop arises because the recursion does not properly change the value of numDays when it equals 1.
  • One participant proposes a revised version of the function, introducing a static variable for totalPennies, but later retracts this suggestion, stating it is unnecessary.
  • Another participant suggests that the totalPennies variable can be eliminated altogether, providing a streamlined version of the function that still adheres to the assignment requirements.

Areas of Agreement / Disagreement

Participants express differing views on the use of recursion versus iteration, with no consensus reached on the best approach to solve the problem. The discussion remains unresolved regarding the optimal implementation of the function.

Contextual Notes

There are unresolved issues regarding the handling of the totalPennies variable and the implications of using static versus non-static declarations in the context of recursion.

ksepe
Messages
5
Reaction score
0
Write code to complete DoublePennies()'s base case. Sample output for below program:
Number of pennies after 10 days: 1024

The if statement is what I am trying to complete, however this places it in an infinite loop
Code:
#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  */
   
[B]if (numDays==1)[/B] {
   totalPennies=DoublePennies((numPennies*2), numDays);
}
   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;
}
 
Technology news on Phys.org
Why use recursion when a simple for loop will do?
 
greg1313 said:
Why use recursion when a simple for loop will do?

The code was provided.. I just have to complete the base case which is what I cannot figure out...
 
I should have guessed. Anyway, the reason you're getting an infinite loop is that you are calling DoublePennies with numDays = 1 and numDays is not being changed. See?

Recursion can be tricky (that's why I asked about the for loop) but it's handy (as you may well know). Here's the code I'd use and I'll leave you to make another attempt in your other post and to post your work if you get stuck.

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

		static long long totalPennies;

		if (numDays == 1)
			return numPennies * 2;
		else {
			totalPennies = numPennies * 2;
			DoublePennies(totalPennies, numDays - 1);
		}
   	
	}

Note the static declaration for totalPennies.

If there's problems with that please post back here.
 
Actually, the static declaration isn't necessary. Sorry about that. :o

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

		long long totalPennies = 0;

		if (numDays == 1)
			return numPennies * 2;
		else {
			totalPennies = numPennies * 2;
			DoublePennies(totalPennies, numDays - 1);
		}
   	
}

This is correct and is in keeping with what was given by the assignment, though it's not necessary to initialize totalPennies. In fact, you could eliminate totalPennies altogether:

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

		if (numDays == 1)
			return numPennies * 2;
		else 
			DoublePennies(numPennies * 2, numDays - 1);
}
 

Similar threads

Replies
5
Views
6K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
14
Views
4K
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 66 ·
3
Replies
66
Views
6K