2d array how to move all ements

how do i move all elements to the left if try to delete a specific element in 2d array? im using static array and not allowed to use vectors and array

for example:

1
2
3
   0  1  2  3  4  5
0| a  b  c  d  e  f
1| g  h  i  j  k  l


enter position x: 0 enter position y: 1

desired output:
1
2
3
   0  1  2  3  4  5
0| a  c  d  e  f  g
1| h  i  j  k  l  


this is my attempted code:

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
  #include<iostream>
#include<string>

std::string array[3][6] = {{"a","b","c","d","e","f",},
                            {"g","h","i","j","k","l",},
                            {" "," "," "," "," "," ",}}; //i made some allowance so that i can insert/delete

void display();

using namespace std;

int main(){

    char xdelete;
    char ydelete;

    display();

    cout<<"\nDelete character" <<endl;
    cout<<"enter position x: "; 
    cin>>xdelete; 
    cout<<"enter position y: ";
    cin>>ydelete;

    for(int x = xdelete; x<2; x++){
        for(int y = ydelete ;y<5;y++){
            array[x][y] = array[x][y+1]; 
        }
    }

    display();

}

//display
void display(){

    cout<<endl;
    for(int z = 0; z<6; z++){
        cout<<"  "<<z;
    }
    cout<<endl;

    for(int x = 0; x<2; x++){
    cout<<x <<"|";
        for(int y = 0; y<6; y++){
            cout<<" " <<array[x][y] <<" "; 
        }
    cout<<endl;
    }
}
move the data, not the strings.

roughly:

for(all the rows starting at deleted)
for (all the cols starting at deleted)
{
array[r][c] = next string. next string is either r[c+1] or r+1[0] when you cross over to the next row.
}
at the end fill in the last element with your 'empty' marker.
Last edited on
There is a trick. The "static array" is a continuous block of memory. array[1][0] is right after array[0][5].

We can use that. We can handle the access the elements like they were in 1D array. We have to compute 1D index from 2D index.
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
#include<iostream>
#include<string>

std::string array[3][6] = {{"a","b","c","d","e","f",},
                            {"g","h","i","j","k","l",},
                            {" "," "," "," "," "," ",}};

void display();

using namespace std;

int main(){

    size_t xdelete = 0;
    size_t ydelete = 2;

    display();

    cout<<"\nDelete character" <<endl;
    cout<<"enter position row: "; 
    //cin>>xdelete; 
    cout<<"enter position col: ";
    //cin>>ydelete;

    std::string * foo = array[0];
    size_t first = 6 * xdelete + ydelete;
    for ( size_t x = first; x < 6*3 - 1; ++x ) {
        foo[x] = foo[x+1]; 
    }
    foo[6*3-1] = " ";

    display();

}

//display
void display(){

    cout << "\n ";
    for(int z = 0; z<6; z++){
        cout<<"  "<<z;
    }
    cout<<endl;

    for(int x = 0; x<2; x++){
    cout<<x <<"|";
        for(int y = 0; y<6; y++){
            cout<<" " <<array[x][y] <<" "; 
        }
    cout<<endl;
    }
}
@thanks jonin
@keskivero i havent learned pointers yet and it seems your trick is kind of advance for me but i still keep this in my mind.

i also like to point out my mistake about
1
2
char xdelete;
          char ydelete; 


it should be integer since im dealing with position, sorry about that. no wonder the elements are not shifting.

fixed code:

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
#include<iostream>
#include<string>


std::string myarray[3][6] = {{"a","b","c","d","e","f",},
							{"g","h","i","j","k","l",},
							{" "," "," "," "," "," ",}}; //i made some allowance so that i can insert/delete

std::string myarray2[3][6];
void display();

using namespace std;

int main(){

	int xdelete;
	int ydelete;

	
	display();
	
	cout<<"\nDelete character" <<endl;
	cout<<"enter position x: "; 
	cin>>xdelete; 
	cout<<"enter position y: ";
	cin>>ydelete;
	

	for(int x = xdelete; x<2; x++){
        for(int y = ydelete ;y<6;y++){
            myarray[x][y] = myarray[x][y+1];
        }
    }
	
	
	
	
	display();

}

//display
void display(){
	
	cout<<endl;
	for(int z = 0; z<6; z++){
		cout<<"  "<<z;
	}
	cout<<endl;
	
	for(int x = 0; x<2; x++){
	cout<<x <<"|";
		for(int y = 0; y<6; y++){
			cout<<" " <<myarray[x][y] <<" "; 
		}
	cout<<endl;
	}
}


now the problem is that the element "h" is missing and was replaced by g. can you give me simple fix for a beginner like me. sorry for bothering you guys and thanks again for the replies
h is missing because of your loop logic, which is probably my fault ... I didnt clearly state this:

if you have

1234
5678
9abc

and you delete the 3, then you are deleting row = 0, col = 2.
then your inner loop executes every time for...
col = 2 to 3.
this is not correct, you need to process all the columns after the first row, but not the first row, see?

what you need is
for(delete row, all the rest of the rows) // this is fine
if row == delete row, startcol = delete col
else startcol = 0;
for(starcol, all the cols, ...)

right?






Last edited on
Apart from starting each processed row from column ydelete there is also an issue with the last column:
1
2
3
4
5
for ( int x = xdelete; x<2; x++) {
  for ( int y = ydelete ;y<6;y++ ) {
    myarray[x][y] = myarray[x][y+1];
  }
}

On each processed row the last column (y) is 5.
That leads to these statements:
myarray[x][5] = myarray[x][6];
You are reading "one past the end of row".

It does seem to work for you due to the "trick" that I did mention. While myarray[0][6] is not on row myarray[0], it is in reality the element myarray[1][0].

Inside the table that is "nice", but if you do process the last row to the end, you will refer to "one past the entire table".


Being vary of pointers is good, so let me rephrase the loop in my version:
1
2
3
4
5
6
7
const int Rows = 3;
const int Cols = 6;
int first = Cols * xdelete + ydelete;
for ( int x = first; x < Cols*Rows - 1; ++x ) {
  myarray[0][x] = myarray[0][x+1]; 
}
myarray[0][Cols*Rows-1] = " ";


Now you can compare "nested loops with many special cases that can go easily wrong" and "advanced" single loop.
1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include<string>


int main() {
        std::string array[3][6] = {{"a","b","c","d","e","f",},
                                   {"g","h","i","j","k","l",},
                                   {" "," "," "," "," "," ",}};

        std::cout << array[0][10]; //there it is
        }


You don't get the segmentation fault unless you go far enough outside the array to go entirely outside the memory the program is allowed to access.
Element array[0][10] is not "outside" of the array[3][6]. It is element array[1][4].
1*6+4==0*6+10
You are correct in all cases where the language is C. :-)
@keskiverto thank you very much!
Topic archived. No new replies allowed.