Sunday, March 17, 2013

To Print Diagonals Of A Matrix

#include<iostream>
using namespace std;
void diag(int a[30][30],int,int);
int main()
{
    int a[30][30],m,n,i,j;
    cout<<"Enter row size: ";
    cin>>m;
    cout<<"Enter column size: ";
    cin>>n;
    cout<<"\n\nEnter matrix elements: \n\n";
    for(i=0;i<m;++i)
    {
        for(j=0;j<n;++j)
        cin>>a[i][j];
    }
    cout<<"\n\nThe matrix is: \n\n";
    for(i=0;i<m;++i)
    {
        for(j=0;j<n;++j)
        {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }
    //diag(a,m,n);
    cout<<"The diagonals of the matrix are: \n\n";
    diag(a,m,n);
   /* for(i=0;i<m;++i)
    {
        for(j=0;j<n;++j)
        {
            cout<<a[i][j]<<" ";
        }
        cout<<endl;
    }*/
    return 0;
}
void diag(int a[30][30],int m,int n)
{
    for(int i=0;i<m;++i)
    {
        for(int j=0;j<n;++j)
        {
            if((i==j)||(i+j==n-1))
            cout<<a[i][j];
            else
            cout<<" ";
        }
        cout<<endl;
    }
}

0 comments:

Post a Comment