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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>

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.