Completing DoublePennies(): Solving the Infinite Loop

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

The forum discussion focuses on completing the base case of the recursive function DoublePennies() in C, which calculates the number of pennies after a specified number of days. The main issue identified is an infinite loop caused by not properly reducing the numDays parameter. The correct implementation involves returning numPennies * 2 when numDays equals 1, and recursively calling DoublePennies() with numPennies * 2 and numDays - 1 otherwise. The final solution eliminates the need for a static variable and initializes totalPennies directly in the return statement.

PREREQUISITES
  • Understanding of recursion in programming
  • Familiarity with C programming language syntax
  • Knowledge of function return values and parameters
  • Basic concepts of loops and conditional statements
NEXT STEPS
  • Study recursion techniques in C programming
  • Learn about the differences between recursion and iteration
  • Explore optimization strategies for recursive functions
  • Practice writing recursive functions with varying base cases
USEFUL FOR

Programmers, computer science students, and software developers interested in mastering recursion and improving their C programming skills.

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
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
2K
  • · Replies 66 ·
3
Replies
66
Views
6K