Ver Mensaje Individual
  #4 (permalink)  
Antiguo 06/11/2012, 18:48
Avatar de cotolon
cotolon
 
Fecha de Ingreso: octubre-2012
Mensajes: 55
Antigüedad: 11 años, 6 meses
Puntos: 10
Respuesta: Error al pasar matriz por referencia a función

Estuve leyendo un poco y la mejor forma es declarar un puntero bidimensional en main y pasar ese como referencia a la función, así queda el código (si funciona)

Código C++:
Ver original
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define F 10
  7. #define C 10
  8.  
  9.  
  10. using namespace std;
  11.  
  12.  
  13. void punto_silla (int **matriz)
  14. {
  15.      int MinCol=100, ContCol=0;
  16.      int MaxFil=0, ContFil=0;
  17.      int PuntoS=0;
  18.      for (int j=0; j<C; j++)
  19.       {
  20.       for (int i=0; i<F; i++)
  21.          if (matriz[i][j]<=MinCol)
  22.               {
  23.               MinCol=matriz[i][j];
  24.               ContCol=i;
  25.               }
  26.       for (int j2=0; j2<C; j2++)
  27.          MaxFil=MaxFil>=matriz[ContCol][j2]?MaxFil:matriz[ContCol][j2];
  28.       if (matriz[ContCol][j]==MaxFil)
  29.         {
  30.         PuntoS++;
  31.         cout<<"Hay un punto de silla ("<<matriz[ContCol][j]<<") en:"<<endl;
  32.         cout<<"Fila: "<<j+1<<'t'<<"Columna: "<<ContCol+1<<endl;
  33.         }
  34.       }
  35.      cout<<"Hay "<<PuntoS<<" puntos de silla"<<endl;
  36. }
  37. void matriz_aleatoria(int **matriz)
  38. {
  39.  
  40.  
  41.       srand(time(0));
  42.       for (int i=0; i<F; i++)
  43.           for (int j=0; j<C; j++)
  44.              matriz[i][j]= rand()%100;
  45.  
  46.  
  47.  
  48.  
  49.  
  50. }
  51. void escribe_matriz(int **matriz)
  52. {
  53.      for (int i=0; i<F; i++)
  54.           for (int j=0; j<C; j++)
  55.           {
  56.           cout<<matriz[i][j]<<'t';
  57.           cout<<endl;
  58.           }
  59.  
  60. }
  61. int main()
  62. {
  63.     int matriz[F][C];
  64.     //Creamos puntero de la matriz para pasar el puntero:
  65.     int **pmatriz;
  66.     //Array de punteros a int (A las filas solamente)
  67.     pmatriz = new int*[F];
  68.     //F "Cajones" de C ints (Ahora a las columnas)
  69.     for(int x = 0; x < F; x++)
  70.         pmatriz[x] = new int[C];
  71.  
  72.     matriz_aleatoria(pmatriz);
  73.     escribe_matriz(pmatriz);
  74.     punto_silla (pmatriz);
  75.  
  76.     system("PAUSE");
  77.     return EXIT_SUCCESS;
  78.  
  79. }