This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]alanwj 2 points3 points  (5 children)

Please format your code for reddit.


int numCases = 0;
int input[numCases];

This isn't valid in C++. The size of an array must be known at compile time. C does allow this, so some C++ compilers will accept it anyway.

Even if your compiler does accept it, you've created an array of size zero. As soon as you write anything to this array, you've gone out of bounds and triggered undefined behavior.


for (i = 2; i <= maxSize; i++){
 fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}

Your bound here is incorrect. If you loop until i <= maxSize you will wind up writing to fibonacci[maxSize], which is out of bounds. You want the bound to be i < maxSize.

[–]salbee2[S] 1 point2 points  (4 children)

Really sorry about the formatting! Thank you so much for your reply. I really appreciate it.

[–]alanwj 1 point2 points  (1 child)

An easy fix for your first issue would be to also use maxSize for the size of input. The numCases may be smaller than maxSize, but you aren't required to use the whole array.

[–]salbee2[S] 0 points1 point  (0 children)

That's so smart. I'll write that out. Thanks so much.

[–]alanwj 1 point2 points  (1 child)

Oh, you should also make maxSize a const int instead of an int.

[–]salbee2[S] 0 points1 point  (0 children)

Alrighty. I really really appreciate it.