### 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` ``````#include #include #include #include #include #include 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

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:

 ``12`` ``````p+=4; *p = 0;``````

The complete code is:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````#include #include #include #include #include #include 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
 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.