Pascal/Turbo Pascal - programa recursivo..

 
Vista:

programa recursivo..

Publicado por Flor (6 intervenciones) el 07/07/2009 13:13:04
SOCORRO!!
TENGO UN PROGRAMA RECURSIVO QUE ME CALCULA LAS COMBINACIONES DE M ELEMENTOS TOMADOS DE N EN N...O AL MENOS ESO ES LO QUE QUIERO.
EL PROBLEMA ES EL SIGUIENTE:
TRABAJO CON UN ARRAY DE M CASILLAS
TENGO UN WHILE Y DENTRO UN FOR CON LA RECURSION. CUANDO TERMINA DE RECURRIR Y AVANZA EN EL WHILE ME UTILIZA EL ARRAY CAMBIADO AUNQUE YO UTILICE UNO AUXILIAR. No se si me explicado bien y soy bastante novata...

¿me echais una mano?
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:programa recursivo..

Publicado por Miguel (159 intervenciones) el 07/07/2009 19:12:58
A ver... no he entendido, hasta el momento le he dedicado 6 meses pura y exclusivamente a funciones recursivas y es lo unico recursivo que conozco, no te habras referido a ellas no?.
Pega el enunciado que te han dado, por favor. Y que te refieres a COMBINACIONES?. Me parece un lindo desafio este problema, pero por pavor el enunciado. Saludos.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:programa recursivo..

Publicado por flor (6 intervenciones) el 07/07/2009 22:47:36
vale, vale.... perdona mi torpeza, me piden un subprograma recursivo el enunciado es el siguiente:

dados dos enteros n,m>=1 diseña un subprograma que calcule las distintas combinaciones de los numeros 1...m tomados de n en n

lo tengo que entregar mañana!!!!!!!

S.O.S!!!!!!!!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:programa recursivo..

Publicado por Miguel (159 intervenciones) el 08/07/2009 02:19:55
Sigo perdido con el enunciado, que es una combinacion?.
la funcion tiene como parametro un arreglo de ?? supngamos que enteros, se le pasa un entero n y otro entero m, y que debe devolver.
Como definirias combinación?...
Me encantaria ayudarte pero si no puedo especificar el enunciado esta dificil.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:programa recursivo..

Publicado por FLOR (6 intervenciones) el 08/07/2009 02:32:30
POR EJEMPLO:
M=4 N=2
TENGO EL ARRAY [1,2,3,4]

LAS COMBINACIONES SIN REPETICION SERIAN:
12, 13, 14, 23, 24, 34

¿mE ENTIENDES AHORA?
MI PROGRAMA SOLO FALLA EN UNA COSA, ¿TE LO PUEDO MANDAR DE ALGUNA FORMA Y LE ECHAS UN VISTAZO?

GRACIAS!!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:programa recursivo..

Publicado por Miguel (159 intervenciones) el 08/07/2009 02:41:54
Dale, pegalo aqui.... ;) ...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:programa recursivo..

Publicado por flor (6 intervenciones) el 08/07/2009 02:51:08
PROGRAM COMBINACIONES(IMPUT,OUTPUT);
CONST M=4; N=2;
TYPE typeVector=ARRAY[1..M] OF INTEGER;
VAR VECTOR:typeVector; I,POS:INTEGER;

FUNCTION COMBINACION_VALIDA(COMB:TypeVECTOR;POS:INTEGER):BOOLEAN;
VAR RESULTADO:BOOLEAN; I:INTEGER;
BEGIN
RESULTADO:=TRUE;
I:=POS;
WHILE ((I>1) AND RESULTADO) DO
BEGIN IF (COMB[I-1]<=COMB[I])
THEN RESULTADO:=FALSE;
I:=I-1;
END;
COMBINACION_VALIDA:=RESULTADO;
END;

PROCEDURE IGUALAR_VECTORES(VECTOR1:typeVector; var vector2:typevector);
var i:INTEGER;
BEGIN FOR I:=1 TO M DO
VECTOR2[I]:=VECTOR1[I];
END;

PROCEDURE CORTAR_VECTOR(VAR VECTOR:typeVector;POS:INTEGER);
VAR I:INTEGER;
BEGIN IF (POS<M)
THEN FOR I:=POS TO M-1 DO
VECTOR[I]:=VECTOR[I+1];
END;

PROCEDURE MOSTRAR_VECTOR(VECTOR:TypeVECTOR;S,F:INTEGER);
VAR I:INTEGER;
BEGIN IF (S<=M) AND (F<=M)
THEN BEGIN FOR I:=S TO F DO
WRITE('',VECTOR[I],'');
WRITELN;
END;
END;

PROCEDURE COMBINAR(M,N:INTEGER;VECTOR:TypeVECTOR;VAR POS:INTEGER);
VAR I,J:INTEGER; VECTORAUX:typeVector;
BEGIN
IF(POS=M-N)
THEN MOSTRAR_VECTOR(vector,POS+1,POS+N)
ELSE BEGIN I:=POS+N;
MOSTRAR_VECTOR(VECTOR,POS+1,POS+N);
WHILE I>=1 DO
BEGIN IGUALAR_VECTORES(VECTOR,VECTORAUX);
CORTAR_VECTOR(VECTORAUX,POS+I);
COMBINAR(M-1,N,VECTORAUX,POS);
I:=I-1;
END;

END;
END;

BEGIN (*programa principal*)
FOR i:=1 to M do
vector[i]:=i;
POS:=0;
COMBINAR(M,N,VECTOR,POS);
WRITELN;
END.
¿que opinas?jejeje
mi problema es que cuando termina de recurrir y hace i-1 el vector con el que empieza es el ultimo antes de acabar la recursion y yo kiero que sea el primero de todos!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:programa recursivo..

Publicado por Willy (4 intervenciones) el 09/07/2009 01:54:16
Hoooooch!
Y es para mañana?

Yo estoy más perdido que Miguel en este asunto.
Talvez sea una buena idea que pegues el ejercicio anterior a este, lo que nos llevaría a comprendér qué es lo nuevo que están aprendiendo Flor.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:programa recursivo..

Publicado por flor (6 intervenciones) el 08/07/2009 03:15:19
¿entiendes algo? yo casi no se ni lo que hace y lo he hecho yo, asi que no me kiero imaginar lo que se te pasa por la cabeza...
si no lo soluciono para mañana lo tengo que entregar asi...
por cierto, la funcion combinacion valida no la uso para nada.

si sigues ahi contesta porfa que me voy a volver loca con tanta pantalla azul...jejeje

gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:programa recursivo..

Publicado por Miguel (159 intervenciones) el 08/07/2009 03:21:04
Pasame tu msn asi me orientas mejor jejeje... Esta dificil saber lo que hace, veo que repite uno al final, si no estaria bien...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:programa recursivo..

Publicado por Miguel (159 intervenciones) el 08/07/2009 03:25:42
Ahi te agregue.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:programa recursivo..

Publicado por Willy (4 intervenciones) el 09/07/2009 02:55:32
Hola. No sé, probablemente sea ya muy tarde para tratar de resolver el problema con este código.

Sin embargo yo veo que tú (Flor como que estás usando demasioados begin y end y eso está causdando una gran confusión)

Fíjate en esta línea que marqué:

El error dice: Type mismatch
¿Porqué? por que tienes demasiados begin y eso nos confunde, ok por cada begin tiene que haber un end, aún así el compilador no se confunde, pero si te das cuenta tienes un repeat y como a las tres líneas existe su "until", luego tienes de nuevo un repeat xxxxxxxxx until ioresult=0;
luego tienes un begin sin qué ni porqué, luego de ese begin pusiste más instrucciones que citan a otro begin
Pero el problema que vé el compilador creo que es este:

Tienes un Reapeat sin ser precedido de su "Until esto u lo otro", en cambio efs presedido por otro begin pero sin su end.

Mira me hiciste
"bolas" y estoy seguro de que el problema se vuelve complejo porque nos es sumamente difícil el controlar tantos begins y ends.

Seguro debe haber una forma de lograr que el programa haga lo que debe hacer sin tanta confución para nosotros.

No sé, espero las opiniones de Miguel. Al menos yo no tengo cabeza para comprender semejante complegidad de subprogramas dentro de otros jajaa.


program asignacion;
uses crt;
type
cedula = array[1..30] of longint;
nombre = array[1..10] of string[10];
edad = array[1..10] of integer;
promedio = array[1..10] of integer;
region = array[1..10] of integer;
acumulado=array[1..10] of integer;
total=array[1..10] of integer;
R1=array[1..10]of integer;
R2=array[1..10]of integer;
R3=array[1..10]of integer;
var
ced:cedula;
nom: nombre;
ed:edad;
ioresult,resp:integer;
i:integer;
prom:promedio;
Reg:Region;
tot:total;
acumR1:integer;
acumR2:integer;
acumR3:integer;
seccion1:integer;
seccion2:integer;
cursoA:integer;
cursoB:integer;
procedure registro_datos(ced1:cedula;no:nombre;ed1:edad;pro:promedio;re:region);
var
cedl:longint;
nombr:string;
ed:integer;
promed:integer;
reg:integer;
R1,R2,R3:integer;**********************************************************************
total:integer;
begin
for i:= 1 to 10 do
begin
clrscr;
cedl:=0;
while (cedl<1)do
begin
REPEAT
gotoxy(10,07);write('CEDULA ');
readln(cedl);
until ioresult=0;
end;
gotoxy(10,08); write('NOMBRE Y APELLIDO: ');
readln(nombr);
gotoxy(10,09); write('EDAD: ');
readln(ed);
clrscr;
gotoxy(10,10); write(' REGION: ');
readln(Reg);
Begin
if ((Reg=1) or (Reg=2) or (Reg=3)) then

begin
clrscr;
repeat
if((R1=0) or (R2=0) or (R3=0)) then
begin
(R1):=R1+1;
total:=(acumR1*R1/100)
until R1>=10
end;
end;
end;
clrscr;
begin
repeat
(R2):=R2+1;
total:=(acumR2*R2/100)
until R2>=10
end;
end;
clrscr;
begin
repeat
(R3):=R3+1;
total:=(acumR3*R3/100)
until R3>=10

end;
end;
(Me aparece el error "26" en la linea de comparar las Reg='1' or Reg'=2' or Reg='3'
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

RE:programa recursivo..

Publicado por Miguel (2 intervenciones) el 09/07/2009 03:07:43
Yo jamas entendi que hacia el codigo... La verdad que no esta facil darse cuenta cual es el error!!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar