Completing DoublePennies(): Solving the Infinite Loop

In summary, the conversation was about completing the base case of the DoublePennies() function which caused an infinite loop when called with numDays = 1. The expert suggested using recursion and provided code to handle the base case. The conversation also mentioned the use of static declaration for totalPennies, but it was later clarified that it is not necessary.
  • #1
ksepe
5
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
  • #2
Why use recursion when a simple for loop will do?
 
  • #3
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...
 
  • #4
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.
 
  • #5
Actually, the static declaration isn't necessary. Sorry about that. :eek:

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);
}
 

Related to Completing DoublePennies(): Solving the Infinite Loop

1. How does the "Completing DoublePennies()" function solve the infinite loop?

The "Completing DoublePennies()" function uses a control structure, such as a while loop, to continuously check and update the value of a counter variable that keeps track of the number of iterations. Once the counter reaches a certain value, the loop is terminated, thereby preventing an infinite loop.

2. What are some potential causes of an infinite loop in the "Completing DoublePennies()" function?

An infinite loop in the "Completing DoublePennies()" function can be caused by a mistake in the loop's condition, such as using the wrong variable or forgetting to update the counter variable. It can also be caused by an error in the loop's body, such as an incorrect calculation or an infinite recursive function call.

3. Can the "Completing DoublePennies()" function be used for other problems besides solving infinite loops?

Yes, the "Completing DoublePennies()" function can be adapted to solve other problems that involve a loop with a counter variable. For example, it can be used to limit the number of times a user can input a value, or to iterate through a list until a certain condition is met.

4. How can I test the "Completing DoublePennies()" function to ensure it is working correctly?

To test the "Completing DoublePennies()" function, you can create a simple loop with a known number of iterations and manually check if the function terminates after the correct number of iterations. You can also use debugging tools to step through the code and track the value of the counter variable.

5. Can the "Completing DoublePennies()" function be optimized or improved?

Yes, the "Completing DoublePennies()" function can be optimized by using more efficient control structures or by implementing other strategies, such as storing the values in an array and using the length of the array as the loop's terminating condition. Additionally, error handling can be added to handle unexpected inputs or to prevent potential errors that could lead to an infinite loop.

Similar threads

  • Programming and Computer Science
Replies
5
Views
6K
  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
775
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
4
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
940
Back
Top