Write your own C program that reads through real matrix, 10x10 dimensioned and finds the smallest element in main diagonal and smallest element in secondary diagonal.

Code:
```#include <stdio.h>

#define NR_ROW      10
#define NR_COL      10

int main(void) {

int     i, j;
float mat[NR_ROW][NR_COL], min_maindg, min_secdg;

printf("Input matrix elements :");
for (i = 0; i < NR_ROW; i++) {
for (j = 0; j < NR_COL; j++) {

printf("\nInput element [%d][%d] : ", i, j);
scanf("%f", &mat[i][j]);

}
}

min_maindg = mat[0][0];

//min el. is mat(0,0), this is why loop starts from 1

for (i = 1; i < NR_ROW; i++) {

if (mat[i][i] < min_maindg) {

min_maindg = mat[i][i];

}
}

min_secdg = mat[0][NR_COL -1];
for (i = 1; i < NR_ROW; i++) {

if (mat[i][NR_COL-i-1] < min_secdg) {

min_secdg = mat[i][NR_COL-i-1];

}
}

printf("\nSmallest el. in main diagonal is: %f",
min_maindg);
printf("\nSmallest el. in second diagonal is: %f",
min_secdg);
}

Shorter version – single run through the matrix:

#include <stdio.h>

#define NR_ROW     10
#define NR_COL     10

int main(void) {

int     i, j;
float mat[NR_ROW][NR_COL], min_maindg, min_secdg;

printf("\nInput matrix elements :\n");
for (i = 0; i < NR_ROW; i++) {
for (j = 0; j < NR_COL; j++) {

printf("\nInput element [%d][%d] : ", i, j);
scanf("%f", &mat[i][j]);

if (i == 0 && j == 0) {

min_maindg = mat[i][j];

}
if (i == 0 && j == NR_COL - 1) {

min_secdg = mat[i][j];

}
if (i == j) {
if (mat[i][j] < min_maindg) {

min_maindg = mat[i][j];

}
}
if (i == NR_COL - 1 - j) {
if (mat[i][j] < min_secdg) {

min_secdg = mat[i][j];

}
}
}
}
printf("\nSmallest element in main diagonal is : %f",
min_maindg);
printf("\nSmallest element in second diagonal is : %f",
min_secdg);
}```

Last edited by shabbir; 12May2010 at 12:35.. Reason: Code blocks