The trouble is you think you have time. Your success depends on how you utilize your time.

C++ Program to print middle row and middle column of a matrix in 2D array

Write a C++ program which accepts a 2D array of integers and its size as arguments and display the elements of middle row and the elements of middle column.
[Assuming the 2D Array to be a square matrix with odd dimension i.e 2x3, 5x5, 7x7 etc....].
Example, If the array contents is
2  5  4
7  6  9
2  1  8
Output through the program should be:
Middle Row: 7  6  9
Middle Column: 5  6  1



#include<iostream>
using namespace std;
int main()
{ cout<<"[moznum.blogspot.com]"<<endl<<endl; 

 int size;
 cout<<"Enter the order matrix ( Order Shoud B in Odd Demension) : ";
 cin>>size;
  int A[size][size];
  for(int i=0;i<size;i++)   //taking input
  {
  for(int j=0;j<size;j++)
   {
    cout<<"Enter the "<< i+1<<j+1 <<" value of Matrix : ";
    cin>>A[i][j];
   }
  }

  cout<<"Middle Row is: ";   // printing the mid row
  for (int i=0;i<size;i++)
  {
   for(int j=0;j<size;j++)
    {
    if (i== size/2)
    cout<<A[i][j]<<"\t";
    }
  
  
  }  
  cout<<"\nMiddle Column is:";    // printing the mid col
   for (int i=0;i<size;i++)
      {
         for(int j=0;j<size;j++)
    {
    if(j==size/2)
       cout<<A[i][j]<<"\t";
       }
  }

}

You Can also Search This topic by this:

3 comments: