Hi guys. I will explain the problem I have in my own words.

I have to make a program using the Fibonacci numbers, in which the user inputs a number of days and the original size a green crud population, and every 5 days the population changes by fibonacci numbers. I went to the professor's office, and he gave me some hints, that I must divide the number of days entered by 5, and that is the number of iterations that I should run the loop. 1st of all I really dont know how to do a loop in which I run several times for that asked... and the other problem is that the loop must be made in a function, and I have never done a function before and I have some problems on the compiler, so if you guys could help me out.

The professor also said that to calculate the fibonacci number using a certain amount of time the formula I should use for my function is:

fib(iter) = (fib(iter - 1) + fib(iter - 2))

BTW, fib(0) = sizeCrud, fib(1) = sizeCrud... and then I use that formula

So the problem I have is that I should do a loop that repeats 'iter' amount of times, and also it should be made on a function, and I really do not know how!!!

I will put my code in here so you guys can help me.

#include<iostream>
using namespace std;

double fib(int iter);
//Calcules the size of the green crud population after different 'iter' amount of time

int main()

{
	double sizeCrudPopulation, finalSize;
	int numberDays, iter; 
	char ans;
	
	do
	{
		cout << "We are about to help you find the size in pounds of a ";
		cout << "green crud  population after a certain amount of days.\n";
		cout << "Enter the initial size of the green crud population in pounds:\n";
		cin >> sizeCrudPopulation;
		cout << "Now enter the number of days to see the size of the green crud:\n";
		cin >> numberDays;

		iter = numberDays / 5;

		finalSize = fib(iter);

		cout.setf(ios::fixed);
		cout.setf(ios::showpoint);
		cout.precision(2);

		cout << "The size of the green crud population after " << numberDays;
		cout << " days is " << finalSize << " pounds.\n";
		cout << endl;
		cout << "Do you want to run this program again? Press 'Y' for yes, or 'N' for no.\n";
		cin >> ans;
	} while (ans == 'Y' || ans == 'y');

	cout << "Have a nice day!\n";
	return 0;
}


double fib(int iter)
{

	while (iter >= 0)
	{
		while (iter >= 0)
		{
		fib(0) = sizeCrudPopulation;
		}
		while (iter >= 1)
		{
		fib(1) = sizeCrudPopulation;
		}
		while (iter >= 2)
		fib(iter) = (fib(iter - 1) + fib(iter - 2));
	}
}

This code is wrong... because I am not using a loop that repeats iter amount of times... and even my function is bad... help please!! and BTW, remember that Im a beginner, only use int main(), not other strange things to me

Help plz!!

double fib(int iter)
{

	while (iter >= 0)
	{
		while (iter >= 0)
		{
		fib(0) = sizeCrudPopulation;
		}
		while (iter >= 1)
		{
		fib(1) = sizeCrudPopulation;
		}
		while (iter >= 2)
		fib(iter) = (fib(iter - 1) + fib(iter - 2));
	}
}

Most of your code (not the above function) looks good. The statement fib(0) = sizeCrudPopulation; is not valid as fib() is a call to a function. You could use an array and then use fibarr[iter] = ..., but as you only need the value from the previous 2 states, you can just have 2 variables for the previous states and update them with each iteration, which will make it much simpler. And make sure you decrease the iterator with each loop.

Something like:

double fib(int iter)
{
   double fib_1 = 1., fib_2 = 0.;   // the old state values (t-1 and t-2)
   double fib_0 = 1.;               // the current state
   
	while (iter > 0)
	{
      // calculate the new value
      fib_0 = fib_1 + fib_2;

      // update the old states for the next iteration
      fib_2 = fib_1;
      fib_1 = fib_0;

      // reduce the number of remainning iterations
      iter = iter - 1;     
   }
   
	return fib_0;
}

might do the trick. I haven't tested it. Also, I would have thought that the final size would be the initial size multiplied by the fibonacci number obtained..

Hey Dougy, the program compiled and I ran it... but it gave me a wrong number... so there's a problem with the algorithm...

double fib(int iter)
{
   double fib_1 = 1., fib_2 = 0.;   // the old state values (t-1 and t-2)
   double fib_0 = 1.;               // the current state
   
	while (iter > 0)
	{
      // calculate the new value
      fib_0 = fib_1 + fib_2;

      // update the old states for the next iteration
      fib_2 = fib_1;
      fib_1 = fib_0;

      // reduce the number of remainning iterations
      iter = iter - 1;     
   }
   
	return fib_0;
}

I mean... how do I calculate with the size in pounds entered by the user, if it is not used in the function?

Thanks a lot for the help!!

I mean... how do I calculate with the size in pounds entered by the user, if it is not used in the function?

Thanks a lot for the help!!

Confused :confused:

Remember that the initial value of the size crud population is the one the user inputs... its not exactly 0, so we need to insert that value on the function... and I dont know what to do

Might someone help me?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.