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.
(*Name: Yogesh Byanjankar
Course: CSCI 3010 Spring 2019
Summary: The purpose of this program is to read STDIN containing the number of candidates and the number of votes that each received and find the winner with highest number of votes.
Date: 27 March 2019*)
Program ElectionResults;
uses sysutils;
type
arrayOfElection = array[1..100] of integer;
arrayOfName = array[1..100] of String;
arrayOfPercentage = array[1..100] of real;
var
votes1 : arrayOfElection;
percentage1 : arrayOfPercentage;
name1 : arrayOfName;
(*this function returns the total number of votes*)
function totals(voting : array of integer; counts : integer): integer;
(*local vairables*)
var
i, total1 : integer;
begin
total1 := 0;
for i := 0 to counts-1 do //loop starts from 0
begin
total1 := total1 + voting[i];
end;
totals := total1 ; //returns the total number of votes
end;
(*this function returns the winnerr of the election*)
function print(voting : array of integer; counts : integer): integer;
(*local vairables*)
var
maximum, i,indexOfmaximum : integer;
begin
maximum := 0; //initialize maximum to 0
for i := 0 to counts-1 do //loop starts from 0
begin
(*compares all the votes to find maximum*)
if (voting[i]>= maximum) then
begin
maximum := voting[i]; //the maximum votes
indexOfmaximum := i; //finds the index of with maximum votes
end;
end;
print := indexOfmaximum ; //returns the Last name of the winner
end;
(*prints the last name, votes received ,percent of total votes, and the winner of election in the election*)
procedure runElection(var votes : arrayOfElection; var names : arrayOfName; var percent : arrayOfPercentage);
(*declare local variables*)
var
j, count, total : integer;
str, winner: string;
begin
writeln(format('%-18s%-18s%-18s', ['Candidate', 'Votes Received','% of Total Votes'])); //header display
readln(count); //stores the total number of lines
for j := 1 to count do //loop from 1 to count
begin
readln(names[j]); //read next line and stores in the names array in current index
readln(votes[j]); //read next line and stores in the votes array in the current index
end;
total := totals(votes, count);
(*loop starts*)
for j := 1 to count do
begin
percent[j] := ((votes[j]/total) * 100 ); //calculates percent vote and stores in the percent array
str := format('%-13s%13d%18.2f', [names[j], (votes[j]), (percent[j])]); //formats string
writeln(str); //display the last name, votes, and the percentage of vote
end;
winner := names[print(votes, count) + 1];
writeln(format('%-13s%13d', ['Total', total])); //displays the total number of votes in given format
writeln('The winner of the election is ' + winner + '.'); //displays the winner of the election
end;
begin {main program}
runElection(votes1, name1, percentage1);
end.
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more