user2946940
user2946940

Reputation: 13

Trouble with my dynamic arrays

{
    // FIRST ARRAY
    int NumberOfXValues;
    double* FirstArray;
    NumberOfXValues = 0;
    FirstArray = new double[NumberOfXValues];
    // END FIRST ARRAY

    // SECOND ARRAY
    int NumberOfProbabilities;
    double* SecondArray;
    NumberOfProbabilities = 0;
    SecondArray = new double[NumberOfProbabilities];
    // END SECOND ARRAY

    // THIRD ARRAY
    int Product;
    double* ThirdArray;
    Product = 0;
    ThirdArray = new double[Product];
    // END THIRD ARRAY

    // OBTAINING THE NUMBER OF X VALUES
    cout << "Please input the number of \"x\" values we will be using: ";
    cin >> NumberOfXValues;
    // END OF OBTAINING THE NUMBER OF X VALUES

    // OBTAINING THE VALUES IN THE FIRST ARRAY
    int x;
    cout << "Please input the \"x\" values in order from least to greatest: " << endl;
    cout << endl;
    for (int x = 0; x < NumberOfXValues; x++)
    {
        cout << "Enter number: ";
        cin >> FirstArray[x]; 
    }
    cout << endl;
    // END OF OBTAINING THE VALUES IN THE FIRST ARRAY

    // OBTAINING THE VALUES IN THE SECOND ARRAY
    cout << "Please input the probabilities corresponding to their \"x\" values: " << endl;
    cout << endl;
    for (int x = 0; x < NumberOfXValues; x++)
    {
        cout << "Enter probability in decimal form: ";
        cin >> SecondArray[x];
    }
    cout << endl;
    // END OF OBTAINING THE VALUES IN THE SECOND ARRAY

    // MULTIPLYING THE FIRST TWO ARRAYS TO OBTAIN THE THIRD
    for (int x = 0; x < NumberOfXValues; x++)
    {
        ThirdArray[x] = FirstArray[x] * SecondArray[x];
    }
    // END OF MULTIPLYING THE FIRST TWO ARRAYS TO OBTAIN THE THIRD

    //TEST
    for (int x = 0; x < NumberOfXValues; x++)
    {
        cout << ThirdArray[x];
        cout << endl;
    }
    //TEST
}

This is the first time I have ever resorted to one of these websites, but I have worked for close to over an hour to fix this. It runs completely smooth until it displays the 3rd value in the 3rd array. It always gives me a weird number.

Upvotes: 0

Views: 68

Answers (2)

Preet Kukreti
Preet Kukreti

Reputation: 8607

When you say new Foo[NumItems], that array will only ever have NumItems items, it wont grow by itself. If you want to resize it later on you need to allocate a new array of the right size and copy the members across. Sometimes you can get away with extending your current allocation with realloc.

Your issue is two fold:

  • You are initialising the array with size 0, which is meaningless. Once you know how many items you need, allocate the array with that number.
  • You are assuming the array will grow by itself as you add items.

For the second case, you should really just use an std::vector which will grow automatically and save you the trouble.

std::vector<double> firstArray(initialSize);
firstArray.push_back(firstItem);
firstArray.push_back(secondItem);
//...

In your current case, you can just defer the creation of the array until you know the total number of items you need to store in it.

Upvotes: 1

xorguy
xorguy

Reputation: 2744

The problem is that the variables that you use for size are set to 0 when you create the array:

int NumberOfXValues;
double* FirstArray;
NumberOfXValues = 0;
FirstArray = new double[NumberOfXValues];

Here FirstArray is 0 so it will create an array of 0 elements.

You have to create the array after getting the quantity of elements.

cout << "Please input the number of \"x\" values we will be using: ";
cin >> NumberOfXValues;
FirstArray = new double[NumberOfXValues];

Upvotes: 4

Related Questions