CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

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!

2. Elite Member Power Poster
Join Date
May 2006
Location
UK
Posts
4,473

## 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.

3. Member
Join Date
Nov 2011
Posts
189

## 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

4. Member +
Join Date
Jan 2009
Posts
596

## 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?

5. Member
Join Date
Nov 2011
Posts
189

## 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
•