简体   繁体   中英

How to delete column in 2d array c++ with dynamic array?

I want to delete column with max integer in 2d array, I do it in this way, but why is deleting the column and also row? Can I fix that and delete only column? The task was do it with delete command, but now I think it's impossible

#include <iostream>

using namespace std;

int main()
{

int row = 3, col = 3;
int** arr = new int* [row];
for(int i = 0; i < row; i++){
    arr[i] = new int[col];
}
for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++) {
        cin >> arr[i][j];
    }
}
for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++) {
        cout << arr[i][j] << " ";
    }
    cout << endl;
}
    cout << " ------------- " << endl;

int max = 0, index = 0;
for(int i =0; i < row; i++){
    for(int j = 0; j < col; j++){
        if(arr[i][j] > max){
            max = arr[i][j];
            index = i;
        }
    }
}
delete [] arr[index];
int** tmp = new int*[index - 1];
int tmpI = 0;
for(int i = 0; i < col; i++){
    if(i != index){
        tmp[tmpI++] = arr[i];
    }
}
delete [] arr;
arr = tmp;
col = col - 1;

for(int i = 0; i < row; i++){
    for(int j = 0; j < col; j++) {
        cout << arr[i][j] << " ";
    }
    cout << endl;
}

}

For starters the variable index is set to a row number

index = i;

Then this row is deleted

delete [] arr[index];

But you are going to remove a column instead of a row. So this code does not make a sense.

Also you are incorrectly searching the maximum element. If the user will enter all negative values then the maximum value will be equal to 0 though an element with such value is not present in the array.

In this declaration

int** tmp = new int*[index - 1];

you allocated an array with rows that one less than the number of rows in the original array. Moreover if index is equal to 0 then there is allocated a very large extent of memory.

This statement

delete [] arr;

produces a memory leak.

It seems what you need is something like the following

#include <iostream>

int main() 
{
    size_t row = 3, col = 3;
    
    int **arr = new int * [row];

    for ( size_t i = 0; i < row; i++ )
    {
        arr[i] = new int[col];
    }
    
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ ) 
        {
            std::cin >> arr[i][j];
        }
    }
    
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ ) 
        {
            std::cout << arr[i][j] << " ";
        }
        std::cout << std::endl;
    }
    
    std::cout << "------------- " << std::endl;
    
    size_t max_i = 0, max_j = 0;
    
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ )
        {
            if ( arr[max_i][max_j] < arr[i][j] )
            {
                max_i = i; max_j = j;
            }
        }
    }
    
    int **tmp = new int*[row];
    for ( size_t i = 0; i < row; i++ )
    {
        tmp[i] = new int[col-1];
    }
    
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0, k = 0; j < col; j++ )
        {
            if ( j != max_j ) tmp[i][k++] = arr[i][j];
        }
    }
    
    for ( size_t i = 0; i < row; i++ )
    {
        delete [] arr[i];
    }
    
    delete [] arr;
    
    arr = tmp;
    
    --col;
    
    for ( size_t i = 0; i < row; i++ )
    {
        for ( size_t j = 0; j < col; j++ ) 
        {
            std::cout << arr[i][j] << " ";
        }
        std::cout << std::endl;
    }
    
    for ( size_t i = 0; i < row; i++ )
    {
        delete [] arr[i];
    }
    
    delete [] arr;
    
    return 0;
}

The program output might look like

1 2 3 
6 5 4 
7 9 8 
 ------------- 
1 3 
6 4 
7 8 

Here's an alternate suggestion: You are storing the data in row x column format. If you change to column x row format, it becomes easier to delete a column. I also made a helper function to print the matrix and I combined the input loop and the loop that finds the max.

#include <iostream>
#include <iomanip>
#include <cstdlib>  // for rand
#include <climits>

using namespace std;

void print_matrix(int** arr, int rows, int columns)
{
    for(int row = 0; row < rows; row++){
        for(int col = 0; col < columns; col++) {
            cout << setw(2) << arr[col][row] << " ";
        }
        cout << "\n";
    }
}

int main()
{
    srand(time(nullptr));  // For testing
    
    int rows = 3, columns = 3;
    
    // Create the matrix
    int** arr = new int *[columns];
    for (int col = 0; col < columns; col++) {
        arr[col] = new int[rows];
    }
    
    // Input values - finding max, too
    int max_value = INT_MIN, max_value_column = -1;
    for (int row = 0; row < rows; row++) {
        for (int col = 0; col < columns; col++) {
            //cin >> arr[col][row];
            arr[col][row] = rand() % 50;    // Using rand for testing.
            if (arr[col][row] > max_value) {
                max_value = arr[col][row];
                max_value_column = col;
            }
        }
    }   
    print_matrix(arr, rows, columns);
    cout << " ------------- " << endl;

    // Delete the column with max
    delete [] arr[max_value_column];
    columns--;
    // Shift columns to the right of the deleted column left one
    for (int col = max_value_column; col < columns; col++) {
        arr[col] = arr[col + 1];
    }
    print_matrix(arr, rows, columns);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM