13 Replies - 3624 Views - Last Post: 03 December 2010 - 09:24 PM Rate Topic: -----

#1 Guest_Nagasami*


Reputation:

Population increase using recursive

Posted 24 November 2010 - 10:44 PM

Hello, im having problem in creating a code to printout the population of the organisms each day as it increases. Everyday it will increase by 2 which meant 2/100. so by using the loop normally i could do this problem but since i have to integrate recursive its kinda difficult for me since i dont know what i should put for the base case and the recursive case.

Basically i have the main method and two other methods
displayPopulation method will display the heading and call the showPopulation to print out the population of organism each day.

import java.util.Scanner;
public class Population
{
	public static void main(String[] arg)
	{
		double neworgan,organ,increase;
		int count=1;
		double day;



		//create a scanner object
		Scanner keyboard = new Scanner(System.in);

		System.out.println("Enter the starting # of organisms: ");
		organ=keyboard.nextInt();

		System.out.println("Enter the increase rate: ");
		increase=keyboard.nextInt();

		System.out.println("Enter the total number of days: ");
		day=keyboard.nextInt();




		neworgan=organ;
		for(int i=1;i<day;i++)
		{
			neworgan+=(organ*increase/100);
		}

		System.out.println(showPopulation(day,count));

	}

	public static double displayPopulation(double d,int c)
	{
		System.out.print("Day " + "\t\t\t" + "Organisms" + "\n--------------------------------");
		return showPopulation(d,c);


	}

	public static double showPopulation(double d,int c)
	{

		if(c>d)
			return ;
		else
		{
			return showPopulation(d,c+1);
		}
	}
}



So far this is what i have and im stuck at the recursive method. What the recursive method should do is print out the population on each day that was calculated in the main method

If someone could help and lead me to the right direction in the recursive method..Thanks :)

Is This A Good Question/Topic? 0

Replies To: Population increase using recursive

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Population increase using recursive

Posted 24 November 2010 - 11:01 PM

well, you said that the number of organisms will increase by some percentage each day. So that means your recursive algorithm will work "day" number of times. Think about that, and there you have your base case :)

This post has been edited by mostyfriedman: 24 November 2010 - 11:05 PM

Was This Post Helpful? 1
  • +
  • -

#3 Guest_Nagasami*


Reputation:

Re: Population increase using recursive

Posted 24 November 2010 - 11:17 PM

so does that meant let say my count=1

my base case would be
if(count==day)
return day;

? im confuse in what to return if count reach the number of day
Was This Post Helpful? 0

#4 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Population increase using recursive

Posted 24 November 2010 - 11:22 PM

well, think about it this way.. if i tell you to increase the number of dollars in a piggy bank by a dollar a day, and i want you to do this for 30 days. first day comes and you put a dollar in the piggy bank, and then you think to yourself "ok, 29 days to go".. on the next day you say "ok, 28 days to go". get it now ;)??
Was This Post Helpful? 1
  • +
  • -

#5 Guest_Nagasami*


Reputation:

Re: Population increase using recursive

Posted 24 November 2010 - 11:30 PM

so which mean in my base case i would have to return the lowest day? which is 1.
Was This Post Helpful? 0

#6 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Population increase using recursive

Posted 24 November 2010 - 11:37 PM

no, its 0..if i tell you to increase the number of dollars for 0 days then you're trivially done because you don't have to do anything. whenever you do recursion, think of the most trivial case possible, that is usually the base case.
Was This Post Helpful? 1
  • +
  • -

#7 Guest_Guest*


Reputation:

Re: Population increase using recursive

Posted 24 November 2010 - 11:42 PM

base case would be
if(count==day)
return 0;

but dont i have to set day to 0 first before returning it so it can go into recursion?
Was This Post Helpful? 0

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Population increase using recursive

Posted 24 November 2010 - 11:49 PM

no, forget the count

PopulationsIncrease(days, increment, numOrganisms)
IF days == 0
return numOrganisms

return PopulationsIncrease(days-1, increment, numOrganisms+increment)
END

something like this should help you understand.
Was This Post Helpful? 0
  • +
  • -

#9 Guest_Guest*


Reputation:

Re: Population increase using recursive

Posted 25 November 2010 - 12:01 AM

I see when days=0 it will return that numOrganism passed from main method into the method.

but the second return PopulationIncrease that you wrote..i assume that is or part of the recursive case.

since our numOrganism+=numOrganism*2/100
how is that suppose to be pass back into the recursive case each time?

What is numOrganism+increment do to help return the different Organism population of that day.

I guess what im trying to say is. With in the recursive case how can i return the numOrganism calculated in the main method? Is there a way or that would need to be calculate from within the recursive case?
Was This Post Helpful? 0

#10 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: Population increase using recursive

Posted 25 November 2010 - 12:14 AM

View PostGuest, on 25 November 2010 - 09:01 AM, said:

I see when days=0 it will return that numOrganism passed from main method into the method.

yes, since the number of days is 0, i will just print the current number of population without increasing it.

Quote

but the second return PopulationIncrease that you wrote..i assume that is or part of the recursive case.

yes, it is the recursive case.

Quote

since our numOrganism+=numOrganism*2/100
how is that suppose to be pass back into the recursive case each time?

when you call the method in the recursive case, pass them as parameters.

Quote

What is numOrganism+increment do to help return the different Organism population of that day.

on each recursive call, you increment the population by that number. Each recursive call represents 1 day, so on each recursive call you calculate the new size of the population.

I think this is enough to help you understand this problem since this is a very trivial problem to solve using recursion, if you still do not understand it, then i advise you to read on the basics of recursion and see some easy solved examples before you attempt to solve one on your own.
Was This Post Helpful? 0
  • +
  • -

#11 Guest_Guest*


Reputation:

Re: Population increase using recursive

Posted 25 November 2010 - 12:19 AM

thanks for all your help :bigsmile:
Was This Post Helpful? 0

#12 Guest_Nagasami*


Reputation:

Re: Population increase using recursive

Posted 25 November 2010 - 12:23 AM

View PostGuest, on 24 November 2010 - 11:19 PM, said:

thanks for all your help :bigsmile:


oh just one more question.
How do i pass those numOrganism over from main to the method since they are not label like array[i], how does java know exactly which numOrganism to pass?
Was This Post Helpful? 0

#13 dunsta  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 79
  • Joined: 08-April 10

Re: Population increase using recursive

Posted 29 November 2010 - 05:54 AM

Hi, I happen to be doing the same problem from the same text as you.
I hit a mental block when writing the algorithum for this problem, but I came across this thread and with mostyFriedmans helpful post, my head cleared and it was pretty straight forward.

The numOrganisms are stored in memory where the function returns, this is the extra overhead that the book you are studying mentions. The overhead recursion has that an increment loop doesn't have. (I think..)
You seem a bit confused between the difference of an array element and a variable. Pass the numOrgs as a normal argument in the method.

post#8 is the help you need, just translate it into code, but use your equation orgs+=orgs*increment as the argument for organisms.

The Gaddis text you are using is great, I am really enjoying studying from this book.
Was This Post Helpful? 1
  • +
  • -

#14 Guest_Nagasami*


Reputation:

Re: Population increase using recursive

Posted 03 December 2010 - 09:24 PM

Ya i got it too,
it see i made it much more complicated than what it suppose to be.
all i had to do was if day equal to 0 then return the original numoforganism. Then recursive happen and calling the method again. I guess i was confused with the idea of recursive and how to actually use it, but i got it after looking through it with help from mostyfriedman

Thanks :)
Was This Post Helpful? 0

Page 1 of 1