Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

ElectionResults

{*
Name: Aswin Timalsina
CWID: 30098693

Program Description: The program reads the file STDIN and displays the name, votes received and percent of the votes. The program also calculates the total number of votes and finds the winner of the election. The program make good use of subprograms procedure and function, local variables and arrays.
*}


Program ElectionResults;      {name of the program}

  Uses sysutils;              {imports sysutils library}
  
procedure headline();         {procedure that displays the headline}
begin
    writeln('Candidate        Votes Received        % of Total Votes');
end;

function readFile() : string ;{function that displays the table and calculates the winner}
type                          {arrays declared}
names = array of string;  
votes = array of integer;
    
var                           {variables declared}
name1 : names;
vote1 : votes;
number, i, l: integer;
highest : integer = 0;
vote_sum : integer = 0;
candidate : string = '';



begin
    readln(number);           {stores the number of voters to 'number'}
    setlength(name1, number); {length of the array set}
    setlength(vote1, number);
    
    for i:= 1 to number do    {loop to store in array and storing the sum of vote}
    begin
        readln(name1[i]);
        readln(vote1[i]);
        vote_sum := vote_sum + vote1[i];
        
        if(vote1[i] > highest) then  {finds the winner}
        begin
            highest := vote1[i];
            candidate := name1[i];
        end;
        
    end;
    
    for l:=1 to number do
    begin
            writeln(Format('%0:-20s %-21d %5.2f',[name1[l], vote1[l], ((vote1[l]/vote_sum)*100)])); {prints the table of name, votes and percentage}
    
    end;
    writeln('Total               ', vote_sum);  {prints the total votes}
    
  readFile := candidate + '.';
  
end;

begin
  
  headline();                                                         {headline procedure called}
  
  writeln(Format('The winner of the election is %s', [readFile()]));  {prints readFile function and prints the winner of the election}
  
end.                  {end of program}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.