lundi 21 décembre 2015

Replacing random elements in array in c

I have an array[3][7] of random numbers and I want to replace 3 random numbers of each row with the number: -1. How to do this?

Here is the code with the array of random numbers:

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

void shuffle(int *array, size_t array_size, size_t shuff_size)
{   
    if (array_size > 1)  
    {   
        size_t i;
        time_t t;
        srand((unsigned)time(&t));
        for (i = 0; i < shuff_size - 1; i++) 
        {   
          size_t j = i + rand() / (RAND_MAX / (array_size - i) + 1); 
          int t = array[j];
          array[j] = array[i];
          array[i] = t;
        }
    }
}

int main(int argc, char * argv[]) {
    int a[50];
    int b[3][7];
    int i,j,k=0;
    for(i=0; i<50;++i)
        a[i]=i;
    shuffle(a,50,21);

    for(i=0;i<3;++i){
        for(j=0;j<7;++j) {
            b[i][j] = a[k++];
            printf("%.2d ",b[i][j]);
    }
    printf("\n");}
} 




Aucun commentaire:

Enregistrer un commentaire