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.

``````/*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

### Join with account you already have

Preview

Online-Classroom Classes
www.writeabc.com

1 person following this question

Question tags

c++ × 1

Asked 3 years and 5 months ago ago
Number of Views -501