Wednesday, February 21, 2024

WAP to implement Diagonal Matrix using one-dimensional array.

 // C++ Program to print the Diagonals of a Matrix


#include <bits/stdc++.h>

using namespace std;

#define MAX 20




// Function to print the Principal Diagonal

void printPrincipalDiagonal(int mat[][MAX], int n)

{

cout << "Principal Diagonal: ";


for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {


// Condition for principal diagonal

if (i == j)

cout << mat[i][j] << ", ";

}

}

cout << endl;

}


// Function to print the Secondary Diagonal

void printSecondaryDiagonal(int mat[][MAX], int n)

{

cout << "Secondary Diagonal: ";


for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {


// Condition for secondary diagonal

if ((i + j) == (n - 1))

cout << mat[i][j] << ", ";

}

}

cout << endl;

}


// Driver code

int main()

{

int n ;

int a[20][20]; 

cin>>n;

cout<<"enter eleents";

for (int i = 0; i < n; i++) {

for (int j = 0; j < n; j++) {


cin>> a[i][j] ;

}

}


printPrincipalDiagonal(a, n);

printSecondaryDiagonal(a, n);

return 0;

}


No comments:

Post a Comment