Randomly Remove Points From Array

Hey,

So I am fairly new to programming with c++ and I was wondering if there was a way to set up a code with an array of variable dimensions that removed random points on that array i.e.

1111
1111
1111

then randomly remove a point:

1101
1111
1111

and then be able to repeat this process until all points are removed.
Any ideas?
My strategy is first count the remaining elements in the array, if there are/is remaining element(s), then generate a random index of the remainders and remove it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

const int ROWS = 3;
const int COLS = 4;

bool removePoint(int arr[ROWS][COLS]);
void print2dArray(int arr[ROWS][COLS]);

int main()
{
    srand(time(0));
    int arr[ROWS][COLS];
    for (int i=0; i<ROWS; ++i)
        for (int j=0; j<COLS; ++j)
            arr[i][j] = 1;

    while (removePoint(arr))
    {
        print2dArray(arr);
        cout << endl;
    }
}

//----------------------------------------------
bool removePoint(int arr[ROWS][COLS])
{
    // Count remaining elements
    int rem = 0;
    for (int i=0; i<ROWS; ++i)
        for (int j=0; j<COLS; ++j)
            if (arr[i][j]) ++rem;

    // If there is no remaining, return false, means
    //the function does nothing to the array
    if (!rem) return false;

    // Generate a random remove index, range [1,rem] inclusive
    //This is not row or col index. If we have 7 remaining elements,
    //and removeIndex is 4, then the 4th element in the 7 remaining
    //elements will be removed.
    int rmi = rand() % rem + 1;

    // Remove it
    for (int i=0; i<ROWS && rmi; ++i)
        for (int j=0; j<COLS && rmi; ++j)
            if (arr[i][j] && !--rmi) arr[i][j] = 0; //if arr[i][j] is not empty and (rmi after decremented) is 0,
                                                  //we found the element to remove

    // Return true means the function successfully removes a point
    return true;
}
//----------------------------------------------
void print2dArray(int arr[ROWS][COLS])
{
    for (int i=0; i<ROWS; ++i)
    {
        for (int j=0; j<COLS; ++j)
            cout << arr[i][j] << " ";
        cout << endl;
    }
}
Okay! That makes sense, could you explain why you used void there? I am pretty sure i understand everything else. Thank you so much for your help.
Topic archived. No new replies allowed.