Wednesday, March 20, 2013

Printing Diagonals Of A Matrix

This is used to print the diagonals of a square matrix, to find the sum of the diagonal elements.....
Here in one diagonal the index values are same and in the other the sum of the indeces is (n-1)....



Program:


#include<iostream>
#include<iomanip>
#include<windows.h>


using namespace std;




int main()
{
                            system("cls");
                            int n;
                            cout<<setw(54)<<"*****------------------------------*****\n";
                            cout<<setw(54)<<"*****PRINTING DIAGONALS OF A MATRIX*****\n";
                            cout<<setw(54)<<"*****------------------------------*****\n";
                            cout<<"\n\n";
                            int a[3][3],i,j,sum=0;
                            cout<<"\tEnter size of square matrix: ";
                            cin>>n;
                            cout<<"Enter matrix elements : ";
                            for(i=0;i<n;++i)
                            {
                                 for(j=0;j<n;++j)
                                 {
                                     cout<<"Enter ["<<i<<"]["<<j<<"] : ";
                                      cin>>a[i][j];
                                 }
                            }
                            cout<<"\n\nThe matrix is : \n\n";
                            for(i=0;i<n;++i)
                            {
                                for(j=0;j<n;++j)
                                {
                                     cout<<a[i][j]<<" ";
                                }
                                cout<<endl;
                            }
                            cout<<"\n\nThe left diagonal is : \n";
                            for(i=0;i<n;++i)
                            {
                                for(j=0;j<n;++j)
                                {
                                     if(i==j)
                                     cout<<a[i][j];
                                     else
                                     cout<<" ";
                                }
                                cout<<endl;
                            }
                            cout<<"\n\nThe right diaconal is : \n";
                            for(i=0;i<n;++i)
                            {
                                for(j=0;j<n;++j)
                                {
                                     if(i+j==(n-1))
                                     cout<<a[i][j];
                                     else
                                     cout<<" ";
                                }
                                cout<<endl;
                             }

                             for(i=0;i<n;++i)
                             {
                                 for(j=0;j<n;++j)
                                 {
                                     if(i==j)
                                     sum+=a[i][j];
                                 }
                             }
                             cout<<"\n\nThe sum of left diagonal is : "<<sum;
                             for(i=0;i<n;++i)
                             {
                                 for(j=0;j<n;++j)
                                 {
                                     if(i+j==2)
                                     sum+=a[i][j];
                                 }
                             }
                             cout<<"\n\nThe sum of right diagonal is : "<<sum;
                             cout<<"\n\nDiagonals of the matrix is : \n\n";
                             for(i=0;i<n;++i)
                             {
                                 for(j=0;j<n;++j)
                                 {
                                     if((i==j)||(i+j==(n-1)))
                                     cout<<a[i][j];
                                     else
                                     cout<<" ";
                                 }
                                 cout<<endl;
                             }

                            return 0;
}



Output:



0 comments:

Post a Comment