# Help Please [Question]

Discussion in 'C' started by big2soad, May 11, 2010.

1. ### big2soadNew Member

Joined:
May 11, 2010
Messages:
2
0
Trophy Points:
0
hi all
can you please write this program ?

1-Declare 8 by 8 integer array.
2-write a function that is named Getmax that will receive the array that was declared in part 1 as a parameter and find the maximum value among array's secondary diagonal elements .The function should return the maximum value.

2. ### big2soadNew Member

Joined:
May 11, 2010
Messages:
2
0
Trophy Points:
0
btw it's not a demand !!
i have no idea about this problem so if you can help to know how to solve it that would be great

3. ### shaju1981New Member

Joined:
Jan 21, 2010
Messages:
3
0
Trophy Points:
0
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;

//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[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 a moderator: May 12, 2010