c++ Trying to delete last row and column of a 2d array
up vote
0
down vote
favorite
I am trying to delete last row and the last column of a 2d array. I created this method for this purpose.
void deleteLastRowColumn(int **A, int row, int column)
{
int **temp = A;
A = new int *[row - 1];
for (int i = 0; i < row - 1; i++)
{
A[i] = new int[column - 1];
for (int j = 0; j < column - 1; j++)
{
A[i][j] = temp[i][j];
}
delete temp[i];
}
}
and printing the result with
void printArray(int **a, int row, int column)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
And my main method creates a 5x5 array
int main()
{
int **a;
a = new int *[5];
for (int i = 0; i < 5; i++)
a[i] = new int[5];
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
a[i][j] = (i*5) + j + 1;
}
}
printArray(a, 5, 5);
cout << "nnnn";
deleteLastRowColumn(a,5,5);
printArray(a,4,4);
}
The output didn't make sense to me:
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
6 7 8 9
11 12 13 14
16 17 18 19
0 0 18 19
I expected it to remove last column and last row but something different happened. If you can explain me what's wrong here that would be great!
Thank you.
c++ arrays dynamic-arrays
add a comment |
up vote
0
down vote
favorite
I am trying to delete last row and the last column of a 2d array. I created this method for this purpose.
void deleteLastRowColumn(int **A, int row, int column)
{
int **temp = A;
A = new int *[row - 1];
for (int i = 0; i < row - 1; i++)
{
A[i] = new int[column - 1];
for (int j = 0; j < column - 1; j++)
{
A[i][j] = temp[i][j];
}
delete temp[i];
}
}
and printing the result with
void printArray(int **a, int row, int column)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
And my main method creates a 5x5 array
int main()
{
int **a;
a = new int *[5];
for (int i = 0; i < 5; i++)
a[i] = new int[5];
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
a[i][j] = (i*5) + j + 1;
}
}
printArray(a, 5, 5);
cout << "nnnn";
deleteLastRowColumn(a,5,5);
printArray(a,4,4);
}
The output didn't make sense to me:
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
6 7 8 9
11 12 13 14
16 17 18 19
0 0 18 19
I expected it to remove last column and last row but something different happened. If you can explain me what's wrong here that would be great!
Thank you.
c++ arrays dynamic-arrays
1
Are you willing to switch from pointers tostd::vector
?
– Micha Wiedenmann
Nov 22 at 13:56
4
If you want to changeA
indeleteLastRowColumn
- pass it by reference.
– Algirdas Preidžius
Nov 22 at 13:56
@MichaWiedenmann I wish I could. The course I'm taking doesn't let me...
– burakokumus
Nov 22 at 14:03
@burakokumus: That is a problem in your course. You can only change*A
, notA
, so you cannot resize both dimensions. Therefore,A
remains a(int*)[5]
, The best you can do is to setA[0]
toA[3]
toint[4]
andA[4]
tonullptr
.
– MSalters
Nov 22 at 14:08
I made the change proposed by Algirdas Preidzius and test it, it works. Just add adelete temp
at the end of the function, but it was not the cause of the problem
– Damien
Nov 22 at 15:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to delete last row and the last column of a 2d array. I created this method for this purpose.
void deleteLastRowColumn(int **A, int row, int column)
{
int **temp = A;
A = new int *[row - 1];
for (int i = 0; i < row - 1; i++)
{
A[i] = new int[column - 1];
for (int j = 0; j < column - 1; j++)
{
A[i][j] = temp[i][j];
}
delete temp[i];
}
}
and printing the result with
void printArray(int **a, int row, int column)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
And my main method creates a 5x5 array
int main()
{
int **a;
a = new int *[5];
for (int i = 0; i < 5; i++)
a[i] = new int[5];
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
a[i][j] = (i*5) + j + 1;
}
}
printArray(a, 5, 5);
cout << "nnnn";
deleteLastRowColumn(a,5,5);
printArray(a,4,4);
}
The output didn't make sense to me:
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
6 7 8 9
11 12 13 14
16 17 18 19
0 0 18 19
I expected it to remove last column and last row but something different happened. If you can explain me what's wrong here that would be great!
Thank you.
c++ arrays dynamic-arrays
I am trying to delete last row and the last column of a 2d array. I created this method for this purpose.
void deleteLastRowColumn(int **A, int row, int column)
{
int **temp = A;
A = new int *[row - 1];
for (int i = 0; i < row - 1; i++)
{
A[i] = new int[column - 1];
for (int j = 0; j < column - 1; j++)
{
A[i][j] = temp[i][j];
}
delete temp[i];
}
}
and printing the result with
void printArray(int **a, int row, int column)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < column; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
And my main method creates a 5x5 array
int main()
{
int **a;
a = new int *[5];
for (int i = 0; i < 5; i++)
a[i] = new int[5];
for(int i = 0; i < 5; i++)
{
for(int j = 0; j < 5; j++)
{
a[i][j] = (i*5) + j + 1;
}
}
printArray(a, 5, 5);
cout << "nnnn";
deleteLastRowColumn(a,5,5);
printArray(a,4,4);
}
The output didn't make sense to me:
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
6 7 8 9
11 12 13 14
16 17 18 19
0 0 18 19
I expected it to remove last column and last row but something different happened. If you can explain me what's wrong here that would be great!
Thank you.
c++ arrays dynamic-arrays
c++ arrays dynamic-arrays
asked Nov 22 at 13:53
burakokumus
41
41
1
Are you willing to switch from pointers tostd::vector
?
– Micha Wiedenmann
Nov 22 at 13:56
4
If you want to changeA
indeleteLastRowColumn
- pass it by reference.
– Algirdas Preidžius
Nov 22 at 13:56
@MichaWiedenmann I wish I could. The course I'm taking doesn't let me...
– burakokumus
Nov 22 at 14:03
@burakokumus: That is a problem in your course. You can only change*A
, notA
, so you cannot resize both dimensions. Therefore,A
remains a(int*)[5]
, The best you can do is to setA[0]
toA[3]
toint[4]
andA[4]
tonullptr
.
– MSalters
Nov 22 at 14:08
I made the change proposed by Algirdas Preidzius and test it, it works. Just add adelete temp
at the end of the function, but it was not the cause of the problem
– Damien
Nov 22 at 15:13
add a comment |
1
Are you willing to switch from pointers tostd::vector
?
– Micha Wiedenmann
Nov 22 at 13:56
4
If you want to changeA
indeleteLastRowColumn
- pass it by reference.
– Algirdas Preidžius
Nov 22 at 13:56
@MichaWiedenmann I wish I could. The course I'm taking doesn't let me...
– burakokumus
Nov 22 at 14:03
@burakokumus: That is a problem in your course. You can only change*A
, notA
, so you cannot resize both dimensions. Therefore,A
remains a(int*)[5]
, The best you can do is to setA[0]
toA[3]
toint[4]
andA[4]
tonullptr
.
– MSalters
Nov 22 at 14:08
I made the change proposed by Algirdas Preidzius and test it, it works. Just add adelete temp
at the end of the function, but it was not the cause of the problem
– Damien
Nov 22 at 15:13
1
1
Are you willing to switch from pointers to
std::vector
?– Micha Wiedenmann
Nov 22 at 13:56
Are you willing to switch from pointers to
std::vector
?– Micha Wiedenmann
Nov 22 at 13:56
4
4
If you want to change
A
in deleteLastRowColumn
- pass it by reference.– Algirdas Preidžius
Nov 22 at 13:56
If you want to change
A
in deleteLastRowColumn
- pass it by reference.– Algirdas Preidžius
Nov 22 at 13:56
@MichaWiedenmann I wish I could. The course I'm taking doesn't let me...
– burakokumus
Nov 22 at 14:03
@MichaWiedenmann I wish I could. The course I'm taking doesn't let me...
– burakokumus
Nov 22 at 14:03
@burakokumus: That is a problem in your course. You can only change
*A
, not A
, so you cannot resize both dimensions. Therefore, A
remains a (int*)[5]
, The best you can do is to set A[0]
to A[3]
to int[4]
and A[4]
to nullptr
.– MSalters
Nov 22 at 14:08
@burakokumus: That is a problem in your course. You can only change
*A
, not A
, so you cannot resize both dimensions. Therefore, A
remains a (int*)[5]
, The best you can do is to set A[0]
to A[3]
to int[4]
and A[4]
to nullptr
.– MSalters
Nov 22 at 14:08
I made the change proposed by Algirdas Preidzius and test it, it works. Just add a
delete temp
at the end of the function, but it was not the cause of the problem– Damien
Nov 22 at 15:13
I made the change proposed by Algirdas Preidzius and test it, it works. Just add a
delete temp
at the end of the function, but it was not the cause of the problem– Damien
Nov 22 at 15:13
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53432509%2fc-trying-to-delete-last-row-and-column-of-a-2d-array%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Are you willing to switch from pointers to
std::vector
?– Micha Wiedenmann
Nov 22 at 13:56
4
If you want to change
A
indeleteLastRowColumn
- pass it by reference.– Algirdas Preidžius
Nov 22 at 13:56
@MichaWiedenmann I wish I could. The course I'm taking doesn't let me...
– burakokumus
Nov 22 at 14:03
@burakokumus: That is a problem in your course. You can only change
*A
, notA
, so you cannot resize both dimensions. Therefore,A
remains a(int*)[5]
, The best you can do is to setA[0]
toA[3]
toint[4]
andA[4]
tonullptr
.– MSalters
Nov 22 at 14:08
I made the change proposed by Algirdas Preidzius and test it, it works. Just add a
delete temp
at the end of the function, but it was not the cause of the problem– Damien
Nov 22 at 15:13