🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: es.comp.lenguajes.c
2 messages
2 total messages Started by Xancarl Wed, 19 Jan 2011 22:06
Dudas
#824
Author: Xancarl
Date: Wed, 19 Jan 2011 22:06
2 lines
1174 bytes
Alguien me puede dar una idea de como se declaran las variables de una 
matriz cuando todav�a mo se sabe el rango o la dimension que van a tener? 
Re: Dudas
#825
Author: Pascal J. Bourguignon
Date: Thu, 20 Jan 2011 01:11
126 lines
5166 bytes
"Xancarl" <a@a.com> writes:

> Alguien me puede dar una idea de como se declaran las variables de una 
> matriz cuando todav�a mo se sabe el rango o la dimension que van a tener? 

{
    Element element;
    int cols=read_integer(stream);
    int rows=read_integer(stream);

    Matrix* m=make_matrix(cols,rows);
    element=matrix_element(m,c,r);
    matrix_set_element(m,c,r,element);
}

Luego, se puede preguntar como implementar make_matrix, pero eso es otra
cosa.



El problema, es que en C no hay matrices, solamente vectores (lo que
llaman 'array').  Hay varias manejas para almacenar los datos de un
matriz usando vectores.  Las dos principales son:

- usar un vector de vector.  Como no sabes de antemano el tama�o de las
  columnas, tienes que hacer un vector de punteros de vectores.  Esa
  soluci�n puede ser interesante, en particular cuando el tama�o de cada
  columna puede ser diferente. Pero no me gusta mucho para las matrices.


      #include <iso646.h>
      typedef float Element;
      typedef Element* Column;
      typedef struct {
         int cols;
         int rows;
         Column* columns;
      }      Matrix;


      Matrix* make_matrix(int cols,int rows){
          Matrix* m=malloc(sizeof(*m));
          if(m==0){ error("Out of memory"); }
          m->cols=cols;
          m->rows=rows;
          m->columns=malloc(sizeof(*(m->columns))*cols);
          if(m->columns==0){ free(m);  error("Out of memory"); }
          {
              int i;
              for(i=0;i<cols;i++){
                 m->columns[i]=malloc(sizeof(*(m->columns[0]))*rows);
                 if(m->columns[i]==0){
                     while(0<i){
                        free(m->columns[--i]);
                     }
                     free(m->columns);
                     free(m);
                     error("Out of memory");
                 }
              }
          }
          return(m);
      }


      Element matrix_element(Matrix* m,int c,int r){
         if((0<=c) and (c<m->cols) and (0<=r) and (r<m->rows)){
            return(m->columns[c][r]);
         }else{
            error("Out of bound");
         }
      }

      void matrix_set_element(Matrix* m,int c,int r,Element e){
         if((0<=c) and (c<m->cols) and (0<=r) and (r<m->rows)){
            m->columns[c][r]=e;
         }else{
            error("Out of bound");
         }
      }

                   

- usar un vector por todos los elementos de la matriz, y usar una
  funci�n para acceder a estos.

      #include <iso646.h>
      typedef float Element;
      typedef struct {
         int cols;
         int rows;
         Element* elements;
      }      Matrix;


      Matrix* make_matrix(int cols,int rows){
          Matrix* m=malloc(sizeof(*m));
          if(m==0){ error("Out of memory"); }
          m->cols=cols;
          m->rows=rows;
          m->elements=malloc(sizeof(*(m->elements))*cols*rows);
          if(m->elements==0){ free(m);  error("Out of memory"); }
          return(m);
      }

      Element matrix_element(Matrix* m,int c,int r){
         if((0<=c) and (c<m->cols) and (0<=r) and (r<m->rows)){
            return(m->elements[c*m->cols+r]);
         }else{
            error("Out of bound");
         }
      }

      void matrix_set_element(Matrix* m,int c,int r,Element e){
         if((0<=c) and (c<m->cols) and (0<=r) and (r<m->rows)){
            m->elements[c*m->cols+r]=e;
         }else{
            error("Out of bound");
         }
      }



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads