Thread: Arrrays, Loops, and Randomization

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    Arrrays, Loops, and Randomization

    I have an int arrary of 10 -

    int array[10]

    by using #include <time.h> to be able to srnd using time for true varience like:

    srand( (unsigned)time( NULL ) );
    j = (int) 10 * rand() / (RAND_MAX + 1.0);

    I want to be able to somehow create a loop (probably by using another array as well) that will put the numbers 1-10 in the array indices but in a way that all the numbers 1-10 are used and none are repeated.

    I don't know if I am even on the right track of how I am going about this.. but I was thinking using another array filled with the numbers 1-10 and then somehow when I get j turn j's indice into a 0 and then my check for continuing the loop would be if all the indces of array2 are used and to only use the indice of array[j] if it is not 0.

    so check for the loop would be something like...

    for (i = 10, c = 0; i < 10 ; i++)
    {
    c = c + array[i];
    }

    if (c == 0)

    /* stop loop */

    else
    /* loop continues */

    and then in a nested loop maybe? something like..

    {
    srand( (unsigned)time( NULL ) );
    j = (int) 10 * rand() / (RAND_MAX + 1.0)
    if (array2[j] == 0);

    else
    {
    array2[j] = 0;
    array1[i] = j;
    i++;
    }
    }

    Any suggestions? thoughts? comments? How do I do this?

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >Any suggestions? thoughts? comments? How do I do this?
    Not a bad problem, here's one way:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    #define LOW   1
    #define HIGH  10
    #define SIZE  10
    #define NPOS  SIZE
    #define INVAL -1
    
    static int find(int* array, int size, int val)
    {
        int i;
    
        for (i = 0; i < size; i++)
        {
            if (array[i] == val)
                return i;
        }
    
        return size;
    }
    
    int main(void)
    {
        int array[SIZE];
        int i = 0;
        int r;
    
        srand((unsigned)time(0));
    
        for (i = 0; i < SIZE; i++)
            array[i] = INVAL;
    
        while ((i = find(array, SIZE, INVAL)) != NPOS)
        {
            r = (int)((double)rand() * HIGH) / RAND_MAX + LOW;
    
            if (find(array, SIZE, r) == NPOS)
                array[i] = r;
        }
    
        for (i = 0; i < SIZE; i++)
            printf("%d, ", array[i]);
    
        return 0;
    }
    p.s. What the alphabet would look like without q and r.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    4
    Thank you for the help!!

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    195
    is the plural of index really indices i thougth it was indexes

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    see http://www.hyperdictionary.com/dictionary/indices

    Both 'indexes' and 'indices' are valid plurals of index except:

    6. (Math.) The figure or letter which shows the power or root
    of a quantity; the exponent. [In this sense the plural is
    always {indices}.]
    DavT
    -----------------------------------------------

Popular pages Recent additions subscribe to a feed