Search code examples
c++if-statementwhile-loopinfinite-loopfunction-definition

My code is in an infinite loop and I need it taken out. It should output 5 rows with 3 columns with no duplicates in each row


So there is a bug in my code that puts it in an infinite loop. I need that taken out and I cannot find it for the life of me.

Here is the code:

#include <iostream>
#include <cstdlib>

const int MAX             = 6;
const int SIZE_OF_SAMPLES = 3;
const int REP             = 5;

bool inArray     (int[], int, int  );
void UniqRandInt (int,   int, int[]);

int main() {
//   std::cerr<<"in main\n";

int arr[SIZE_OF_SAMPLES];

srand(9809);  //Seed random number generator.


for (int i = 0; i < REP; i++) {
    UniqRandInt(MAX, SIZE_OF_SAMPLES, arr);
    for(int j = 0; j < SIZE_OF_SAMPLES; j++) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
}
return 0;
}
void UniqRandInt(int max, int n, int result[]) {

int cntr = 0, r;

while(cntr < n) {

    r = rand();  //Get random number
    r = r % (max + 1);
    if (inArray(result, cntr, r)) {
        result[cntr] =  r;
        cntr++;
    }
}
return;
}
bool inArray(int array[], int arrSize, int x) {
for (int i = 0; i < arrSize; ++i) {
    if (array[i] == x) {
        return true;
    }
}
return false;
}

It is outputting somthing like: 222 000 555 000 Here is what is supposed to output: something like: 254 105 035 432 523 I need 5 rows with all different numbers.


Solution

  • Within the while loop

    while(cntr < n) {
    
        r = rand();  //Get random number
        r = r % (max + 1);
        if (inArray(result, cntr, r)) {
            result[cntr] =  r;
            cntr++;
        }
    }
    

    the variable cntr is not incremented if the condition in the if statement evaluates to false. So if such a value of r that is absent in a sub-array of the array arr was generated then the while loop will be infinite.

    And any value of r for the first iteration of the loop when cntr is equal to 0 is such a value that is not present in a sub-array of the array due to this function definition

    bool inArray(int array[], int arrSize, int x) {
    for (int i = 0; i < arrSize; ++i) {
        if (array[i] == x) {
            return true;
        }
    }
    return false;
    }
    

    because within the function definition the for loop will not have an iteration and the control will be passed to the return statement

    return false;
    

    That is in this case the for loop will look like

    for (int i = 0; i < 0; ++i) {
    

    As you can see its condition will evaluate to false.

    Also if you need all different numbers then at least the condition in the if statement

    if (inArray(result, cntr, r)) {
    

    should be rewritten like

    if ( not inArray(result, cntr, r)) {
    

    It seems it is enough to change this if statement to get the expected result (privided that the code does not have any other logical or other errors :)).

    Pay attention to that you have a typo in this for loop

    for(int j = 0; j < SIZE_OF_SAMPLES; j++) {
        std::cout << arr[i] << " ";
                     ^^^^^^
    }
    

    You need to write

    for(int j = 0; j < SIZE_OF_SAMPLES; j++) {
        std::cout << arr[j] << " ";
                     ^^^^^^
    }