### Fibonacci Sequences

I am having problems with my code to find the answer to Project Euler problem 25. I already found the answer online for troubleshooting, but I still can't get the correct answer. What's wrong with my code?

 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 #include using namespace std; //function prototype void sequence(int [1000][3]); int main() { //declare variables int num[1000][3]; int count = 1; for(int x = 0; x < 1000; x++) { num[x][0] = 0; num[x][1] = 0; num[x][2] = 0; }//end for num[999][1] = 1; num[999][2] = 1; while(num[0][2] == 0) { sequence(num); count++; }//end while cout << count << endl; return 0; } void sequence(int n[1000][3]) { for(int x = 999; x >= 0; x--) { n[x][0] = n[x][1]; n[x][1] = n[x][2]; n[x][2] = n[x][1] + n[x][2]; }//end for for(int x = 999; x > 0; x--) { if(n[x][2] > 9) { n[x - 1][2]++; n[x][2] -= 10; } }//end for }//end sequence
Last edited on
what is wrong with this ? void sequence(int [1000][3]);

Also, what is the expected output ?
Last edited on
The expected output is the first number in the fibonacci sequence to have 1000 digits. Not the actual number but the n in F(n).

I don't see anything wrong with my prototype.
I asked because u have 2 dim array num[1000][3]. U can do everything with a 1dim num[1000] array and it'd be much less complicated.

OK, replace line 11 with this and see if it compiles:
int [1000][3];
Last edited on
You set the entire 2d array to zeros, and then make [999][1] and [999][2] equal to 1. Your sequence makes [999][0] = 1, [999] = 1, and [999][2] = 2. However, when it loops it starts dealing with [998], which is all zeros.

Hmm, I guess since you keep looping on those [999], your integer is certainly overflowing at some point.

Your solution (a 2d array of ints) isn't going to work too well. You need an integer with 1000 digits, which is how many bytes, much more than four. char number[1000]; would be enough to store the number you need, you just need to program a way to deal with those char like they were a base ten number.
Last edited on
Topic archived. No new replies allowed.