Pascal/Turbo Pascal - Se queda en un if

 
Vista:

Se queda en un if

Publicado por Javier (2 intervenciones) el 16/12/2015 22:28:48
Hola que tal, tengo un problema con este procedimiento. Lo que hace es cargar en un vector los numeros que dividen a un numero ingresado y son primos...
Para mi parecer es correcto pero al hacer trace into se queda en un if. Ese if es el de la linea que dice:
if(numingre mod i = 0) then
¿ Que es lo que estoy haciendo mal? Probe hacerlo en otro lado y funciona correctamente...
Ahi pego el codigo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
procedure divpr(numingre: longword ; v: vector);
        var
        i: longword;
        np: word;
        stop: char;
        begin
        np:= 0;
        stop:= 'a';
        for i:=((numingre div 2)+1) downto 1 do
        begin
                while(stop = 'a') do
                begin
                        if( numingre mod i = 0) then
                        begin
                                if(esprimo(i)= true) then
                                begin
                                        v[np]:= i;
                                        inc(np);
                                        if(np>19)then
                                        begin
                                                writeln('Error ha alcanzado el vector ha alcanzado el limite de divisores primos');
                                                stop:='b';
                                        end;
                                end;
                        end;
                end;
        end;
        while(np<20) do
                begin
                v[np]:= 0;
                inc(np);
                end;
        mostrarvector(v,np);
        end;

Desde ya muchas gracias ;)
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

Se queda en un if

Publicado por ramon (2158 intervenciones) el 18/12/2015 12:24:00
Me faltan datos para poder decirte con exactitud el problema pero el programa se queda colgado en este párrafo y no puede
segur incrementando np

1
2
3
4
5
6
7
8
9
10
11
12
13
if( numingre mod i = 0) then
	begin
		if(esprimo(i)= true) then
		begin
			v[np]:= i;
			inc(np);
			if(np>19)then
			begin
				writeln('Error ha alcanzado el vector ha alcanzado el limite de divisores primos');
				stop:='b';
			end;
		end;
	end;
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

Se queda en un if

Publicado por Javier (2 intervenciones) el 21/12/2015 11:46:27
Lo que pasa que cuando hago el trace into llega hasta el primer if de la parte del codigo que me mandaste y se queda haciendo ciclo ahi.
Ahi te mando la función esprimo. Ambas son parte de una unit.

1
2
3
4
5
6
7
8
9
10
11
function  esprimo(numero: word): boolean;
        var
        i: word;
        begin
        esprimo:= true;
        for i:=2 to trunc(sqrt(numero)) do
                if(numero mod i = 0) then
                esprimo:=false;
        if(numero=1) then esprimo:=true;
        if(numero=2) then esprimo:=true;
        end;
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

Se queda en un if

Publicado por ramon (2158 intervenciones) el 29/12/2015 14:07:17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{Mira si esto  te sirve}
 
 uses
     crt;
 
   type
      longword = longint;
      vector = array[1..19] of integer;
 
  var
    vec : vector;
 
 
 
 
  function  esprimo(numero : word) : boolean;
  var
    i : word;
    begin
       esprimo := true;
       for i := 2 to trunc(sqrt(numero)) do
        if(numero mod i = 0) then
        esprimo := false;
        if(numero = 1) then
        esprimo:=true;
        if(numero = 2) then
         esprimo := true;
      end;
 
  procedure mostrarvector(vv : vector; nn : word; te : real);
  var
    cc : integer;
    begin
       write('  Los Divisores Primos Son ');
       for cc := 1 to nn do
       write('  ',vv[cc]);
       write('  Del Num. ',trunc(te));
    end;
 
   procedure divpr(numingre : longword ; v : vector);
  var
    t, i : longword;
    np : word;
    stop : char;
    begin
       np := 0;
       stop := 'a';
        t := (numingre div 2) + 1;
 
        for i := t downto 1 do
        begin
         if numingre mod i = 0 then
            begin
              if esprimo(i) = true then
              begin
                 np := np + 1;
                 if np > 19 then
                 begin
                 writeln('   Error ha alcanzado el vector ha alcanzado el',
                            'limite de divisores primos');
                 break;
                 end;
                 v[np] := i;
              end;
            end;
          end;
              if np > 0 then
              mostrarvector(v,np,34);
            end;
 
 
  {procedure divpr(numingre : longword ; v : vector);
  var
    i : longword;
    np : word;
    stop : char;
    begin
       np := 0;
       stop := 'a';
        for i := (numingre div 2) + 1 downto 1 do
        begin
          while stop = 'a' do
          begin
            if numingre mod i = 0 then
            begin
              if esprimo(i) = true then
              begin
                 v[np] := i;
              end;
            end;
             inc(np);
             if np > 19 then
             begin
             writeln('   Error ha alcanzado el vector ha alcanzado el',
                            'limite de divisores primos');
             stop := 'b';
            end;
          end;
           while (np < 20) do
           begin
           inc(np);
           v[np] := 0;
          end;
        end;
        if stop <> 'b' then
        mostrarvector(v,np,4);
        end;}
 
 
 
  begin
     clrscr;
     divpr(204,vec);
     readkey;
  end.
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