|
573
|
Programación / Programación C/C++ / Re: RECURSION en su estado mas puro para invertir un vector!
|
en: 16 Septiembre 2018, 04:32 am
|
#include <iostream> #include <ctime> using namespace std; const int FILAS=3, COLUMNAS=4; typedef int arreglo[FILAS][COLUMNAS]; void cargar(arreglo &a, int i, int j); void mostrar(arreglo a, int i, int j); int suma(arreglo a, int i, int j); int main() { arreglo miarreglo; srand(time(0)); cargar(miarreglo,0,0); mostrar(miarreglo,0,0); cout << "SUMA: " << suma(miarreglo,FILAS-1,COLUMNAS-1) << endl; system("pause"); } void cargar(arreglo &a, int i, int j) { a[i][j]=1+rand()% 10; if(i<FILAS){ if(j<COLUMNAS){ cargar(a,i,j+1); } else{ cargar(a,i+1,0); } } } void mostrar(arreglo a, int i, int j) { if(i<FILAS){ if(j<COLUMNAS){ cout << a[i][j] << " "; mostrar(a,i,j+1); } else{ cout << endl; mostrar(a,i+1,0); } } } int suma(arreglo a, int i, int j) { if(i==0 && j==0) return a[i][j]; else if(i>-1) if(j>=-1) return a[i][j]+suma(a,i,j-1); else return a[i][j]+suma(a,i-1,j+COLUMNAS-1); }
|
|
|
574
|
Programación / Programación C/C++ / Re: RECURSION en su estado mas puro para invertir un vector!
|
en: 16 Septiembre 2018, 00:56 am
|
COLUMNAS es solo representativo #include <iostream> using namespace std; const int FILAS=3, COLUMNAS=4; typedef int arreglo[FILAS][COLUMNAS]; int suma(arreglo a, int i, int j); int main() { arreglo miarreglo={{1,1,1,1},{2,2,2,2},{3,3,3,3}}; cout << "SUMA: " << suma(miarreglo,FILAS-1,COLUMNAS-1) << endl; system("pause"); } int suma(arreglo a, int i, int j) { if(i==0 && j==0) return a[i][j]; else if(i>-1) if(j>=-1) return a[i][j]+suma(a,i,j-1); else return a[i][j]+suma(a,i-1,j+COLUMNAS-1); }
|
|
|
576
|
Programación / Programación C/C++ / Lista simple eliminar nodo segun un valor
|
en: 15 Septiembre 2018, 06:13 am
|
Buenas no entiendo el comportamiento de este procedimiento y mas precisamente en el ciclo for typedef struct tnodo *pnodo;//Estructura de la lista typedef struct tnodo{ int dato; pnodo sig; }; pnodo quitar_nodo(pnodo &lista, int valor)//Metodo quitar nodo { pnodo borrado, i; if(lista==NULL){ borrado=NULL; } else{ if(lista->dato==valor){ borrado=lista; lista=borrado->sig; borrado->sig=NULL; } else{ for(i=lista;i->sig!=NULL && valor!=(i->sig)->dato;i=i->sig);//De aqui en adelante no lo tengo bien en claro, se como lo hace pero el codigo me es incomprensible if(i->sig!=NULL){ borrado=i->sig; i->sig=borrado->sig; borrado->sig=NULL; } else{ borrado=NULL; } } } return borrado; }
Alguien que me lo explique en cristiano por favor?
|
|
|
577
|
Programación / Programación C/C++ / Re: invertir un vector en C
|
en: 15 Septiembre 2018, 00:40 am
|
Hola para invertir un vector puedes usar la recursividad void invertir(arreglo a, int n) { if(n>=0) cout << a[n] << " "; invertir(a,n-1); }
O de la forma iterativa void invertir(arreglo a) { for(int i=MAX-1;i>=0;i--) cout << a[i] << " "; cout << endl; }
|
|
|
578
|
Programación / Programación C/C++ / Re: RECURSION en su estado mas puro para invertir un vector!
|
en: 14 Septiembre 2018, 05:19 am
|
int suma(arreglo a, int i, int j) { if(i==0 && j==0) return a[i][j]; else if(i>-1) if(j>=-1) return a[i][j]+suma(a,i,j-1); else return a[i][j]+suma(a,i-1,j+COLUMNAS-1); }
void mostrar(arreglo a, int n) { if(n>=0) cout << a[n] << " "; mostrar(a,n-1); }
void como_la_realidad(short vida) { if(vida<=1){ cout << "Entrando en el sueño"; } else{ como_la_realidad(vida-1); cout << " del sueño"; } }
Mod: No hacer triple post. Usa el botón "Modificar".
|
|
|
579
|
Programación / Programación C/C++ / Re: Pequeña duda sobre palabra reservada new y espacios en memoria
|
en: 13 Septiembre 2018, 22:42 pm
|
Si tengo esta estructura cuantos bytes seria por cada nodo? const int MAX=32; typedef char tcad[MAX]; typedef struct tfecha{ int dia; int mes; int año; }; typedef struct tdomicilio{ tcad calle; int numero; tcad barrio; }; typedef struct tvehiculo{ tcad marca; tcad patente; }; typedef struct thorario{ tcad horaEntrada; tcad horaSalida; }; typedef struct tchofer{ int legajo; tcad nombre; tcad apellido; int DNI; tfecha fecha_nac; tdomicilio domicilio; tvehiculo coche; thorario extras; int horas_extras; }; typedef struct tnodo *pnodo; typedef struct tnodo{ tchofer chofer; pnodo sig; };
|
|
|
580
|
Programación / Programación C/C++ / Una pregunta acerca de mostrar listas circulares
|
en: 13 Septiembre 2018, 06:26 am
|
Hola a todos, si bien esta es una de las formas mas facil de mostrar por pantalla los datos de una lista circular, pero yo queria saber si esto se puede realizar con un ciclo for porque la verdad no lo se Aqui abajo les dejo el procedimiento de mostrar_lista(); void mostrar(tlista lista) { pnodo i=lista.inicio; if(lista.inicio!=NULL){ do{ cout << "Nodo: " << i->dato << endl; i=i->sig; }while(i!=lista.inicio); } else{ cout << "LISTA VACIA" << endl; } }
|
|
|
|
|
|
|