Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Help with a program pls

pascal

  • Please log in to reply
10 replies to this topic

#1 DaJackal

DaJackal

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 01 December 2008 - 04:08 AM

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:

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!
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 01 December 2008 - 08:51 AM

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.
  • 0

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

My MineCraft server site: http://banishedwings.enjin.com/


#3 DaJackal

DaJackal

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 01 December 2008 - 11:29 AM

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
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 01 December 2008 - 02:17 PM

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.
  • 0

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

My MineCraft server site: http://banishedwings.enjin.com/


#5 DaJackal

DaJackal

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 02 December 2008 - 02:14 AM

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!
  • 0

#6 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 02 December 2008 - 05:43 AM

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

  • 0

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

My MineCraft server site: http://banishedwings.enjin.com/


#7 DaJackal

DaJackal

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 03 December 2008 - 01:34 AM

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. :((
  • 0

#8 jakefrog

jakefrog

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 03 December 2008 - 06:42 AM

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
  • 0

#9 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 03 December 2008 - 08:16 AM

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.
  • 0

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

My MineCraft server site: http://banishedwings.enjin.com/


#10 DaJackal

DaJackal

    CC Newcomer

  • Just Joined
  • PipPip
  • 14 posts

Posted 07 December 2008 - 02:30 AM

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.
  • 0

#11 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 07 December 2008 - 04:48 AM

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.
  • 0

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

My MineCraft server site: http://banishedwings.enjin.com/






Also tagged with one or more of these keywords: pascal

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download