Interview Questions   Tutorials   Discussions   Programs   Videos   

C++ - Write a c++ code of diagonal matrix.




501
views
asked marvit August 19, 2014 08:10 AM  

Write a c++ code of diagonal matrix.


           

1 Answers



 
answered By bhjahnavi   0  
/*program for printing the elements in the diagonal of a matrix*/
# include <iostream.h> 
# include <conio.h>
void main()
{
    int i, j, r = 0, c = 0;
    int a [ 10 ][ 10 ];
    clrscr();
    cout << " Enter Number Of Rows & Columns Of 2D Array [ Matrix ] : ";
    cin >> r >> c ;
    cout << " Enter " << r * c << " Values for 2D Array : ";
    for ( i = 0; i < r; i++ )
    {
        for ( j = 0; j < c; j++ )
        {
             cin >> a [ i ][ j ];
        }
    }
    cout << " Values Of 2D Array [ Matrix ] Are : ";
    for ( i = 0; i < r; i++ )
    {
        cout << " \n ";
        for ( j = 0; j < c; j++ )
        {
             cin >> a [ i ][ j ];
        }
    }

if(r==c)
{
  for(i=0; i<r; i++)
  {
   for(j=0; j<c; j++)
   {
     if(i+j==0 || i+j==2 || i+j==4)
     {
      cout<<a[i][j];
     }
   }
  }
} 
}
  

flag   
   add comment

Your answer

Join with account you already have

FF

Preview


Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!

Alert