Ver Mensaje Individual
  #2 (permalink)  
Antiguo 08/08/2009, 20:29
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 2 meses
Puntos: 1360
Respuesta: cambiar orden de filas en una matriz

Disfrutalo:

Código C:
Ver original
  1. #include <stdio.h>
  2.  
  3. int print_r(int *arr, int h, int w);
  4. int cambiar(int *arr, int h, int w, int row1, int row2);
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     int array[3][3] = {10, 20, 30,
  9.                        40, 50, 60,
  10.                        70, 80, 90};
  11.     int alto = 3, ancho = 3;
  12.     print_r(array[0], alto, ancho);
  13.     cambiar(array[0], alto, ancho, 0, 1);
  14.     print_r(array[0], alto, ancho);
  15.     printf("\nPresione la tecla enter para continuar...");
  16.     getchar();
  17.     return 0;
  18. }
  19.  
  20. int print_r(int *arr, int h, int w){
  21.     printf("\n");
  22.     int i, j, t = h * w;
  23.     for(i = 0; i < h; i++){
  24.         for(j = 0; j < w; j++)
  25.         printf("[%d][%d] = %d  ", i + 1, j + 1, *(arr + i*h + j));
  26.             printf("\n");
  27.     }
  28.     return 0;
  29. }
  30.  
  31. int cambiar(int *arr, int h, int w, int row1, int row2){
  32.     int j, aux;
  33.     if (h < 2)
  34.         return 1;
  35.  
  36.     for(j = 0; j < w; j++){
  37.         aux = *(arr + h*row1 + j);
  38.         *(arr + h*row1 + j) = *(arr + h*row2 + j);
  39.         *(arr + h*row2 + j) = aux;
  40.     }
  41.  
  42.     return 0;
  43. }