Home > Back-end >  Delete a row or a column in a matrix
Delete a row or a column in a matrix

Time:09-10

A RxC matrix was input, I have to delete a random colum or row, for example I delete the row number 2, and my code work perfectly with matrix that have below 4 rows, I use the for loop that replace the row which we want to delete by the upper row and the reduce the number of row in the Matrix by 1, but with matrix that have more than 3 rows, this will not work because if we replace the second row with value of the third row and reduce the number of row by 1, we will lost the last row of the matrix and here is my code, sorry for my bad English, any help would be appreciated.

//k is the row which i want to delete
if(k==0){   //in case you want to deleted the first row
    for (int i = 0; i < r; i  )
     {
        for (int j = 0; j < c; j  )                                                                                                    
        {
            a[i][j]=a[i 1][j];                            
        }
     }
    }else{  
    for (int j = 0; j < c; j  )
     {
        a[k][j]=a[k 1][j];
     }
}
r--;



INPUT 4x4 matrix: 1 2 3 4     deleted row number 1:   >>> OUTPUT: 1 2 3 4
                  5 6 7 8                                         9 5 6 8
                  9 5 6 8                                         9 5 6 8
                  0 5 1 2       

CodePudding user response:

Your code only copies the k 1st row to the k-th row, but doesn't copy the rows k 2 to r.


int main()
{
    int a[4][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 0, 1, 2}, {3, 4, 5, 6}};
    int r = 4;
    int c = 4;
    
    for(int i = 0; i < r; i  )
    {
        for(int j = 0; j < c; j  )
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }

    
    int k = 1;
    
    for(k; k < r - 1; k  )
    {
        for(int j = 0; j < c; j  )
        {
            a[k][j] = a[k 1][j];
        }
    }
    
    r--;
    
    printf("\n");
    for(int i = 0; i < r; i  )
    {
        for(int j = 0; j < c; j  )
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }

    return 0;

CodePudding user response:

This separation of the logic of the code in two cases does not make a sense.

Moreover this statement

a[i][j]=a[i 1][j];

can invoke undefined behavior for the initial matrix when i is equal to r - 1 because in this case the index expression i 1 will be equal to r.

And in the else part of the code you are not copying all required rows of the matrix.

Your code can look the following way

//k is the row which i want to delete
if ( 0 <= k && k < r )
{
    for ( int i = k   1; i < r; i   )
    {
        for ( int j = 0; j < c; j   )                                                                                                    
        {
            a[i-1][j] = a[i][j];                            
        }
    }

    --r;
}

Pay attention to that instead of the signed type int of the variable k it is much better to use an unsigned integer type as for example size_t.

CodePudding user response:

Delete a row of the array:

void *deleteRow(size_t rows, size_t cols, int (*arr)[cols], size_t row)
{
    if(rows && cols && arr)
    {
        if(row   1 < rows) // if it is the last row nothing has to be copied
        {
            memmove(arr[row], arr[row   1], (rows - row - 1) * sizeof(*arr));
        }
    }
    return arr;
}
  • Related