MHB Completing DoublePennies(): Solving the Infinite Loop

  • Thread starter Thread starter ksepe
  • Start date Start date
  • Tags Tags
    Infinite Loop
Click For Summary
The discussion revolves around completing the base case for the recursive function DoublePennies, which calculates the number of pennies after a given number of days. The initial code leads to an infinite loop due to improper handling of the base case. The correct implementation requires checking if numDays equals 1, in which case the function should return numPennies multiplied by 2. If numDays is greater than 1, the function should recursively call itself with numPennies doubled and numDays decremented by one. The conversation also touches on the use of recursion versus a for loop, with some participants noting that while recursion can be complex, it is a useful approach for this problem. Suggestions include eliminating unnecessary variables and ensuring the function correctly returns values at each stage 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);
}
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

Replies
5
Views
6K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
14
Views
3K
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
1K
  • · Replies 66 ·
3
Replies
66
Views
5K