1 Replies - 261 Views - Last Post: 29 April 2013 - 05:31 PM Rate Topic: -----

#1 klrre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-March 13

Recursive; n cents with quarters, dimes, nickels...counter help

Posted 29 April 2013 - 04:31 PM

I was hoping to get some helps with my recursion work. I know it's probably a simple solution. Basically I need to count how many times each part of the code runs and have it print; however, everytime I put it into the loop I end up with either a loop that prints out each iteration rather than the total..or an inifinite loop of 1's. (at least those are the two things that happen everytime i change my code). Any help on how to get a counter to work would be much appreciated. I've commented in where I thought they should go...
public class denomination{

public static int makeChange(int c, int denom) {
	//int count=0? but would reset for every iteration. should it go outside instead?
		int next_denom = 0;
			switch (denom) {
		case 25:
			next_denom = 10;
			//count ++;
		break;
		case 10:
			next_denom = 5;
			//count ++;
		break;
		case 5:
			next_denom = 1;
			//count ++;
		break;
		case 1:
		return 1;
		}
	int ways = 0;
	for (int i = 0; i * denom <= c; i++) {
		ways += makeChange(c - i * denom, next_denom);
	
		}
	return ways;
	//return count;?
	}

public  static void main(String []args){

	System.out.println(makeChange(1000, 25));	
	
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Recursive; n cents with quarters, dimes, nickels...counter help

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3640
  • View blog
  • Posts: 12,613
  • Joined: 12-December 12

Re: Recursive; n cents with quarters, dimes, nickels...counter help

Posted 29 April 2013 - 05:31 PM

It could be a static variable of the class:

public class Denomination {
    static int count;    // initialized to 0 once only

(Java classes should be capitalized).

You can examine the value using the class-name: Denomination.count

The tricky thing is how, and when, to reset it to 0.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1