#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

    Linear Search In Pascal


    Hi,

    I'm new to programming and have just started learning Pascal. My assignment is to create Ballot Box/Voting System. So far I have setup the menus etc.

    My current problem is, I need to have a verification system whereby, each user has a unique pin (which is stored in an array), the system searches through the array to see if the pin exists and then moves them to another procedure. If the pin has already been entered or does not exist then a message appears and they have the choice to go back to the main menu.

    Here is the code I have so far which might be of some use.
    Code:
    program ballotbox;
    USES CRT;
    
    TYPE
    candidates=array[1..5,1..5] of string;
    pin=array[1..100] of string;
    
    VAR
    nocandidates:integer;
    novoters:integer;
    acandidates:candidates;
    apin:pin;
    
    procedure getcandidates;
    Begin
         CLRSCR;
         writeln('Set number of candidates');
         readln(nocandidates);
    End;
    
    procedure Voters;
    VAR
    icount:integer;
    
    Begin
         CLRSCR;
         writeln('Set number of voters');
         readln(novoters);
            For icount:= 1 to novoters do begin
                   writeln('Enter unique pin number for candidate ',icount);
                   readln(apin[icount]);
                   end;
    End;
    
    procedure namedescription;
    VAR
    icount:integer;
    
    Begin
              For icount:= 1 to nocandidates do begin
                 writeln('NAME OF CANDIDATE ',icount);
                 readln(acandidates[icount,1]);
                 writeln('DESCRIPTION:');
                 readln(acandidates[icount,2]);
              end;
    End;
    
    
    procedure zeroalltotals;
    Begin
    End;
    
    procedure methodwinner;
    Begin
    End;
    
     Procedure Setup;
     VAR
    choice: char;
    begin
    Repeat
    CLRSCR;
      writeln('1) Set number of voters');
    
      writeln('2) Set number of candidates');
    
      writeln('3) Enter a name and description for each candidate');
    
      writeln('4) Zero all totals');
    
      writeln('5) Select the method of choosing the winner');
    
      writeln('6) EXIT');
    
     readln (choice);
     case choice of
        '1':Voters;
        '2':getcandidates;
        '3':namedescription;
        '4':zeroalltotals;
        '5':methodwinner;
        '6':;
      end;
    Until Choice = '6';
    end;
    
    
    
    
    
    Procedure Voting;
    VAR
    Enterpin:integer;
     Begin
     CLRSCR;
     writeln('Enter unique pin: ');
     readln(enterpin);
     End;
    
     
     Procedure Results;
     Begin
     End;
    
    Procedure writemenu;
    VAR
    choice: char;
    begin
    Repeat
      CLRSCR;
      writeln('1) Setup');
    
      writeln('2) Voting');
    
      writeln('3) Results');
    
      writeln('4) Exit');
    
     readln (choice);
     case choice of
        '1':Setup;
        '2':Voting;
        '3':Results;
        '4':;
      end;
    Until Choice = '4';
    
    end;
    
    
    
    
    Begin
       Writemenu;
    end.
    Thanks for your help, it is much appreciated.

    Ed
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    928
    What exactly do you need help with? Be explicit.

    It seems to me like you have all the pieces: A linear search is just checking each element of an array to see if it matches. From your "Voters" procedure you know how to step through array indices and do something with each element, and at this point you must have covered if-statements.
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);

IMN logo majestic logo threadwatch logo seochat tools logo