Detailed Print – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  


Shop
siteicon
Solved Problem#CPP#5881

Problem Statement - Reversing each column in a 2D Matrix

Write a function REVCOL (int P[][5], int N, int M) in C++to display the content of a two dimensional array, with each column content in reverse order.
Note: Array may contain any number of rows.
For example, if the content of array is as follows:

15 12 56 45 51
13 91 92 87 63
11 23 61 46 81

The function should display output as:

11      23     61    46    81
13      91     92    87    63
15      12     56    45    51

Solution

Click to open popup

Solved Problem Understanding

void REVCOL(int P[][5],int N,int M)
{
  for(int I=N-1;I>=0;I--)
  {
    for(int J=0;J<M;J++)
    cout<<P[I][J];
    cout<<endl;
  }
}
OR
void REVCOL(int P[][5],int N,int M)
{
  for(int I=0;I<N/2;I++)
  {
    for(int J=0;J<M;J++)
    {
      int T = P[I][J];
      P[I][J] = P[N-I-1][J];
      P[N-I-1][J] = T;
    }
  }
  for(I=0;I<N;I++)
  {
    for(int J=0;J<M;J++)
      cout<<P[I][J];
    cout<<endl;
  }
}