-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path05_sum_of_left_diagonals.c
67 lines (55 loc) · 1.7 KB
/
05_sum_of_left_diagonals.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// // Write a program in C to find the sum of left diagonals of a matrix.
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define MAX_ROWS 10
#define MAX_COLS 10
// // Main Function Start
int main()
{
const int ROWS, COLS;
printf("\nEnter Order of Square Matrix-A (Rows x Cols) (MAX %d x %d) => ", MAX_ROWS, MAX_COLS);
scanf("%d%d", &ROWS, &COLS);
// // Check Invalid Input for Matrix Order
if (ROWS < 1 || ROWS != COLS || ROWS > MAX_ROWS || COLS < 1 || COLS > MAX_COLS)
{
puts("\n!!! Invalid order of Matrix, Plz Enter Appropriate Order...\n");
exit(0);
}
// // Declare 2-d Array of Entered Order
int matrixA[ROWS][COLS], sumOfLeftDiagonals = 0;
// // Input Elements of Matrix-A
printf("\n>>>>>> Enter Elements of Matrix-A of Order %d x %d <<<<<<<\n", ROWS, COLS);
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
printf("\nEnter element[%d][%d] => ", i + 1, j + 1);
scanf("%d", &matrixA[i][j]);
}
}
// // Print Matrix-A
printf("\n\n>>>>>>>> Matrix-A of %d x %d <<<<<<<<<\n", ROWS, COLS);
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
printf("%4d ", matrixA[i][j]);
putch(10); // // Add New line
}
// // Find Sum of Left Diagonals
for (int i = 0; i < ROWS; i++)
{
for (int j = 0; j < COLS; j++)
{
if (i == j)
sumOfLeftDiagonals += matrixA[i][j];
}
}
// // Print Sum of Left Diagonals
printf("\nSum of Left Diagonals => %d", sumOfLeftDiagonals);
putch('\n');
getch();
return 0;
}
// // Main Function End