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.
Program ElectionResults;
uses sysutils;
{ The function maximumVote returns the winner }
function maximumVote(vote : array of integer; counts : integer): integer;
var
i, j, max : integer; //variable declaration
begin
max := 0; //initializes max to 0
for i := 0 to counts-1 do
begin
if (vote[i]>= max) then //comparision to determine the winner
begin
max := vote[i];
j := i;
end;
end;
maximumVote := j ; //returns last name of winner
end;
{ displays required outputs including the winner of election}
procedure getInput();
var // local variable declaration
str, winner: string;
num, sum, count : integer;
names : array[1..30] of string;
votes : array[1..30] of integer;
percentageOfVotes : array[1..30] of real;
begin
writeln(format('%-18s%-18s%-18s', ['Candidate', 'Votes Received', '% of Total Votes'])); //header display
readln(count); //reads each line and stores in count
sum := 0;
for num := 1 to count do
begin
readln(names[num]); //stores in the given num of name arrays after reading each line
readln(votes[num]); //stores in the given num of votes arrays after reading each line
sum := sum + votes[num]; //calculating the sum of votes
end;
for num := 1 to count do
begin
percentageOfVotes[num] := (votes[num]/sum * 100 ); //stores votes percent in array after its calculation
str := format('%-13s%13d%18.2f', [names[num], (votes[num]), (percentageOfVotes[num])]); //formatting strings for output
writeln(str);
end;
winner := names[maximumVote(votes, count) + 1];
writeln(format('%-13s%13d', ['Total', sum])); //displaying the total sum of number of votes
writeln('The winner of the election is ' + winner + '.'); //displaying the winner of election
end;
{ main program }
begin
getInput();
end.
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more