Discussion:
Dudas
(demasiado antiguo para responder)
Xancarl
2011-01-19 22:06:54 UTC
Permalink
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?
Pascal J. Bourguignon
2011-01-20 01:11:15 UTC
Permalink
Post by Xancarl
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 {}.
Loading...