Jump to content

Help with a program pls

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
DaJackal

DaJackal

    Newbie

  • Members
  • PipPip
  • 14 posts
Hi!
I made a program in pascal for school, and there is a mistake when i'm running it. I can't figure out what and where is the mistake. Please help me in finding it out. Here's the problem:

Quote

More sequences of natural numbers are read. Reading a sequence ends when reading “0” number, and reading all sequences ends with reading the empty sequence. For each sequence print the longest subsequence of consecutive related numbers. Two numbers are related if their writings in basis 10 have at least two distinct digits. E.g., 13678 and 8759 are related, and 123 and 65482 are not). Print all the numbers found in all sequences, without printing a number twice (determine whether two numbers are related, determine the longest sequence, add a number to a given sequence if not already there).

And here is my attempt in resolving this problem. I don't know where is the mistake:


type vector=array[1..30] of string;

var x,z:vector;

    n,m,i,max,pmax,ok:integer;


procedure reading(var x:vector; var n:integer);

var i,j:integer;

    s:string[10];

begin

     writeln('Give the sequence: ');

     read(s);

     i:=1;

     repeat

     for j:=1 to length(s) do

         if s[j]<>'0' then

            x[i]:=x[i]+s[j]

         else

            i:=i+1;

      until x[i-1]='';

     n:=i-2

end;


function maxim(a,b:string):string;

begin

     if length(a)>length(b) then

        maxim:=a

     else

        maxim:=b

end;


function related(a,b:string):boolean;

var v:array[1..9] of integer;

    i,j,c:integer;

    aux:string;

begin

     c:=0;

     for i:=1 to 9 do

         v[i]:=0;

     related:=false;

     if maxim(a,b)=b then

        begin

             aux:=a;

             a:=b;

             b:=aux

        end;

     for i:=1 to length(a) do

         begin

              if v[ord(a[i])-48]<>1 then

                 begin

                      v[ord(a[i])-48]:=1;

                      for j:=1 to length(b) do

                          if a[i]=b[j] then

                             begin

                                  c:=c+1;

                                  break

                             end;

                      if c=2 then

                         begin

                              related:=true;

                              break

                         end

                 end

         end

end;


procedure sequence(x:vector; n:integer; var max,pmax:integer);

var i,j,c,nr1,nr2:integer;

begin

     max:=1;

     pmax:=1;

     j:=1;

     for i:=1 to  n-1 do

         if related(x[i],x[i+1]) then

            begin

                 val(x[i],nr1,c);

                 val(x[i+1],nr2,c);

                 if nr2=nr1+1 then

                    j:=j+1

                 else

                    if j>max then

                       begin

                            max:=j;

                            pmax:=i-max+1;

                            j:=1

                       end;

            end

          else

              if j>max then

                 begin

                      max:=j;

                      pmax:=i-max+1;

                      j:=1;

                 end

end;


procedure add(a:string; var z:vector; var m:integer);

var i:integer;

begin

     for i:=1 to m do

         if a=z[i] then

            break

          else

            begin

                 z[m]:=a;

                 m:=m+1;

            end;

end;


procedure printing(x:vector; pmax,max:integer);

var i:integer;

begin

     writeln('Longest sequence : ');

     for i:=pmax to pmax+max-1 do

         begin

              write(x[i],' ');

              add(x[i],z,m)

         end;

     readln

end;



procedure final(z:vector;m:integer);

var i:integer;

begin

     writeln('All the sequences: ');

     for i:=1 to m-1 do

         write(z[i],' ');

end;


function execution:integer;

var nr:integer;

begin

     writeln('1.Give a sequence: ');

     writeln('2.Exit');

     read(nr);

     execution:=nr;

end;



begin

     m:=1;

     repeat

           ok:=execution;

           if ok=1 then

              begin

                   reading(x,n);

                   sequence(x,n,max,pmax);

                   printing(x,pmax,max);

              end

           else

              exit

     until ok=2;

     readln;

end.


Thanks you very much!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Perhaps you could tell us what the input and output is. I noticed that you are working with arrays of strings, when your input is supposed to be integers. This could cause some very odd behavior in your output.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
DaJackal

DaJackal

    Newbie

  • Members
  • PipPip
  • 14 posts
Hi WingedPanther!
I'm not a good english speaker, so i'm not sure that i understand you. Could you show me what i didn't make well with concrete exemple? Couldn't you correct my mistake in the source code? Thanks

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
What did you type in?
What did the program give you?
Why is vector an array of string?

I'm asking so I know for sure what the problem is.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
DaJackal

DaJackal

    Newbie

  • Members
  • PipPip
  • 14 posts
I can't write any sequence of numbers, because when it appears to me the 2 options:" 1. Give a sequence 2. Exit", if a press 1 to enter a sequence, it appears to me again these 2 options, if I press 1 again to enter the sequence, it appear to me again these 2 options, and so it goes. If I press 2, it's no problem, it's exit there. Thanks!

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Try this:
function execution:integer;
var nr:integer;
begin
     writeln('1.Give a sequence: ');
     writeln('2.Exit');
     read(nr);
     result:=nr;
end;

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
DaJackal

DaJackal

    Newbie

  • Members
  • PipPip
  • 14 posts
It doesn't work with that result. Now, when i press a number and enter, after that it's pulling me out from the program. :((

#8
jakefrog

jakefrog

    Newbie

  • Members
  • PipPip
  • 11 posts
maybe you can try making a manual debug... like... put some codes to show what is it reading or something like...



writeln('debug 1');
writeln('1.Give a sequence: ');
writeln('debug 2');
writeln('2.Exit');

writeln('debug 3 - b4 read');
read(nr);
writeln('debug 4 - after read');

by this you can know where it is stoppnig

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I would simply step through your code in the debugger and see what is happening. Perhaps variables are not getting set the way you think they are.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
DaJackal

DaJackal

    Newbie

  • Members
  • PipPip
  • 14 posts
I have tried your suggestions, but i can't figure out what the problem is. So, if you please, try to put these code lines in your compiler, and see if you can make it work. Thank you very much for your patience.

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Dajackal: I don't have a pascal compiler on my system at the moment, and it will take me a while to get one on. Here is one way to get useful information, however:
type vector=array[1..30] of string;
var x,z:vector;
    n,m,i,max,pmax,ok:integer;

procedure reading(var x:vector; var n:integer);
var i,j:integer;
    s:string[10];
begin
     writeln('Give the sequence: ');
     read(s);
     //add code to output s for verification here... SUBSCRIPT STARTS AT 0!!!
     for j:=0 to length(s)-1 do
       writeln(s[j]);
     //end debug output
     i:=1;
     repeat
     for j:=1 to length(s) do
         if s[j]<>'0' then
            x[i]:=x[i]+s[j]//odd, x contains strings, but you only copy 1 character???
         else
            i:=i+1;
      until x[i-1]='';
     n:=i-2
end;

function maxim(a,b:string):string;
begin
     if length(a)>length(b) then
        maxim:=a
     else
        maxim:=b
end;

function related(a,b:string):boolean;
var v:array[1..9] of integer;
    i,j,c:integer;
    aux:string;
begin
     c:=0;
     for i:=1 to 9 do
         v[i]:=0;
     related:=false;
     if maxim(a,b)=b then
        begin
             aux:=a;
             a:=b;
             b:=aux
        end;
     for i:=1 to length(a) do
         begin
              if v[ord(a[i])-48]<>1 then
                 begin
                      v[ord(a[i])-48]:=1;
                      for j:=1 to length(b) do
                          if a[i]=b[j] then
                             begin
                                  c:=c+1;
                                  break
                             end;
                      if c=2 then
                         begin
                              related:=true;
                              break
                         end
                 end
         end
end;

procedure sequence(x:vector; n:integer; var max,pmax:integer);
var i,j,c,nr1,nr2:integer;
begin
     max:=1;
     pmax:=1;
     j:=1;
     for i:=1 to  n-1 do
         if related(x[i],x[i+1]) then
            begin
                 val(x[i],nr1,c);
                 val(x[i+1],nr2,c);
                 if nr2=nr1+1 then
                    j:=j+1
                 else
                    if j>max then
                       begin
                            max:=j;
                            pmax:=i-max+1;
                            j:=1
                       end;
            end
          else
              if j>max then
                 begin
                      max:=j;
                      pmax:=i-max+1;
                      j:=1;
                 end
end;

procedure add(a:string; var z:vector; var m:integer);
var i:integer;
begin
     for i:=1 to m do
         if a=z[i] then
            break
          else
            begin
                 z[m]:=a;
                 m:=m+1;
            end;
end;

procedure printing(x:vector; pmax,max:integer);
var i:integer;
begin
     writeln('Longest sequence : ');
     for i:=pmax to pmax+max-1 do
         begin
              write(x[i],' ');
              add(x[i],z,m)
         end;
     readln
end;


procedure final(z:vector;m:integer);
var i:integer;
begin
     writeln('All the sequences: ');
     for i:=1 to m-1 do
         write(z[i],' ');
end;

function execution:integer;
var nr:integer;
begin
     writeln('1.Give a sequence: ');
     writeln('2.Exit');
     read(nr);
     writeln(nr);
     execution:=nr;
end;


begin
     m:=1;
     repeat
           ok:=execution;
           writeln(ok);
           if ok=1 then
              begin
                   reading(x,n);
                   sequence(x,n,max,pmax);
                   printing(x,pmax,max);
              end
           else
              exit
     until ok=2;
     readln;
end.

I've also added some comments about some odd bits of code you have that don't make sense.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog