-4

I have searched on this a lot, and have not found any satisfactory response. I am codding a small mathematical algorithm in order to learn a few programming languages, and as such, this is part of my first C++ program. However, I can't seem to fix this array. Although people say random values in arrays are due to the arrays being uninitialized, and that you should initialize them to the value you want like so: int array[sizeOf] = {0}, it only sort of works in this example. Here as you can see, all values of the array get set to 0, exept the very last one, which does not seem to be getting initialized: what is causing this, and how can I fix it?

#include <iostream>
using namespace std;

int initialNumber;

int main()
{
    cout << "Enter Range value:" << endl;
    cin >> initialNumber; //get size

    cout << "Solving..." << endl << endl;

    //initialize basic ints to be used for calculations
    int limiter = initialNumber/2; //the max value we will ever have to multiply by
    int arraySize = initialNumber++; //size of the array
    int resultingFactorsArray[arraySize] = {0}; //main array

    //print each array item value
    for (int x = 0; x <= arraySize; x++) {
        cout << resultingFactorsArray[x] << endl; //print all array values
        //all values printed should be 0, but the last one is uninitialized
    }
}
  • `int resultingFactorsArray[arraySize]` is a variable length array and is not standard. compile using `-pedantic` and you'll get an error. – NathanOliver Dec 18 '18 at 21:30
  • how else can I do it? should I initialize arraySize as a constant? – Sojan Janso Dec 18 '18 at 21:31
  • and the loop goes past the end of the array: elements start at 0 and end at size-1. – rsjaffe Dec 18 '18 at 21:31
  • Use a `std::vector` is you don't know what the size will be until run time. – NathanOliver Dec 18 '18 at 21:32
  • in that case it would give me an out of range error – Sojan Janso Dec 18 '18 at 21:32
  • 2
    I'd go back and read some more about C++ and arrays. To quote Bjarne Stroustrup: "C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off." Without reading, you're going to continue to make serious errors. – rsjaffe Dec 18 '18 at 21:32
  • 1
    And if you get an out of range error, you know you have a problem! Fix the error. – rsjaffe Dec 18 '18 at 21:33
  • Voting to close as a typo. You have an off by one error. `x <= arraySize` should be `x < arraySize` as `array[size_of_array]` is not a valid index. – NathanOliver Dec 18 '18 at 21:33
  • You do have an out of range error--it's just invisible because you're using an array. – rsjaffe Dec 18 '18 at 21:35
  • Also, how is array[size_of_array] not a valid index? – Sojan Janso Dec 18 '18 at 21:35
  • 3
    @SojanJanso Arrays start at the 0 index. So if you have an array of size 10 the last valid index is 9, not 10. – NathanOliver Dec 18 '18 at 21:36
  • 3
    As for why you don't get an error, see: https://stackoverflow.com/questions/1239938/accessing-an-array-out-of-bounds-gives-no-error-why – NathanOliver Dec 18 '18 at 21:37
  • @SojanJanso I think, you should read a good C++ book. SO has a curated list of good C++ books, just search for it! – SergeyA Dec 18 '18 at 21:44
  • Sory, guys, I am aware of the array size being 0 to -1 range, I just missed it, can't believe I messed up so bad, oh well, thank you all. – Sojan Janso Dec 18 '18 at 21:53
  • Also thanks for suggesting the std::vector as a replacement, I was recoding this snippet to try to find an error in my whole algorithm which gives me an error when the range is at 10000000 or higher, I guess the array on runtime must have had to do with that. – Sojan Janso Dec 18 '18 at 22:06

1 Answers1

2

Arrays are 0 based. Meaning, in your for loop, for (int x = 0; x <= arraySize; x++), the part x <= arraySize should be x < arraySize.

An example:

If you declare your array size 10, the valid array indices are 0,1,2,3,4,5,6,7,8,9 (10 total indices).

If you were to throw that in a for loop with x <= 10, your last index would be array[10] (undefined/uninitialized).

So in short, your initialization is correct, it just that your for loop is going out of bounds.

static_cast
  • 1,174
  • 1
  • 15
  • 21
  • Ah yes, I should have caught that. I am aware of it, I just left it there some time when I ran the function and it did not throw any errors... gotta love undefined behavior... – Sojan Janso Dec 18 '18 at 21:59