1
votes

I have this code. I'm generating 2 arrays with random numbers and then creating 2 strings out of those arrays using the arrayToString function, but my output is strange.

class job1Instance : public pp::Instance {
public:
        explicit job1Instance(PP_Instance instance): pp::Instance(instance) {}
        virtual ~job1Instance() {}

    virtual void HandleMessage(const pp::Var& message) {
        // declare all the zises
        int32_t minNum = 1;
        int32_t maxNum = 100;
        int32_t arrayElements = maxNum;

        // the arrays
        int32_t unsorted1[arrayElements/2];
        int32_t unsorted2[arrayElements/2];

        // fill the arrays with random numbers
        unsortedArrays(unsorted1, unsorted2, arrayElements, minNum, maxNum);
        std::string outRes1, outRes2, jsonStr;
        arrayToString(unsorted1, arrayElements/2, outRes1);
        arrayToString(unsorted2, arrayElements/2, outRes2);
        PostMessage(pp::Var(outRes2));
    }



private:
    // function to create a random number between min and max
    int32_t rangeRandomAlg (int32_t min, int32_t max) {
        int32_t num = max - min + 1;
        int32_t remainder = RAND_MAX % num;
        int32_t x;
        do {
            x = rand();
        } while (x >= RAND_MAX - remainder);
        return min + x % num;
    }

    // function to create arrays with random numbers
    void unsortedArrays (int32_t unsorted1[], int32_t unsorted2[], int32_t arrayElements, int32_t &minNum, int32_t &maxNum) {
        for(int32_t i = 0; i < arrayElements; i++) {
            if (i < arrayElements/2) {
                //unsorted1[i] = rangeRandomAlg(minNum, maxNum);
                unsorted1[i] = rand() % maxNum + minNum;
            } else {
                //unsorted2[i] = rangeRandomAlg(minNum, maxNum);
                unsorted2[i] = rand() % maxNum + minNum;
            }
        }
    }


    // convert the arrays to string
    void arrayToString (int32_t array[], int32_t arraySize, std::string& arrayString) {
        for (int i = 0; i <= arraySize; ++i){
            arrayString+= std::to_string(array[i]);
            if (i != arraySize) {
                arrayString+= ',';
            }
        }
    }

Can someone tell me why my outRes2 output has those numbers?

-18700984,-18701112,8,0,2,0,-66124,0,-66124,0,267757568,0,-65608,0,1,0,-65608,0,266960448,0,-66124,0,1,0,-18699984,0,-66124,0,-18699984,0,266959840,0,7,-66124,-18699984,0,-66124,0,-68200,0,-18699984,0,266959360,0,1,0,536870911,0,-18700016,0,91

They are clearly not between 1 and 100 as my minNum and maxNum define, I can't find the problem.

2
What did you see when you debugged and stepped through your code?Khalil Khalaf
What happens when you try to reduce it to a minimal complete example?Beta

2 Answers

2
votes

You declared two arrays, each of size arrayElements/2:

    int32_t unsorted1[arrayElements/2];
    int32_t unsorted2[arrayElements/2];

Your loop initializes them as follows:

        if (i < arrayElements/2) {
            //unsorted1[i] = rangeRandomAlg(minNum, maxNum);
            unsorted1[i] = rand() % maxNum + minNum;
        } else {
            //unsorted2[i] = rangeRandomAlg(minNum, maxNum);
            unsorted2[i] = rand() % maxNum + minNum;
        }

Therefore, when, for example, the value of i reaches arrayElements/2, the else part of the if statement will execute:

            unsorted2[arrayElements/2] = rand() % maxNum + minNum;

Since the size of unsorted2 is arrayElements/2, this array contains only values unsorted2[0] through unsorted2[arrayElements/2-1], and this assignment runs off the end of the array, resulting in undefined behavior.

-1
votes

Please don't use rand(), use the modern version... it makes everything alot easier!! Here's a simple example code:

#include <random>
#include <iostream>
#include <string>

int main(){

    std::random_device now;
    std::mt19937 engine(now());
    std::uniform_real_distribution<double> r(0, 100); //!

    std::string str = std::to_string( r(engine) );

    std::cout << str << std::endl;

    system("pause");
    return 0;
}