# Assignment compiles but output is wrong.

taylor Lynch

Greenhorn

Posts: 24

posted 3 years ago

Yesterday I posted my code saying taht i coudnt get it to compile due to errors and such. So i took a break then came back to it after i thought about it somemore. Last night i got it to compile and for the most part it works. Although when I use input for the code everything works fine until i get to 5 on the fibonacci sequence.

The code Yields: If it's done correctly it should be like this:

(1)2 (1)2

(2)3 (2)3

(3)5 (3)5

(5)13 (5)8

Here is the code:

This is the JDialog.java Im using this code for input and output. This is how the professor wants it done just so everyone knows.

The code Yields: If it's done correctly it should be like this:

(1)2 (1)2

(2)3 (2)3

(3)5 (3)5

(5)13 (5)8

Here is the code:

This is the JDialog.java Im using this code for input and output. This is how the professor wants it done just so everyone knows.

Stephan van Hulst

Bartender

Posts: 5812

61

posted 3 years ago

The algorithm works fine. I think you're confused. 13 is the correct value for the 5th Fibonacci number.

*The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.*

posted 3 years ago

You need to think about what you're trying to do and then look at what you're actually doing. The reason you're getting 13 instead of 8 when you enter 5 is because your program prompts for indexes of the Fibonacci pattern and not the actually values in the pattern themselves.

Take a look at the Fibonacci pattern:

0 1 1 2 3 5 8 13

When n is 1 in your program, the sum is 2. What this means is that your starting index is the 2 of the pattern. When n is 2, it returns the 2nd index (one to the right of 2) which is 3. This part is deceiving because 3 is also the next value in the pattern after 2. But the input 2 is simply referring to the index. When you type is 3, the next value in the pattern, 5, is returned. Once again, 5 happens to be the Finbonacci number after 3, creating a confusing coincidence. When you type in 4, the next value 8 is returned. When you type in 5, the next value 13 is returned. It doesn't return 8 because although 8 comes after 5 in the pattern, the 5 is merely at index of the pattern, not a value in the pattern.

That may have been confusing to follow, but it comes down to this: What do you want your program to do? Is the input number a number from the Fibonacci sequence where the program's job is to find the next number in the pattern. -OR- Is the input number an index of the pattern where the program's job is to find the value at that index of the pattern?

Take a look at the Fibonacci pattern:

0 1 1 2 3 5 8 13

When n is 1 in your program, the sum is 2. What this means is that your starting index is the 2 of the pattern. When n is 2, it returns the 2nd index (one to the right of 2) which is 3. This part is deceiving because 3 is also the next value in the pattern after 2. But the input 2 is simply referring to the index. When you type is 3, the next value in the pattern, 5, is returned. Once again, 5 happens to be the Finbonacci number after 3, creating a confusing coincidence. When you type in 4, the next value 8 is returned. When you type in 5, the next value 13 is returned. It doesn't return 8 because although 8 comes after 5 in the pattern, the 5 is merely at index of the pattern, not a value in the pattern.

That may have been confusing to follow, but it comes down to this: What do you want your program to do? Is the input number a number from the Fibonacci sequence where the program's job is to find the next number in the pattern. -OR- Is the input number an index of the pattern where the program's job is to find the value at that index of the pattern?

taylor Lynch

Greenhorn

Posts: 24

posted 3 years ago

Joel i just want to say i have posted my problem on about three sites I would like to let you know your the first person who has told me whats wrong. Thank you very much. To answer your question i need it to find the next number in the sequence when someone inputs one number. Like if someone put 5 in it should come back with 8.

posted 3 years ago

As someone who spent A LOT my personal time trying to help you, I find this kind of insulting.

taylor Lynch wrote:Joel i just want to say i have posted my problem on about three sites I would like to let you know your the first person who has told me whats wrong. ty very much.

As someone who spent A LOT my personal time trying to help you, I find this kind of insulting.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

posted 3 years ago

You should keep in mind that crossposters are not that respected in this community (BeForthrightWhenCrossPostingToOtherSites <- click). Also please UseRealWords in your posts.

taylor Lynch wrote:Joel i just want to say i have posted my problem on about three sites I would like to let you know your the first person who has told me whats wrong. ty very much.

You should keep in mind that crossposters are not that respected in this community (BeForthrightWhenCrossPostingToOtherSites <- click). Also please UseRealWords in your posts.

The quieter you are, the more you are able to hear.

taylor Lynch

Greenhorn

Posts: 24

posted 3 years ago

Sorry to offend you but this isnt the same as my post from before this is a different problem from before. I listened to what you told me about the comments and thats what got me here so thank you for that but i posted back on the post and no one was responding....So, I worked on it a little more and figured out what increment index was. This isn't the same problem that i asked in my last post it's similar but not the same.