1

I want to create an array which has 20 elements and each element holds different integer numbers from closed interval [0-19].


Here is the codeblock:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 20

int main() 
{
    srand(time(NULL));

    int array[SIZE]={0}, arrayhold[SIZE]={0}, hold, i;

    for(i=0;i<SIZE;i++) 
    {
        hold = rand()%20+1;

        if(arrayhold[hold]==0)
        {
            array[i]=hold;
            arrayhold[hold]++;
        }
        else 
        {
            while(arrayhold[hold]!=0)
                hold = rand()%20+1;

            array[i]=hold;
        }
    }

    for(i=0;i<SIZE;i++) 
    {
        printf("array[%d]=%d\n",i,array[i]);
    }
}

When I run the program, the output gives array[random]=1 for many times, but it never gives array[random]=3 (just for example).

Example output

John Odom
  • 1,189
  • 2
  • 20
  • 35

3 Answers3

0

From an algorithmic point of view, something like Fisher–Yates shuffle would be perhaps better.


Anyway, in the given code

arrayhold[hold]++;

is missing in else. Try

else {
    while (arrayhold[hold] != 0)
        hold = rand() % 20 + 1;
    array[i] = hold;
    arrayhold[hold]++; // <--
}

Also, either avoid + 1 in

rand() % 20 + 1

to get values from [0, SIZE-1] or use arrayhold[SIZE + 1], since currently arrayhold is indexed from 1.


In fact, the code can be simplified, if is redundant, it would be enough to have just while within for. (Also, IMO, it would be better to use an assignment instead of ++.) Consider

for (i = 0; i<SIZE; i++) {
    hold = rand() % SIZE + 1;
    while (arrayhold[hold] != 0)
        hold = rand() % 20 + 1;
    array[i] = hold;
    arrayhold[hold] = 1;
}
AlexD
  • 32,156
  • 3
  • 71
  • 65
0
while(arrayhold[hold]!=0)
   hold = rand()%20+1;
array[i]=hold;

Here you are iterating until arrayhold[hold] is 0. So you are basically just trying to guess, when hold will be a number, on position of which in arrayhold is 0. Apperantly it's 1.

Possible solution is the following:

for (size_t i = 0; i < SIZE; ++i) {
    array[i] = rand() % 20 + 1;
    int found = 0;
    for (size_t j = 0; j < i; ++j) {
        if (array[j] == array[i]) {
            found = 1;
            break;
        }
    }
    if (!found)
        ++i;
}

Solution in c++ is using std::random_shuffle. You may google for it's implementation and try to translate it into C.

V. Kravchenko
  • 1,859
  • 10
  • 12
0

I was able to simplify your logic a bit [Please pardon the gratuitous style cleanup]. This works:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 20

int
main()
{
    int array[SIZE] = { 0 };
    int arrayhold[SIZE] = { 0 };
    int j;
    int i;

    srand(time(NULL));

    for (i = 0; i < SIZE; i++) {
        while (1) {
            j = rand() % SIZE;
            if (arrayhold[j] == 0) {
                arrayhold[j] = 1;
                array[i] = j + 1;
                break;
            }
        }
    }

    for (i = 0; i < SIZE; i++) {
        printf("array[%d]=%d\n", i, array[i]);
    }
}
Craig Estey
  • 30,627
  • 4
  • 24
  • 48