Do I have an Issue with my For Loops

I have this 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

#include <iostream>
#include<iomanip>
#include<cmath>

using namespace std;

int main()
{

    int n;
    int x[91];


    cin>>n;
    int cases[n];


    for(int i=0; i < n; i++)
    {
        cin >> cases[i];
    }

    x[0]=0;
    x[1]=1;

    for(int i = 2; i<91; i++)
    {
        x[i] = x[i-1] + x[i-2];
    }

    for(int i=0; i < n; i++)
    {

        cout << "case " << i+1 << ": " ;
        cout << "Fib(" << cases[i] << ") = " << x[cases[i]] << endl ;
    }

    return 0;
}


the objective of this code is to:
1
2
3
4
5
6
Write a program that reads an integer N 
and print then prints the Nth value 
in the Fibonacci sequence. 
Remember that the first elements 
of the Fibonacci series are 0 and 1
 and each next term is the sum of the two preceding it.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
The first line of the input contains a single integer T, 
indicating the number of test cases. 
Each test case contains a 
single integer N (0 <= N <= 90), 
corresponding to the N-th 
term of the Fibonacci sequence.

For each test case in the input,
print the case number
print the message "Fib(N) = X", 
where X is the N-th term of the Fibonacci series.
Special Requirement
Your program must use an array
to store the first 91 values in the fibonacci sequence.


Desired output:
1
2
3
4
5
6
7
8
9
10
Input Sample
3
0
4
2
Output Sample:

Case 1: Fib(0) = 0
Case 2: Fib(4) = 3
Case 3: Fib(2) = 1


Now my code is running correctly,
but when i sample input numbers are large the output is incorrect.
1
2
3
4
5
example 
my output:
case 1: Fib(52) = -1408458269
desired output:
case 1: Fib(52) = 32951280099


I believe that this part of my code is not correct:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    x[0]=0;
    x[1]=1;

    for(int i = 2; i<91; i++)
    {
        x[i] = x[i-2] + x[i-3];
    }

    for(int i=0; i < n; i++)
    {
        //OUTPUT:
        cout << "case " << i+1 << ": " ;
        cout << "Fib(" << cases[i] << ") = " << x[cases[i]] << endl ;
    }

If anyone can help thanks



Last edited on
It's an integer overflow problem after fib(20).
Try using std::uint64_t in cstdint instead. That should work till fib(92)
https://en.cppreference.com/w/cpp/types/integer

For even bigger numbers you need a BigInteger class.
This is illegal:

11
12
13
14
15
16
    int n;
    int x[91];


    cin>>n;
    int cases[n];


In standard C++, the size of an array on the stack must be known at compile-time, so it must be a compile-time constant (like you do for x at line 12). You can't declare the size using a variable like n that is only known at run-time.

Presumably, you are using a compiler that allows this as an extension, but you really should be learning to code standard C++, not some proprietry extension.

Last edited on
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
#include <iostream>
#include <vector>

constexpr size_t noFib {91};

int main() {
	size_t n {};
	unsigned long long x[noFib] {};

	std::cin >> n;

	std::vector<size_t> cases(n);

	for (size_t i = 0; i < n; )
		if ((std::cin >> cases[i]) && (cases[i] >= noFib))
			std::cout << "Must be less than " << noFib << '\n';
		else
			++i;

	x[1] = 1;

	for (size_t i = 2; i < noFib; ++i)
		x[i] = x[i - 1] + x[i - 2];

	for (size_t i = 0; i < n; ++i) {
		std::cout << "case " << i + 1 << ": ";
		std::cout << "Fib(" << cases[i] << ") = " << x[cases[i]] << '\n';
	}
}


Using std::vector instead of a c-style array - which can be sized at run-time.
Topic archived. No new replies allowed.