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.
{
This program takes a number of candidates and the votes they received
in a election, and displays their data. Also, it finds out the winner
of the election.
Input: Candidate's name and the votes they got
Output: Formatted data, total votes, and the winner's name
Author: Dipiksha Shrestha
}
Program ElectionResults(output);
Uses sysutils;
// to use the format feature
var
count: integer = 0;
i: integer;
candidates: array[1..10] of string;
votes: array[1..10] of integer;
percent_array: array[1..10] of double;
total_votes: integer = 0;
max: double=0;
indexOfWinner: integer= 0;
procedure read_data();
{
This procedure reads the data from the STDIN and saves
them into their respective data-structures
}
var can_name: string; // name of the candidate
vote: integer; // The vote they received
begin
for i := 1 to count do
begin
readln(can_name); {saves the candidates name into the variable}
readln(vote); {saves the candidates votes into the variable}
candidates[i] := can_name; {variable is stored in an candidates array}
votes[i] := vote; {variable is stored in an votes array}
// the total votes is calculated
total_votes := total_votes+vote;
end;
end;
procedure find_winner();
{
This procedures finds the candidate who has the maximum
votings
}
begin
for i := 1 to count do
begin
// calculates the percentage of the votes they received
percent_array[i] := (votes[i]/total_votes)*100;
if((votes[i]/total_votes)*100>max) then
// saves it to the max variable if it is the highest
max := (votes[i]/total_votes)*100;
end;
end;
procedure winner_name();
{
Finds the name of the winning candidate based on the index of
the winning percentage
}
begin
for i := 1 to count do
begin
if(percent_array[i]=max) then
indexOfWinner:=i;
end;
end;
procedure write_output();
{
This procedure writes the output to the screen in the formatted way.
It includes the candidate name, their vote count, and the percentage.
}
begin
for i := 1 to count do
begin
// Using Format to make the text visually appealing
write(Format('%0:-30s', [candidates[i]]));
write(Format('%d', [votes[i]]));
writeln(Format('%25.2f', [percent_array[i]]));
end;
end;
begin
// Saves the total number of candidates into the variable, count
readln(count);
// The header of the output text
writeln('Candidate' + ^i+ ^i+ ' Votes Received' + ^i + ^i +' % of Total Votes');
{
Respective procedures are called that help read the data
inputted by the user, save them, and display the result
}
read_data();
find_winner();
winner_name();
write_output();
// Remaining footer of the display text shown as output
write('Total ');
writeln(Format('%29d',[total_votes]));
writeln('The winner of the election is ' + candidates[indexOfWinner] + '.');
end.
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more