Some help on a program

Started by
41 comments, last by Blckknight118 13 years, 6 months ago
Quote:Original post by empirical2
Quote:Original post by Blckknight118
Ok I got the program to add every 5 times using this code:
while(crud_days % 5 != 0 && crud_days % 5 != 1)


When I asked you how many growths there would be in 30 days, is that how you worked it out in your head?

How did you get 6 from 30?

Quote:I really don't understand Fibonacci's numbers though I used this formula and it didn't work.


Your original code was closer.

I suggest you wiki Fibonacci's numbers, you cannot tell the computer how to do it, until you understand them yourself.


For the growth, I simply divided by 5.... For every 5 days, you do Fibonacci's code. So if the user enters in 15 days, Fibonacci's code is done 3 times because the amount of growth grows at a rate of every 5 days using Fibonacci's code.

My original code was closer....Ok thanks
Advertisement
Quote:Original post by Blckknight118

For the growth, I simply divided by 5....


Right!. So if you wanted your function to use the formula once for every 5 days....how would you do that?

Lets look at the loop
        for(int i = 1; i <= crud_days; i++)    {        do{            amount_crud = amount_crud + amount_crud;            numberof_crud = amount_crud;        }        while(crud_days % 5 == 0);	}return numberof_crud;}


Here you have two loops, and you run through a loop (the 'for' loop) that iterates once for EVERY day...but... don't we want to only do it once for every five days? Can that for loop be made to loop only once for every five days?

And if we can make the for loop only once for every 5 days......do we actually need that other do-while loop at all? ;)


Quote:My original code was closer....Ok thanks


I'm sorry, I don't really know what help to give other than the answer and that is not permitted.

However I will hint that it will require an additional variable because the order you do each part of the calculation is very important.

But i have to go to bed now as its 4am! I wish you the very best of luck :)
Quote:Original post by empirical2
Quote:Original post by Blckknight118

For the growth, I simply divided by 5....


Right!. So if you wanted your function to use the formula once for every 5 days....how would you do that?

Lets look at the loop
*** Source Snippet Removed ***

Here you have two loops, and you run through a loop (the 'for' loop) that iterates once for EVERY day...but... don't we want to only do it once for every five days? Can that for loop be made to loop only once for every five days?

And if we can make the for loop only once for every 5 days......do we actually need that other do-while loop at all? ;)


Quote:My original code was closer....Ok thanks


I'm sorry, I don't really know what help to give other than the answer and that is not permitted.

However I will hint that it will require an additional variable because the order you do each part of the calculation is very important.

But i have to go to bed now as its 4am! I wish you the very best of luck :)


Thank you so much for the help, you have no idea how much I appreciate it. Good night, and I hope I figure it out!

I'll give you this much to go on:

The Fibonacci sequence requires that you always know what the previous two numbers were. Any number in the sequence n is the sum of the (n-1) value and the (n-2) value (except for the base cases where n-1 or n-2 are less than 1). Those are indexes, not raw values. You were on to something with the amount_crud = amount_crud-1 + amount_crud-2 line, but think about what that actually means vs. what you're trying to add up.

Also, the following line:

if(amount_crud > 1) return amount_crud > 1;

doesn't do what you're thinking. The > operator returns a boolean, so you're saying here "if amount_crud > 1 is a true statement, return 'true'."

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Thanks everyone, but I gave up. I don't know how to manipulate Fibonacci's algorithm. I've spent about 12 hours on this and nothing clicked or anything so I'm done.

Edit: If anyone would mind correcting my code so I can see the correct way, it would be appreciated.

[Edited by - Blckknight118 on October 19, 2010 12:36:56 PM]
Never give up. =)

Do you know how integer division works?
You can use integer division to find out how many times the crud grows. For example:
29 days: growths = 29 / 5 = 5
30 days: growths = 30 / 5 = 6
31 days: growths = 31 / 5 = 6
32 days: growths = 32 / 5 = 6
33 days: growths = 33 / 5 = 6
34 days: growths = 34 / 5 = 6
35 days: growths = 35 / 5 = 7

ints always use integer division, so you can do:
int growths = crud_days / 5;

Then you know how many times it grows.

Don't try to solve the whole thing at the same time. Start by writing two simpler programs, one that only calculates how many times the crud grows, and one that only calculates the Fibonacci sequence. Then when both these work perfectly, you can work on combining them.
Quote:Original post by Erik Rufelt
Never give up. =)

Do you know how integer division works?
You can use integer division to find out how many times the crud grows. For example:
29 days: growths = 29 / 5 = 5
30 days: growths = 30 / 5 = 6
31 days: growths = 31 / 5 = 6
32 days: growths = 32 / 5 = 6
33 days: growths = 33 / 5 = 6
34 days: growths = 34 / 5 = 6
35 days: growths = 35 / 5 = 7

ints always use integer division, so you can do:
int growths = crud_days / 5;

Then you know how many times it grows.

Don't try to solve the whole thing at the same time. Start by writing two simpler programs, one that only calculates how many times the crud grows, and one that only calculates the Fibonacci sequence. Then when both these work perfectly, you can work on combining them.


Ok so the forumula int growths = crud_days / 5 shows how many times the crud grows. That's basically the condition for a statement for Fibonnaci's sequence to iterate, right?

And for the algorithm of Fibonacci's sequence, would this work:
int fibonacci(int n)
{
int u = 0;
int v = 1;
int i, t;

for(i = 2; i <= n; i++)
{
t = u + v;
u = v;
v = t;
}
return v;
}

I googled this...
Hello Blckknight118, I just went through this entire thread.

Your last post is correct. Google has given you the solution for the fibonacci sequence. But do you understand how it works?

The Fibonacci sequence is 0 1 1 2 3 5 8 13 21...

See that first it starts at 0 and 1, and after that, every number is the sum of the previous two numbers?

That should give you an idea about what you need to do with your problem. Your crud grow every 5 days. So you get number of fibonacci iterations by Growth=crud_days/5 which I see you have already obtained.

Now try setting up a loop that iterates for "Growth" number of times, and try and replicate the above fibonacci sequence, with the only difference being your sequence does not start from 0 or 1, but from 10.

So after 5 days, it still stays at 10, after 10, it becomes 10 + 10 (previous 2 amounts) etc.

I hope that was helpful :)
I've been hesitant to post on this topic, as it is already receiving lots of attention, but rather than posting a question asking if the snippit works, and waiting for the responses, TRY IT! The great thing about math is that the answer is there, you just need to follow the path.

int fibonacci(int n){int u = 0;int v = 1;int i, t;for(i = 2; i <= n; i++){t = u + v;u = v;v = t;}return v;}


What does your function return with these inputs: 1, 2, 3, 4?
Is this the expected sequence?
What about Fibonacci(0), or fibonacci(-1)?

The more questions you can ask yourself, and answer yourself, the faster you will become proficient in problem solving.


Be aware that were you to use that function, your instructor will almost certainly guess that you didn't write it. You should try write it yourself. You might find it helpful to use that function as a reference, but the more you depend on it the more obvious it will be that your solution is not original work.

This topic is closed to new replies.

Advertisement