0% found this document useful (0 votes)
28 views2 pages

1 Diagnol

This C++ code defines functions to read in a matrix from the user, write out the matrix, and calculate the sum of the diagonals of the matrix. It takes in the matrix size n from the user, calls the readmatrix function to populate the matrix, then calls the diagonalsum function to calculate the sums of the first diagonal, second diagonal, and total sum, outputting the results. It then calls the main function that brings all the parts together - getting the matrix size, populating the matrix, calling the diagonal sum function, and outputting the results.

Uploaded by

Priyanshu Modi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views2 pages

1 Diagnol

This C++ code defines functions to read in a matrix from the user, write out the matrix, and calculate the sum of the diagonals of the matrix. It takes in the matrix size n from the user, calls the readmatrix function to populate the matrix, then calls the diagonalsum function to calculate the sums of the first diagonal, second diagonal, and total sum, outputting the results. It then calls the main function that brings all the parts together - getting the matrix size, populating the matrix, calling the diagonal sum function, and outputting the results.

Uploaded by

Priyanshu Modi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

SOURCE CODE

(Ajay,Paritosh)
//To calculate diagonal sum
#include <iostream.h>
#include <conio.h>

void readmatrix (int a[10][10], int n)


{
int i, j;
for (i = 0; i < n; ++i)
for (j = 0; j < n; ++j)
cin >> a[i][j];
cout << endl ;
}
void writematrix (int a[10][10])
{
int i, j, n;
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
cout << a[i][j]
<< endl;
}
}
void diagonalsum (int a[10][10], int n)
{
int sum1 = 0, sum2 = 0, i, j;
for (i = 0; i < n; ++i)
{
sum1 += a[i][i];
sum2 += a[i][n-i-1];
}
cout <<"\nSum of first diagonal = " << sum1;
cout <<"\nSum of second diagonal = " << sum2;
cout <<"\nTotal sum = " << sum1 + sum2;
}
void main ()
{
clrscr ();
int a[10][10], n;
cout <<"\nEnter the limit = ";
cin >> n;
cout <<"\nEnter the matrix = \n";
readmatrix (a, n);
cout <<"\nSum of Diagonals = \n";
diagonalsum (a, n);
getch ();
}

OUTPUT

You might also like