c++ Trying to delete last row and column of a 2d array












0















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.










share|improve this question


















  • 1





    Are you willing to switch from pointers to std::vector?

    – Micha Wiedenmann
    Nov 22 '18 at 13:56






  • 4





    If you want to change A in deleteLastRowColumn - pass it by reference.

    – Algirdas Preidžius
    Nov 22 '18 at 13:56











  • @MichaWiedenmann I wish I could. The course I'm taking doesn't let me...

    – burakokumus
    Nov 22 '18 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 '18 at 14:08











  • I made the change proposed by Algirdas Preidzius and test it, it works. Just add a delete tempat the end of the function, but it was not the cause of the problem

    – Damien
    Nov 22 '18 at 15:13
















0















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.










share|improve this question


















  • 1





    Are you willing to switch from pointers to std::vector?

    – Micha Wiedenmann
    Nov 22 '18 at 13:56






  • 4





    If you want to change A in deleteLastRowColumn - pass it by reference.

    – Algirdas Preidžius
    Nov 22 '18 at 13:56











  • @MichaWiedenmann I wish I could. The course I'm taking doesn't let me...

    – burakokumus
    Nov 22 '18 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 '18 at 14:08











  • I made the change proposed by Algirdas Preidzius and test it, it works. Just add a delete tempat the end of the function, but it was not the cause of the problem

    – Damien
    Nov 22 '18 at 15:13














0












0








0








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.










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 13:53









burakokumusburakokumus

41




41








  • 1





    Are you willing to switch from pointers to std::vector?

    – Micha Wiedenmann
    Nov 22 '18 at 13:56






  • 4





    If you want to change A in deleteLastRowColumn - pass it by reference.

    – Algirdas Preidžius
    Nov 22 '18 at 13:56











  • @MichaWiedenmann I wish I could. The course I'm taking doesn't let me...

    – burakokumus
    Nov 22 '18 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 '18 at 14:08











  • I made the change proposed by Algirdas Preidzius and test it, it works. Just add a delete tempat the end of the function, but it was not the cause of the problem

    – Damien
    Nov 22 '18 at 15:13














  • 1





    Are you willing to switch from pointers to std::vector?

    – Micha Wiedenmann
    Nov 22 '18 at 13:56






  • 4





    If you want to change A in deleteLastRowColumn - pass it by reference.

    – Algirdas Preidžius
    Nov 22 '18 at 13:56











  • @MichaWiedenmann I wish I could. The course I'm taking doesn't let me...

    – burakokumus
    Nov 22 '18 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 '18 at 14:08











  • I made the change proposed by Algirdas Preidzius and test it, it works. Just add a delete tempat the end of the function, but it was not the cause of the problem

    – Damien
    Nov 22 '18 at 15:13








1




1





Are you willing to switch from pointers to std::vector?

– Micha Wiedenmann
Nov 22 '18 at 13:56





Are you willing to switch from pointers to std::vector?

– Micha Wiedenmann
Nov 22 '18 at 13:56




4




4





If you want to change A in deleteLastRowColumn - pass it by reference.

– Algirdas Preidžius
Nov 22 '18 at 13:56





If you want to change A in deleteLastRowColumn - pass it by reference.

– Algirdas Preidžius
Nov 22 '18 at 13:56













@MichaWiedenmann I wish I could. The course I'm taking doesn't let me...

– burakokumus
Nov 22 '18 at 14:03





@MichaWiedenmann I wish I could. The course I'm taking doesn't let me...

– burakokumus
Nov 22 '18 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 '18 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 '18 at 14:08













I made the change proposed by Algirdas Preidzius and test it, it works. Just add a delete tempat the end of the function, but it was not the cause of the problem

– Damien
Nov 22 '18 at 15:13





I made the change proposed by Algirdas Preidzius and test it, it works. Just add a delete tempat the end of the function, but it was not the cause of the problem

– Damien
Nov 22 '18 at 15:13












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'