-
March 4th, 2012, 01:29 AM
#1
In need of assistance, please.
I do not know the first place to begin on this project. Here's what I have been asked to do:
Leonardo Fibonacci is known for discovery of the number pattern in nature:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Where the next number is determined by adding the two preceding values. The sequence begins with the first two number being 1 and 1.
I need to ask the user which Fibonacci number they would like to calculate and print the appropriate number. For example: The 6th Fibonacci number is 8.
Help would be greatly appreciated!
-
March 4th, 2012, 05:37 AM
#2
Re: In need of assistance, please.
You get a pen and paper and write out in detail how you would do it if I asked you for the nth number. Do not assume you have a list of the numbers.
The instructions tell you how to calculate the series of numbers so you just need to keep looping around the calculation until you get to the nth loop.
-
March 4th, 2012, 11:25 AM
#3
Re: In need of assistance, please.
Ok.. I've done this one in C some time ago and it was one of my biggest efforts in programming since i only knew about printf and getch.. So I've reached a solution after quite some work. I cant give you the code( it's written in C anyways ) but i can give you a tip that saved my nerves
You need to use 4 "cup" or "glass" variables. Just pass the info between them and it's gonna be a piece of cake
-
March 4th, 2012, 11:48 AM
#4
Re: In need of assistance, please.
Originally Posted by cens
You need to use 4 "cup" or "glass" variables.
What are "cup" and "glass" variables?
-
March 4th, 2012, 12:34 PM
#5
Re: In need of assistance, please.
They are called from the famous example of the two cups used to describe variables to a total beginner.
Glass variables are variables that are often used to transfer data between them.
The best example is the fibbonnaci codex, and I cant figure out any example that will put the concept in the correct light;
But this is how it works:
In an iteration, you may need, at a point, to use a variable as a storage matter. I dont want to make any contact with the fibbonacci codex, so i posted my latest use of "cup" variables.
This chunk of code checks for a double quote sequence to detect strings.
"string" code "string"
so in order to omit the code part ( which is also between 2 quotes) i used a "glass variable".
Code:
while(true){
firstIndex = outputCommentFree.indexOf('"', lastIndex);
lastIndex = outputCommentFree.indexOf('"', firstIndex+1);
if(firstIndex == -1|| lastIndex == -1)break;
betweenCommas = outputCommentFree.substring(firstIndex, lastIndex+1);
betweenCommas2 = outputCommentFree.substring(firstIndex, lastIndex+1);
betweenCommas2 = betweenCommas.replaceAll("\\{\n", "{");
betweenCommas2 = betweenCommas.replaceAll("\\}\n", "}");
betweenCommas2= betweenCommas.replaceAll(";\n", ";");
outputCommentFree.replace(betweenCommas,betweenCommas2);
System.out.println("exec");
firstIndex = outputCommentFree.indexOf('"', lastIndex);
lastIndex = outputCommentFree.indexOf('"', firstIndex+1);
}
"firstIndex" and "lastIndex" are the variables that you need to check
As you see i juggle with their values to get the correct position of the quotes..
Thats it with glass variables - variables that hold a value for a limited time and pass it back at a point.
Oh, yes Virtuos you may want to skip the code part since its a little confusing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|