Board index » delphi » problem with arrays

problem with arrays

this is the source code for the section of the program giving me problems...

{--------------------------------}
procedure factor2 (fact1, fact2 : real; var endfact :factortype);
var
   f1,
   f2 : synthtype;
   sum :real;
   compare,
   s,
   l,
   l2 :integer;
begin
     l := -100;
     for l := -200 to 200 do
      begin
       f1[l] := -998;
       f2[l] := 0;
      end;
     compare := 0;
     for l := -200 to -1 do
       begin
         sum := l;
         if fact1/sum = int(fact1/sum)
            then if fact1/sum >= compare
                 then f1[l] := l;
         if fact2/sum = int(fact2/sum)
            then if fact2/sum >= compare
                 then f2[l] := l;
       end;
     for l := 1 to 200 do
       begin
         sum := l;
         if fact1/sum = int(fact1/sum)
            then if fact1/sum >= compare
                 then f1[l] := l;
         if fact2/sum = int(fact2/sum)
            then if fact2/sum >= compare
                 then f2[l] := l;
       end;
     s := 1;
     for l := 0 to 200 do
         endfact[l] := -999;
     for l := -200 to 200 do
      for l2 := -200 to 200 do
         begin
          if f2[l] <> 0
            then if f1[l2] <> -998
              then begin
                    endfact[s] := f1[l2]/f2[l];
                    s := s + 1;
                  end;
          end;
end;

{--------------------------------}
procedure evaluate (low, high : integer; num :real; poly :synthtype; var ans
:real);
var
   sub2,
   sum  :real;
   loop :integer;
begin
     ans := 0;
     for loop := low to high do
         begin
            sub2 := exp2(num,loop);
            sum := poly[loop] * sub2;
            ans := ans+sum;
         end;
end;
{--------------------------------}
procedure polyeval (num : real; var ans :real; poly :synthtype;low, high
:integer);
var
   loop : integer;
begin
     ans := 0;
     Write ('Enter the highest exponent in the polynomial: ');
     readln (high);
     write ('Enter the lowest exponent in the polynomial: ');
     readln (low);
     for loop := high downto low do
         begin
              write ('Enter the coefficient of X^',loop,': ');
              readln (poly[loop]);
         end;
     evaluate (low,high,num,poly,ans);
end;
{--------------------------------}
procedure synth2 (low,high :integer; num :real; var poly :synthtype);
var
   row2,
   endcoef : synthtype;
   loop :integer;
begin
     for loop := -200 to 200 do
         endcoef[loop] := 0;
     row2[high] := 0;
     for loop := high downto low do
      begin
           endcoef[loop-1] := poly[loop]+row2[loop];
           row2[loop-1] := num*endcoef[loop-1]
      end;
      for loop := -200 to 200 do
          poly[loop] := endcoef[loop];
      if low = 0
         then high := high-1
         else begin
                   high := high-1;
                   low := low-1;
              end;
end;
{--------------------------------}
procedure endzeroes (root :factortype);
var
   loop :integer;
begin
     writeln ('The Rational roots of this function are: ');
     for loop := 0 to 200 do
         if root[loop] <> -999
            then writeln (root[loop]:1:5);
end;
{--------------------------------}
procedure zeroes2 (var poly :synthtype; count,low,high,loop :integer;endfact
:factortype;ans,num :real;root :factortype);
begin
     for loop := 0 to 200 do
       begin
         if endfact[loop] <> -999
            then evaluate (low,high,(-endfact[loop]),poly,ans);
         if ans = 0
            then begin
                  num := endfact[loop];
                  root[count] := endfact[loop];
                  count := count + 1;
                  synth2 (low,high,-num,poly);
                 end;
       end;
end;
{--------------------------------}
Procedure Zeroes (poly :synthtype; count,high,low :integer;root
:factortype);
var
   loop :integer;
   fact1,
   fact2,
   num,
   ans :real;
   endfact :factortype;
   f,
   flag :char;
begin
     f := 'n';
     while f = 'n'do
      begin
        flag := 'n';
        fact1 := poly[0];
        fact2 := poly[high];
        factor2 (fact1,fact2,endfact);
        for loop := -200 to -1 do
           if poly[loop] <> 0
              then flag := 'Y';
        for loop := 1 to 200 do
           if poly[loop] <> 0
              then flag := 'Y';
        if flag = 'Y'
          then zeroes2 (poly,count,low,high,loop,endfact,ans,num,root)
          else begin
                endzeroes(root);
                f := 'y';
               end;
     end;
end;
{--------------------------------}
procedure getcoef;
var
   count,
   loop,
   high,
   low :integer;
   poly :synthtype;
   root :factortype;
begin
     for loop := -200 to 200 do
         poly[loop] := 0;
     write ('Enter the highest degree in the polynomial: ');
     readln (high);
     write ('Enter the lowest degree in the polynomial: ');
     readln (low);
     for loop := high downto low do
         begin
              Write ('Enter the coefficient of X^',loop,': ');
              readln (poly[loop]);
         end;
     count := 0;
     for loop := 0 to 200 do
           root[loop] := -999;
     Zeroes (poly,count,high,low,root);
end;
{--------------------------------}

for my tpyical test values, starting with the procedure getcoef, high = 2,
low = 0, poly[0] = 6, poly[1] = 5, and poly[2] = 1
it goes through the procedure zeroes fine, but when it comes back to it from
zeroes2, the values are reset to -999. Then, when it comes back from factor2
the second time, thats when the array root gets filled with ludicrous
values. Even if somewher i accidently passed as a value parameter, why would
the values get corrupted? Thank you for your help.

 

Re:problem with arrays


Quote
ted sussman <e...@javanet.com> wrote:
> this is the source code for the section of the program giving me problems...
> {--------------------------------}
> procedure zeroes2 (var poly :synthtype; count,low,high,loop :integer;endfact
> :factortype;ans,num :real;root :factortype);

<root> should be a var parameter. This is causing you to lose the returned
values...

Quote
> it goes through the procedure zeroes fine, but when it comes back to it from
> zeroes2, the values are reset to -999. Then, when it comes back from factor2
> the second time, thats when the array root gets filled with ludicrous
> values. Even if somewher i accidently passed as a value parameter, why would
> the values get corrupted? Thank you for your help.

In zero2 and synth2, you should make <high> and <low> variable parameters.
They're being altered as you reduce the degree of the polynomial in synth2,
but this change is not passed back to zeroes2 and zeroes, so on the second
pass it tries to deal with a second-order polynomial that doesn't exist.

The lesson is: if your procedure changes a parameter, it should (in general)
be a var parameter.

Also, in zeroes2, you have loop as a parameter. This is silly; make it a
local variable. The initial value isn't used, and the final value is
undefined, so you're not returning anything useful (and it's not used by the
calling function either, of course).

There are other improvements that could be made -- quite a few, actually --
but this should at least get you going.

--
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will
   sa...@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
______________________________|_______________________________________

Other Threads