Anda di halaman 1dari 6

Arrays

1. [001.txt] Crea una función creaVector que te devuelva un vector de n enteros

Solución:

int ∗ c r e a V e c t o r I n t ( int n ) {
return new int [ n ] ;
}

2. [002.txt] Crea una función creaTabla que te devuelva una tabla cuadrada de n x n enteros

Solución:

int ∗∗ c r e a T a b l a I n t ( int n ) {
int ∗∗ r e t = new int ∗ [ n ] ;
f or ( int i =0; i <n ; i ++){
r e t [ i ] = new int [ n ] ;
}
return r e t ;
}

3. [003.txt] Crea una función creaCuboInt que te devuelva un cubo de n x n x n enteros

Solución:

int ∗∗∗ creaCuboInt ( int n ) {


int ∗∗∗ r e t = new int ∗ ∗ [ n ] ;
f or ( int i =0; i <n ; i ++){
r e t [ i ] = new int ∗ [ n ] ;
f or ( int i 2 =0; i 2 <n ; i 2 ++){
r e t [ i ] [ i 2 ] = new int [ n ] ;
}
}
return r e t ;
}

4. [004.txt] Crea una función buscarEnCuboInt que busque la primera aparición de un número dado dentro de
un cubo de tamaño n Devolverá su dirección de memoria ó 0 si no está

Solución:

int ∗ buscarEnCuboInt ( int num , int s i z e , int ∗∗∗ cubo ) {


f or ( int i 1 =0; i 1 <s i z e ; i 1 ++){
f or ( int i 2 =0; i 2 <s i z e ; i 2 ++){
f or ( int i 3 =0; i 3 <s i z e ; i 3 ++){
i f ( cubo [ i 1 ] [ i 2 ] [ i 3 ] == num) {
return &(cubo [ i 1 ] [ i 2 ] [ i 3 ] ) ;
}
}
}
}
return 0 ;
}

Página 1 de 6
Arrays

5. [005.txt] Crea una función crearCuboCadenas que te devuelva un cubo de n x n x n punteros a char

Solución:

char∗ ∗∗∗ crearCuboCadenas ( int n ) {


char∗ ∗∗∗ r e t = new char∗ ∗ ∗ [ n ] ;
f or ( int i =0; i <n ; i ++){
r e t [ i ] = new char∗ ∗ [ n ] ;
f or ( int i 2 =0; i 2 <n ; i 2 ++){
r e t [ i ] [ i 2 ] = new char∗ [ n ] ;
}
}
return r e t ;
}

6. [006.txt] Crea una función crearCuboCadenas que te devuelva un cubo de n x n x n punteros a char

Solución:

char∗ ∗∗∗ crearCuboCadenas ( int n ) {


char∗ ∗∗∗ r e t = new char∗ ∗ ∗ [ n ] ;
f or ( int i =0; i <n ; i ++){
r e t [ i ] = new char∗ ∗ [ n ] ;
f or ( int i 2 =0; i 2 <n ; i 2 ++){
r e t [ i ] [ i 2 ] = new char∗ [ n ] ;
}
}
return r e t ;
}

7. [007.txt] Generalı́zala con el nombre crearCubo para que pueda crear cubos de cualquier tipo de datos

Solución:

template <c l a s s T> T ∗∗∗ crearCubo ( int n ) {


T ∗∗∗ r e t = new T ∗ ∗ [ n ] ;
f or ( int i =0; i <n ; i ++){
r e t [ i ] = new T ∗ [ n ] ;
f or ( int i 2 =0; i 2 <n ; i 2 ++){
r e t [ i ] [ i 2 ] = new T [ n ] ;
}
}
return r e t ;
}

8. [008.txt] Crea una función buscarEnCubo que te permita buscar algo en cualquier tipo de cubo Devolverá su
dirección de memoria ó 0 si no está

Solución:

Página 2 de 6
Arrays

template <c l a s s T> T ∗ buscarEnCubo (T dato , int s i z e , T ∗∗∗ cubo ) {


f or ( int i 1 =0; i 1 <s i z e ; i 1 ++){
f or ( int i 2 =0; i 2 <s i z e ; i 2 ++){
f or ( int i 3 =0; i 3 <s i z e ; i 3 ++){
i f ( cubo [ i 1 ] [ i 2 ] [ i 3 ] == dato ) {
return &(cubo [ i 1 ] [ i 2 ] [ i 3 ] ) ;
}
}
}
}
return 0 ;
}

9. [009.txt] Crea una clase CuboInt que represente a un cubo de enteros Agrega los siguientes servicios:
un método setValue que pone un valor en un lugar del cubo
un método getValue que obtiene el valor que hay en un lugar del cubo
Un destructor
Un operador de asignacion
Un constructor de copias

Solución:

c l a s s CuboInt {
public :
CuboInt ( int s i z e =1) ;
void s e t V a l u e ( int x , int y , int z , int v a l u e ) ;
int g e t V a l u e ( int x , int y , int z ) ;
˜ CuboInt ( ) ;
CuboInt & operator=(CuboInt & c ) ;
private :
int ∗∗∗ d a t o s ;
int s i z e ;
};
CuboInt : : CuboInt ( int s i z e ) {
this−>s i z e = s i z e ;
d a t o s = new int ∗ ∗ [ s i z e ] ;
f or ( int i 1 =0; i 1 <s i z e ; i 1 ++){
d a t o s [ i 1 ] = new int ∗ [ s i z e ] ;
f or ( int i 2 =0; i 2 <s i z e ; i 2 ++){
d a t o s [ i 1 ] [ i 2 ] = new int [ s i z e ] ;
}
}
}
CuboInt : : ˜ CuboInt ( ) {
f or ( int i 1 =0; i 1 <s i z e ; i 1 ++){
f or ( int i 2 =0; i 2 <s i z e ; i 2 ++){
delete [ ] d a t o s [ i 1 ] [ i 2 ] ;
}
delete [ ] d a t o s [ i 1 ] ;
}
delete [ ] d a t o s ;
}
CuboInt & CuboInt : : operator=(CuboInt & c ) {

Página 3 de 6
Arrays

i f (&c == t h i s ) {
return ∗ t h i s ;
}
f or ( int i 1 =0; i 1 <s i z e ; i 1 ++){
f or ( int i 2 =0; i 2 <s i z e ; i 2 ++){
delete [ ] d a t o s [ i 1 ] [ i 2 ] ;
}
delete [ ] d a t o s [ i 1 ] ;
}
delete [ ] d a t o s ;

this−>s i z e = c . s i z e ;
d a t o s = new int ∗ ∗ [ s i z e ] ;
f or ( int i 1 =0; i 1 <s i z e ; i 1 ++){
d a t o s [ i 1 ] = new int ∗ [ s i z e ] ;
f or ( int i 2 =0; i 2 <s i z e ; i 2 ++){
d a t o s [ i 1 ] [ i 2 ] = new int [ s i z e ] ;
f or ( int i 3 =0; i 3 <s i z e ; i 3 ++){
datos [ i1 ] [ i2 ] [ i3 ] = c . datos [ i1 ] [ i2 ] [ i3 ] ;
}
}
}
return ∗ t h i s ;
}
void CuboInt : : s e t V a l u e ( int x , int y , int z , int v a l u e ) {
datos [ x ] [ y ] [ z ] = value ;
}
int CuboInt : : g e t V a l u e ( int x , int y , int z ) {
return d a t o s [ x ] [ y ] [ z ] ;
}

10. [010.txt] Parametriza la clase para que represente cubos de cualquier tipo de elementos

Solución:

template <c l a s s T> c l a s s Cubo{


public :
Cubo ( int s i z e =1) ;
void s e t V a l u e ( int x , int y , int z , T v a l u e ) ;
T g e t V a l u e ( int x , int y , int z ) ;
˜Cubo ( ) ;
Cubo & operator=(Cubo & c ) ;
protected :
T ∗∗∗ d a t o s ;
int s i z e ;
};
template <c l a s s T> Cubo<T> : : Cubo ( int s i z e ) {
this−>s i z e = s i z e ;
d a t o s = new T ∗ ∗ [ s i z e ] ;
f or ( int i 1 =0; i 1 <s i z e ; i 1 ++){
d a t o s [ i 1 ] = new T ∗ [ s i z e ] ;
f or ( int i 2 =0; i 2 <s i z e ; i 2 ++){
d a t o s [ i 1 ] [ i 2 ] = new T [ s i z e ] ;
}
}

Página 4 de 6
Arrays

}
template <c l a s s T> Cubo<T> : : ˜ Cubo ( ) {
f or ( int i 1 =0; i 1 <s i z e ; i 1 ++){
f or ( int i 2 =0; i 2 <s i z e ; i 2 ++){
delete [ ] d a t o s [ i 1 ] [ i 2 ] ;
}
delete [ ] d a t o s [ i 1 ] ;
}
delete [ ] d a t o s ;
}
template <c l a s s T> Cubo<T> & Cubo<T> : : operator=(Cubo & c ) {
i f (&c == t h i s ) {
return ∗ t h i s ;
}
f or ( int i 1 =0; i 1 <s i z e ; i 1 ++){
f or ( int i 2 =0; i 2 <s i z e ; i 2 ++){
delete [ ] d a t o s [ i 1 ] [ i 2 ] ;
}
delete [ ] d a t o s [ i 1 ] ;
}
delete [ ] d a t o s ;

this−>s i z e = c . s i z e ;
d a t o s = new T ∗ ∗ [ s i z e ] ;
f or ( int i 1 =0; i 1 <s i z e ; i 1 ++){
d a t o s [ i 1 ] = new T ∗ [ s i z e ] ;
f or ( int i 2 =0; i 2 <s i z e ; i 2 ++){
d a t o s [ i 1 ] [ i 2 ] = new T [ s i z e ] ;
f or ( int i 3 =0; i 3 <s i z e ; i 3 ++){
datos [ i1 ] [ i2 ] [ i3 ] = c . datos [ i1 ] [ i2 ] [ i3 ] ;
}
}
}
return ∗ t h i s ;
}
template <c l a s s T> void Cubo<T> : : s e t V a l u e ( int x , int y , int z , T v a l u e ) {
datos [ x ] [ y ] [ z ] = value ;
}
template <c l a s s T> T Cubo<T> : : g e t V a l u e ( int x , int y , int z ) {
return d a t o s [ x ] [ y ] [ z ] ;
}

11. [011.txt] Crea una clase HiperCubo heredera de la anterior en la que se sustituyan los métodos de obtener y
poner un valor por un solo operador sobrecargado que te permita ofrecer ambos servicios. Elige el operador más
adecuado.
Haz que se pueda construir un HiperCubo inicializando todos sus valores con un valor determinado. Sobrecarga
el operador ! para que envı́e el HiperCubo por la pantalla

Solución:

#include ” 0 1 0 . cpp ”
#include <i o s t r e a m >
using namespace s t d ;
template <c l a s s T> c l a s s HiperCubo : public Cubo<T>{
public :

Página 5 de 6
Arrays

HiperCubo ( int s i z e =1) : Cubo<T>( s i z e ) {}


HiperCubo ( int s i z e , T v a l o r I n i c i a l ) : Cubo<T>( s i z e ) {
f or ( int i 1 =0; i 1 <this−>s i z e ; i 1 ++){
f or ( int i 2 =0; i 2 <this−>s i z e ; i 2 ++){
f or ( int i 3 =0; i 3 <this−>s i z e ; i 3 ++){
this−>d a t o s [ i 1 ] [ i 2 ] [ i 3 ]= v a l o r I n i c i a l ;
}
}
}
}
T & operator ( ) ( int x , int y , int z ) ;
void operator ! ( ) ;
};
template <c l a s s T> T & HiperCubo<T> : : operator ( ) ( int x , int y , int z ) {
// CUIDADO! El uso de t h i s −> e s o b l i g a t o r i o a q u i
// para que l a p l a n t i l l a s e g e n e r e c o r r e c t a m e n t e
return this−>d a t o s [ x ] [ y ] [ z ] ;
}
template <c l a s s T> void HiperCubo<T> : : operator ! ( ) {
f or ( int i 1 =0; i 1 <this−>s i z e ; i 1 ++){
f or ( int i 2 =0; i 2 <this−>s i z e ; i 2 ++){
f or ( int i 3 =0; i 3 <this−>s i z e ; i 3 ++){
c o u t << this−>d a t o s [ i 1 ] [ i 2 ] [ i 3 ] ;
}
c o u t <<e n d l ;
}
c o u t <<e n d l ;
}
c o u t <<e n d l ;
}

int main ( ) {
HiperCubo<int> c ;
!c;
HiperCubo<int> d ( 2 ) ;
!d;
HiperCubo<int> e ( 3 ) ;
!e;
HiperCubo<char>f ( 4 , ’X ’ ) ;
!f;
}

Página 6 de 6

Anda mungkin juga menyukai