Remove Specific Elements of a 2d Array

So I am currently working on a program that will remove elements from an array.
Suppose I had a 3x3 Array

1 2 3
4 5 6
7 8 9

and chose the element 5 as the one I wanted to remove.

How would I do that?

This is where I am so far. I am not looking for a solution, just a push in the right direction.

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
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <time.h>
#include <algorithm>
#include <vector>
using namespace std;

const int cols=3;
const int rows=3;

//---------------------------------------VARIABLES-------------
long int perc[rows+1][cols+1];
long int ArrayDEF;
long int elements=cols*rows;
long int n=1;
long int V=n;
long int rem=5;
//---------------------------------------BODY------------------
int main()
{
    //------------------------------------
    srand (time(0));
        
/*
     the random function needs to create the string of random numbers here, then set this value equal to rem at each instance
*/
    //------------------------------------
    if (!rem)
    {
        long int V=0;
        for(int y=1;y<rows+1;y++)             //this function defines the number of row in the array
            for(int x=1;x<cols+1;x++)         //this function defines number of columns in the array
                perc[x][y]=V++;
    }
    else {
        long int V=n;
        for(int y=1;y<rows+1;y++)             //this function defines the number of row in the array
            for(int x=1;x<cols+1;x++)         //this function defines number of columns in the array
                perc[x][y]=V++;
    }
    //------------------------------------
    cout<<"rows="<<rows<<endl;
    cout<<"columns="<<cols<<endl;       //displays rows, columns, and elements on screen
    cout<<"elements="<<elements<<endl;
    //------------------------------------
    /*for(int y=1;y<rows+1;y++)             //this function defines the number of row in the array
        for(int x=1;x<cols+1;x++)           //this function defines number of columns in the array
            perc[x][y]=V++;*/               //builds the array...IN MEMORY
    
    //------------------------------------
    for(int y=1;y<rows+1;y++)             //prints the rows
    {
        for(int x=1;x<cols+1;x++)         //prints the columns
            cout<<perc[x][y]<<"  ";     //displays the final product
        cout<<endl;                     //creates a new line for each row
    }
    
    //------------------------------------
}



Thanks
closed account (Dy7SLyTq)
first of all, loops arent functions. they are loops. second i dont think you can. look up std::vector
So I am currently working on a program that will remove elements from an array.
Suppose I had a 3x3 Array

1 2 3
4 5 6
7 8 9

and chose the element 5 as the one I wanted to remove.

How would I do that?


What should your array look like after the element is removed?
What should your array look like after the element is removed?


The array would look like:

1 2 3
4 0 6
7 8 9

Thanks!
So, basically it's replace a cell value with a zero. Althought it's a 2d array, you can think of the memory it occupies like this:

[1][2][3][4][5][6][7][8][9]

To get a pointer to the first element of your grid you would need to get the address thus:

long* p = &perc[0][0];

Then to change the cell currently occupied by the value '5' you would just increment your pointer by 4 (remember we are dealing with a zero based index) and replace the value '5' with '0':

*(p+=4) = 0;

The

 
p+=4


merely increments our pointer from the address for cell[0][0] to the address occupied by cell[1][1]

The preceding asterrisk is the de-reference operator so that we can update the address value, which is the same as:

1
2
p+=4;
*p = 0;


The complete code is:

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 <cmath>
#include <time.h>
#include <algorithm>
#include <vector>
using namespace std;

const int cols=3;
const int rows=3;

//---------------------------------------VARIABLES-------------
long int perc[rows][cols];
long int ArrayDEF;
long int elements=cols*rows;
long int n=1;
long int V=n;
long int rem=5;
//---------------------------------------BODY------------------
int main()
{
    //------------------------------------
    srand (time(0));
        
/*
     the random function needs to create the string of random numbers here, then set this value equal to rem at each instance
*/
    //------------------------------------
    if (!rem)
    {
        long int V=0;
        for(int y=0;y<rows;y++)             //this function defines the number of row in the array
            for(int x=0;x<cols;x++)         //this function defines number of columns in the array
                perc[x][y]=V++;
    }
    else {
        long int V=n;
        for(int y=0;y<rows;y++)             //this function defines the number of row in the array
            for(int x=0;x<cols;x++)         //this function defines number of columns in the array
                perc[x][y]=V++;
    }
    //------------------------------------
    cout<<"rows="<<rows<<endl;
    cout<<"columns="<<cols<<endl;       //displays rows, columns, and elements on screen
    cout<<"elements="<<elements<<endl;
    //------------------------------------
    /*for(int y=1;y<rows+1;y++)             //this function defines the number of row in the array
        for(int x=1;x<cols+1;x++)           //this function defines number of columns in the array
            perc[x][y]=V++;*/               //builds the array...IN MEMORY
    
    //------------------------------------
    for(int y=0;y<rows;y++)             //prints the rows
    {
        for(int x=0;x<cols;x++)         //prints the columns
            cout<<perc[x][y]<<"  ";     //displays the final product
        cout<<endl;                     //creates a new line for each row
    }
    
    //------------------------------------

	 // remove elemet at index 5, i.e. replace value with '0':
	long* p = &perc[0][0];
	*(p+=4) = 0;
}
0 1 2
3 4 5
6 7 8

Position 4 on this 2D matrix is perc[1][1].

So you just do perc[1][1] = 0;
Last edited on
So, basically it's replace a cell value with a zero. Althought it's a 2d array, you can think of the memory it occupies like this:

[1][2][3][4][5][6][7][8][9]

To get a pointer to the first element of your grid you would need to get the address thus:

long* p = &perc[0][0];

Then to change the cell currently occupied by the value '5' you would just increment your pointer by 4 (remember we are dealing with a zero based index) and replace the value '5' with '0':

*(p+=4) = 0;

The


p+=4


merely increments our pointer from the address for cell[0][0] to the address occupied by cell[1][1]

The preceding asterrisk is the de-reference operator so that we can update the address value, which is the same as:

1
2
p+=4;
*p = 0;


Okay! I hadn't thought of using pointers! That solved my problem. Thanks everybody!
Topic archived. No new replies allowed.