mardi 18 décembre 2018

C++ initialized array last value is random

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
    }
}




Aucun commentaire:

Enregistrer un commentaire