-1

I have created an array from numbers 1-5 in C for a project I have. Now I want to randomly place 15 zeroes in this array, but also I don't want them to be on the [0][0] of the array

void setMyArray(int arr[R][C]){
int i,j,y;
int x;

for(i=0; i<R; i++){
    for(j=0; j<C; j++){
        y=rand()%5;
        arr[i][j]=y+1;
    }
}   

for(i=0; i<15; i++)  
        x=rand()%1;
        arr[i][j]=x;
}

By doing that all I get is 15 zeroes at the start of my array and not shuffled.

Can anyone help my correct my code? Thank you.

CaptainBli
  • 4,121
  • 4
  • 39
  • 58
anastasis
  • 1
  • 1
  • 1

1 Answers1

0

Here's a possible way with a little example included about shuffling an array:

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

void shuffle(int *array, int n) {
    srand((unsigned)time(NULL));
    for (int i = 0; i < n - 1; i++) {
        size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
        int t = array[j];
        array[j] = array[i];
        array[i] = t;
    }
}

#define N 6

int main() {
    int positions[N] = {0, 1, 2, 3, 4, 5};

    for (int j = 0; j < 10; j++) {
        shuffle(positions, N);
        for (int x = 0; x < N; x++) printf("%d ", positions[x]);
        printf("\n");
    }

    return 0;
}

The output:

2 5 3 1 0 4 
3 4 1 5 2 0 
1 0 5 4 3 2 
5 2 4 0 1 3 
4 3 0 2 5 1 
0 1 2 3 4 5 
2 5 3 1 0 4 
3 4 1 5 2 0 
1 0 5 4 3 2 
5 2 4 0 1 3 

Now, if we focus on your particular example, where you want to shuffle an array of random values with 15 zeroes, you just need to fill the array with positive integers first, then adding 15 zeroes, for instance in the first 15 positions and then shuffling the array as many times as you like, example below:

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

#define WIDTH 15
#define HEIGHT 5

void shuffle(int *array, int n) {
    srand((unsigned)time(NULL));
    for (int i = 0; i < n - 1; i++) {
        size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
        int t = array[j];
        array[j] = array[i];
        array[i] = t;
    }
}

void fill_array(int *array, int height, int width) {
    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            int value = (rand() % 5) + 1;
            array[y * width + x] = value;
        }
    }
    for (int x = 0; x < WIDTH; x++) {
        array[x] = 0;
    }
}

void display_array(int *array, int height, int width) {
    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            printf("%d ", array[y * width + x]);
        }
        printf("\n");
    }
}

int main() {
    int array[WIDTH * HEIGHT];

    fill_array(array, HEIGHT, WIDTH);

    for (int j = 0; j < 10; j++) {
        shuffle(array, HEIGHT * WIDTH);
    }

    display_array(array, HEIGHT, WIDTH);

    return 0;
}

2 5 1 0 0 2 2 2 4 2 2 4 3 1 4 
5 3 0 2 0 3 5 1 1 4 3 5 0 2 1 
2 3 5 0 2 5 3 3 0 2 0 0 4 0 4 
1 1 2 3 4 0 0 3 3 1 0 3 5 3 3 
5 4 0 4 5 2 2 3 0 1 4 5 4 3 4 
BPL
  • 9,632
  • 9
  • 59
  • 117
  • thank you for your answer. but i can't understand where excactly you set how many 0s are going to be created – anastasis May 25 '17 at 18:48
  • I had left that as exercise for you but I felt in the mood to edit the answer again, happy coding :) – BPL May 25 '17 at 19:00