MHB Completing DoublePennies(): Solving the Infinite Loop

  • Thread starter Thread starter ksepe
  • Start date Start date
  • Tags Tags
    Infinite Loop
AI Thread 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);
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top