Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Assignment compiles but output is wrong. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Assignment compiles but output is wrong." Watch "Assignment compiles but output is wrong." New topic
Author

Assignment compiles but output is wrong.

taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
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.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3598
    
  14

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

Joined: Apr 20, 2011
Posts: 231
    
    1

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?
taylor Lynch
Greenhorn

Joined: Dec 05, 2012
Posts: 24
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.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11160
    
  16

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
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: Dec 05, 2012
Posts: 24
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.
huy Binh
Greenhorn

Joined: Apr 03, 2012
Posts: 1
//try :
if(n ==0 || n == 1)return 1;
while (index<n)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assignment compiles but output is wrong.
 
Similar Threads
Fibonacci Sequence Problem
Online Java Editor/Compiler?
Java Assignment Problem
Recursion of Fibonacci Numbers
Displaying a histogram using input from an array