El Jueves 12 Julio 2007, Federico del Rio Garcia escribió:
> Hola,
> necesito crear un array bidimensional en C++ (tipo var[FIL][COL]) pero como
> no conozco el tamaño en tiempo de compilacion quiero hacerlo en memoria
> dinamica, y no encontre como. Con el operador new no puedo hacer algo del
> estilo tipo *var = new tipo[FIL][COL];
Hasta donde sé, no se puede. Por eso hace un tiempo me hice una clase matriz,
que adjunto.
Ojo, no digo que el código sea bueno :-)
--
<elruso> angasule: tenes el teletransportador encendido?
<gurtaj> :P
<angasule> si
<elruso> Yo no :-P
<angasule> lo arme con el PIC16F84, una batata y algo que estaba
adentro de una zapatilla vieja
<elruso> angasule: yo tenia uno andando pero speedy te
bloquea el puerto...
Lisandro Damián Nicanor Pérez Meyer
http://perezmeyer.com.ar/
#bblug irc.freenode.net
/***************************************************************************
* Copyright (C) 2005 by Lisandro Damián Nicanor Pérez Meyer *
* [EMAIL PROTECTED] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "matriz.h"
#include <iostream>
using namespace std;
Matriz::Matriz( int dimension )
{
esCuadrada = 1;
filas = dimension;
columnas = dimension;
arreglo = new double[filas * columnas];
/* Llenamos la matríz con ceros, por las dudas */
int i;
for( i=0; i<( filas * columnas ); i++ )
arreglo[i] = 0;
}
Matriz::Matriz( int cantFilas, int cantColumnas )
{
esCuadrada = 0;
if( cantFilas == cantColumnas )
esCuadrada = 1;
filas = cantFilas;
columnas = cantColumnas;
arreglo = new double[filas * columnas];
/* Llenamos la matríz con ceros, por las dudas */
int i;
for( i=0; i<( filas * columnas ); i++ )
arreglo[i] = 0;
}
Matriz::~Matriz()
{
delete[] arreglo;
}
int Matriz::GetDimension ()
{
if( esCuadrada==1 )
return filas;
else
return 0;
}
int Matriz::ChequearMatriz(int &laFila, int &laColumna )
{
/* Chequeo de suma por fila, debe ser ==1 */
for( laFila=0; laFila<filas; laFila++ )
{
double suma = 0;
for( laColumna=0; laColumna<columnas ; laColumna++ )
{
if( GetValor( laFila, laColumna )>1 )
{
printf("El valor de la fila %i columna %i es %1.20f \n",laFila,laColumna,GetValor(laFila,laColumna));
return 1;
}
suma += GetValor( laFila,laColumna );
}
if( suma < 0.9999 )
{
printf("El valor de la suma de la fila %i es %1.20f \n",laFila,suma);
return 2;
}
if( suma > 1.0001 )
{
printf("El valor de la suma de la fila %i es %1.20f \n",laFila,suma);
return 3;
}
}
return 0;
}
double Matriz::GetValor (int laFila, int laColumna)
{
return arreglo[(laFila * columnas) + laColumna];
}
void Matriz::SetValor (int laFila, int laColumna, double valor)
{
arreglo[(laFila * columnas) + laColumna] = valor;
}
/***************************************************************************
* Copyright (C) 2005 by Lisandro Damián Nicanor Pérez Meyer *
* [EMAIL PROTECTED] *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
/**
El propósito de la clase Matriz es obtener una matríz de dimensiones
filaxcolumna, de forma dinámica.
Esto se logra creando un arreglo sin dimensión inicial, para luego
en el constructor obtener el valor de dimension, y creando el arreglo
de manera acorde.
La "matriz" se accede con valores 0..(n-1)x0..(n-1), como cualquier
otro arreglo en C++.
*/
#ifndef MATRIZ_H
#define MATRIZ_H
class Matriz
{
public:
Matriz(int dimension);
Matriz(int cantFilas, int cantColumnas );
~Matriz();
int GetDimension();
int ChequearMatriz( int & laFila, int & laColumna );
double GetValor( int laFila, int laColumna );
void SetValor( int laFila, int laColumna, double valor );
private:
double *arreglo;
/** Booleano para saber si la matríz es o no cuadrada **/
int esCuadrada;
int filas;
int columnas;
};
#endif // MATRIZ_H
_______________________________________________
Lista de correo Programacion.
[email protected]
http://listas.fi.uba.ar/mailman/listinfo/programacion