Hello everyone. I'm working on an assignment for my computer science class and I can't for the life of me figure out why my array has the number 16 in the first index.
My goal with this function is for the user to enter the command "N" followed by a number to add a number to the array. The code I have for this works, however it changes the 16 added by default to a 10 and display it at the end of the array. When tested without putting in any numbers, the 16 is there by default. What do I need to change in my code to avoid this? Thanks for any help!
int arraySize = 1;
int statsArray[arraySize];
arraySize++;
The size of an array must be known at compile time, so the size of an array is constant. In this case, the array will only have a size of 1. When you try to access the array with an index greater than arraySize - 1, you aren't actually accessing what's stored in the array, but some junk values next to the array in memory. As a result, your program will not run as expected and usually crash. You just got lucky, and didn't crash.
Thank you for your reply! Unfortunately, we haven't gone over vectors in class yet, so I can't use them. I did however, based on your response, create a fixed sized frequency array that I'm toying around with.