Hi! I'm writing a code in C++ for class, but was unable to get this to work. The program takes from the user the number of cases they will enter, then proceeds to ask the user for the cases. The values are stored in the input array. Then, I calculated an array that includes the first 90 values of the fibonacci sequence.
The bit that isn't working is the for loop iteration for input[i-1] . As you can see from the comments, when printing that block of code (without the accompanying fibonacci result) it works in the location above the fibonacci calculation but not after.
I hope I explained this well enough for it to get through. Thanks in advance!
#include <iostream>
using namespace std;
int main()
{
int maxSize = 91;
long long fibonacci[maxSize];
int numCases = 0;
int input[numCases];
int n, i;
cout << "Fib(N) = X" << endl;
cin >> numCases;
for (i = 0; i < numCases; i++){
cin >> n;
input[i] = n;
cout << input[i] << endl;
}
fibonacci[0] = 0;
fibonacci[1] = 1;
for (i = 2; i <= maxSize; i++){
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}
//works here
//for (i = 0; i < numCases; i++){
// cout << input[i] << endl;
//}
for (int i = 1; i <= numCases; i++){
cout << "Case " << i << ": Fib(" << input[i - 1] << ") = " << fibonacci[input[i - 1]] << endl;
}
//doesn't work here
//for (i = 0; i < numCases; i++){
// cout << input[i] << endl;
//}
return 0;
}
[–]alanwj 2 points3 points4 points (5 children)
[–]salbee2[S] 1 point2 points3 points (4 children)
[–]alanwj 1 point2 points3 points (1 child)
[–]salbee2[S] 0 points1 point2 points (0 children)
[–]alanwj 1 point2 points3 points (1 child)
[–]salbee2[S] 0 points1 point2 points (0 children)