Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Matrices y punteros

Estas en el tema de Matrices y punteros en el foro de C/C++ en Foros del Web. En el siguiente código tengo un problema al escanear la matriz de un archivo, alguien puede orientarme? Muchas gracias @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código C: Ver original ...
  #1 (permalink)  
Antiguo 28/11/2012, 18:56
Avatar de rodrigoemece  
Fecha de Ingreso: septiembre-2011
Mensajes: 68
Antigüedad: 12 años, 6 meses
Puntos: 1
Información Matrices y punteros

En el siguiente código tengo un problema al escanear la matriz de un archivo, alguien puede orientarme? Muchas gracias

Código C:
Ver original
  1. struct matriz{
  2.   float *datos;
  3.   int fil;
  4.   int col;
  5.   };
  6.  
  7. void ler(struct matriz *mt)
  8. {
  9.   int i=0;
  10.   int j=0;
  11.   FILE *fp;
  12.   fp = fopen("matrix.txt","rt");
  13.   if(fp==NULL)
  14.    {
  15.      printf("Erro na apertura do arquivo.\n");
  16.      exit(1);
  17.    }
  18.    
  19.      fscanf(fp,"%d",&mt[1].fil);
  20.      fscanf(fp,"%d",&mt[1].col);
  21.      mt[1].datos=(float*)malloc(mt[1].fil*mt[1].col*sizeof(float));
  22.      while(i<mt[1].fil)
  23.      {
  24.          while(j<mt[1].col)
  25.          {
  26.              fscanf(fp,"%f",(mt[1].datos + i*mt[1].col+j));
  27.              j++;
  28.          }        
  29.          i++;    
  30.      }        
  31.      
  32.      i=0;
  33.      j=0;
  34.      fscanf(fp,"%d",&mt[2].fil);
  35.      fscanf(fp,"%d",&mt[2].col);
  36.      mt[2].datos=(float*)malloc(mt[2].fil*mt[2].col*sizeof(float));
  37.      while(i<mt[2].fil)
  38.      {
  39.          while(j<mt[2].col)
  40.          {
  41.              fscanf(fp,"%f",(mt[2].datos + i*mt[2].col+j));
  42.              j++;
  43.          }        
  44.          i++;    
  45.      }  
  46.  
  47. }
  48.  
  49.  
  50.  
  51. main()
  52. {  
  53.   struct matriz mt[3];
  54.   ler(mt);
  55.   return 0;
  56.    
  57. }
  #2 (permalink)  
Antiguo 29/11/2012, 04:27
Avatar de Heimish2000  
Fecha de Ingreso: enero-2011
Ubicación: Madrid
Mensajes: 844
Antigüedad: 13 años, 2 meses
Puntos: 89
Respuesta: Matrices y punteros

¿Y qué error es?
  #3 (permalink)  
Antiguo 30/11/2012, 08:40
Avatar de rodrigoemece  
Fecha de Ingreso: septiembre-2011
Mensajes: 68
Antigüedad: 12 años, 6 meses
Puntos: 1
Respuesta: Matrices y punteros

Error en el uso de punteros :S
  #4 (permalink)  
Antiguo 30/11/2012, 08:44
Avatar de rodrigoemece  
Fecha de Ingreso: septiembre-2011
Mensajes: 68
Antigüedad: 12 años, 6 meses
Puntos: 1
Respuesta: Matrices y punteros

De todas formas he desistido en el uso de estructuras junto con punteros :/
Ya tengo casi terminado el programa pero me encuentro ahora con un error de ejecución, es decir, no efectúa correctamente las operaciones, (función sum y función mul):

Código C:
Ver original
  1. void sum(int, int, int, int, float*, float*);
  2. void mul(int, int, int, int, float*, float*);
  3.  
  4. main()
  5. {
  6.   int filA,colA,filB,colB;
  7.   float *mat1,*mat2;
  8.   FILE *p;
  9.     int i,j,t;
  10.     p=fopen("matrices.txt","r");
  11.     if(p==NULL) printf("Error al abrir el archivo");
  12.     else
  13.     {
  14.         fscanf(p,"%d %d",&filA,&colA);
  15.         printf("Matriz A (%dx%d)\n",filA,colA);
  16.         mat1=(float*)malloc(filA*colA*sizeof(float));
  17.         for(i=0; i<filA; i++)
  18.          {
  19.           for(j=0; j<colA; j++)
  20.             {
  21.                fscanf(p,"%f",(mat1+i*colA+j));          
  22.                printf("%f ",*(mat1+i*colA+j));    
  23.             }
  24.             putchar('\n');            
  25.          }
  26.             putchar('\n');    
  27.        
  28.         fscanf(p,"%d %d",&filB,&colB);
  29.         printf("Matriz B (%dx%d)\n",filB,colB);
  30.         mat2=(float*)malloc(filB*colB*sizeof(float));
  31.         for(i=0;i<filB;i++)
  32.          {
  33.           for(j=0;j<colB;j++)
  34.             {
  35.                fscanf(p,"%f",(mat1+i*colB+j));          
  36.                printf("%f ",*(mat1+i*colB+j));    
  37.             }
  38.             putchar('\n');    
  39.          }
  40.     }
  41.            
  42.    do
  43.     {
  44.     printf("\n\n  Escolla a operacion a realizar sobre as duas matrices: \n   1. Sumar\n   2. Multiplicar\n   3. Sair\n\n   Seleccion:  ");
  45.     scanf("%d", &t);
  46.     switch(t)
  47.          {                
  48.            case 1: sum(filA,colA,filB,colB,mat1,mat2); break;
  49.            case 2: mul(filA,colA,filB,colB,mat1,mat2); break;
  50.            case 3: t=999; break;
  51.            default: printf("Por favor, escolla unha das opcions disponhibles");
  52.          }
  53.     }while(t!=999);
  54.      
  55.      free(mat1);
  56.      free(mat2);
  57.      
  58. return 0;
  59. }
  60.  
  61. void sum(int filA, int colA, int filB, int colB, float *mat1, float *mat2)
  62. {
  63.     int filC, colC, i, j;
  64.     float *mat3=NULL;
  65.       if(filA==filB && colA==colB)
  66.       {    
  67.             filC=filA;
  68.             colC=colB;
  69.             mat3=(float*)malloc(filC*colC*sizeof(float));
  70.             printf("\n\nMatriz C (%dx%d)\n",filC,colC);
  71.             for(i=0; i<filA; i++)
  72.               {
  73.                 for(j=0; j<colA; j++)
  74.                   {
  75.                      *(mat3+i*colC+j)=*(mat1+i*colA+j)+*(mat2+i*colB+j);
  76.                      printf("%f ",*(mat3+i*colC+j));
  77.                   }
  78.                 putchar('\n');
  79.                }
  80.        }
  81.       else printf("No coincide la dimension de las matrices\n");
  82.       free(mat3);
  83. }
  84.  
  85.  
  86. void mul(int filA, int colA, int filB, int colB, float *mat1, float *mat2)
  87. {
  88.     int filC, colC, k, i, j;
  89.     float *mat3=NULL;
  90.      if(colA==filB)
  91.      {
  92.             filC=filA;
  93.             colC=colB;
  94.             mat3=(float*)malloc(filC*colC*sizeof(float));
  95.             printf("\n\nMatriz C (%dx%d)\n",filC,colC);
  96.             for(i=0; i<filC; i++)
  97.               {
  98.                for(j=0; j<colC; j++)
  99.                  {
  100.                   for(k=0; k<colA; k++)
  101.                     {
  102.                        *(mat3+i*colC+j)=*(mat3+i*colC+j)+*(mat1+i*colA+k)**(mat2+k*colB+j);        
  103.                     }
  104.                   printf("%f ",*(mat3+i*colC+j));
  105.                  }  
  106.                putchar('\n');  
  107.               }            
  108.      }
  109.       else printf("El numero de columnas de la matriz 1 es distinto del numero de filas de la matriz 2\n");    
  110.       free(mat3);
  111.     }

Gracias, un saludo!

Etiquetas: int, matrices, punteros
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:42.