Ver Mensaje Individual
  #4 (permalink)  
Antiguo 10/02/2014, 14:04
vosk
 
Fecha de Ingreso: agosto-2012
Mensajes: 601
Antigüedad: 11 años, 8 meses
Puntos: 83
Respuesta: Imprimir solo dos caracteres de una matriz en c

No, max y min son los valores reales maximo y minimo encontrados, no las coordenadas de dichos valores dentro de la matriz:

Código C:
Ver original
  1. int m[2][3] = {1,2,3,4,5,6};
  2. int i, j, min = INT_MAX, max = INT_MIN;
  3. for (i=0;i<2;i++) {
  4.     for (j=0;j<3;j++)
  5.     {  
  6.         if(m[i][j] < min) {
  7.             min = m[i][j];
  8.             printf("Sobreescribo min %d\n", min);
  9.         }
  10.         if(m[i][j] > max) {
  11.             max = m[i][j];
  12.             printf("Sobreescribo max %d\n", max);
  13.         }
  14.     }
  15.     printf("\n");
  16. }
  17. printf("Al final el minimo valor dentro de la matriz es %d, y el maximo es %d", min, max);

Si quieres guardar las coordenadas de la posicion del minimo y del maximo tienes que declarar las variables para guardar las dos coordenadas de cada posicion (2 para max y dos para min). Algo asi:

Código C:
Ver original
  1. typedef struct {
  2.     int x, y;
  3. } COORD;
  4.  
  5. int main() {
  6.     int m[2][3] = {1,2,3,4,5,6};
  7.     int i, j, min = INT_MAX, max = INT_MIN;
  8.     COORD coord_min, coord_max;
  9.  
  10.     for (i=0; i<2; i++) {
  11.         for (j=0; j<3; j++) {
  12.             printf(" %d, ",m[i][j]);
  13.             if(m[i][j] < min) {
  14.                 min = m[i][j];
  15.                 coord_min.y = i;
  16.                 coord_min.x = j;
  17.             }
  18.             if(m[i][j] > max) {
  19.                 max = m[i][j];
  20.                 coord_max.y = i;
  21.                 coord_max.x = j;
  22.             }
  23.         }
  24.         printf("\n");
  25.     }
  26.  
  27.     printf("MIN en [%d][%d] = %d = %d\n", coord_min.x, coord_min.y, m[coord_min.y][coord_min.x], min);
  28.     printf("MAX en [%d][%d] = %d = %d\n", coord_max.x, coord_max.y, m[coord_max.y][coord_max.x], max);
  29.  
  30.     return 0;
  31. }

Saludos
vosk