Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help Please [Question] (http://www.go4expert.com/forums/help-please-question-t22073/)

big2soad 11May2010 21:09

Help Please [Question]
 
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.

Thanks in advance.

big2soad 11May2010 21:27

Re: Help Please [Question]
 
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

shaju1981 12May2010 11:48

Re: Help Please [Question]
 
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);
}



All times are GMT +5.5. The time now is 20:13.