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
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;