#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# determine the winner

Posts: 11Member
I am trying to write an algorithm that would take as input four students and their grades. Purpose: to determine who is the winner among the four.
The algorithm must be able to take any four students and their grades to determine the winner.

my email: cutie1gd@gmail.com

thanks
· ·
«1

• Posts: 163Member
: I am trying to write an algorithm that would take as input four
: students and their grades. Purpose: to determine who is the winner
: among the four.
: The algorithm must be able to take any four students and their
: grades to determine the winner.
:
: Can you help me please?
: Any advise would be appreciated.
: my email: cutie1gd@gmail.com
:

First calculate the average grade for each student, then find out who has the highest. This topic was discussed here not too long ago with code examples, read on...
· ·
• Posts: 31Member
[code]PROGRAM solution;
USES crt;
VAR score, highest, i, winner: integer; {or real if score has decimals}

BEGIN
score:= 0; highest:= 0;
FOR i:= 1 TO 4 DO
BEGIN
clrscr;
write('Input student ', i, '''s score: '); readln(score);
IF score>highest THEN BEGIN highest:= score; winner:= i END
END;
clrscr;
writeln('Student ', winner, ' with score, ', highest, ', is the winner.');
END.[/code]

Modify it to determine 'valid input' (example: score is in range 1~100).
· ·
• Posts: 11Member
thank you very much, this is working good.

I was wondering if an 'array' can be used?
· ·
• Posts: 11Member
Can arrays be used in this algorithm?
· ·
• Posts: 31Member
Yes, you can apply ARRAY in this case.

And a suggested solution will be:

[code]PROGRAM solution2;

USES crt;

VAR data_amt, count, winner, highest: integer;
data: ARRAY[1..40] OF integer;

BEGIN

data_amt:= 0; winner:= 0; highest:= 0; FOR count:= 1 TO 40 DO data[count]:= 0;

write('How many pieces of data you are going to enter? (2~40) '); readln(data_amt);

FOR count:= 1 TO data_amt DO
BEGIN write('What is the score of student ', count, ': '); readln(data[count]);
IF data[count]>highest THEN BEGIN winner:= count; highest:= data[count] END END;

writeln('The winner is student ', winner, ' and he has a score of ', highest, '!');

END.[/code]
· ·
• Posts: 11Member
thank you very much, saw my mistake from the help you posted.

I have another problem

Problem:
write an algorithm that accepts vote data at a voting station in a constituency.
Data should include special votes, general votes and spoilt votes. Votes should be cast for any one of four parties, FIRST, SECOND, THIRD or FOURTH. The algorithm should trace the increment of each
vote category and determine which party secured the majority of votes. The algorithm should have at least ten iterations and should end when a specific value is entered.

What I came up with so far:

Program Vote;

VAR
WNA,PDR,DAP,PLM:integer;
v :integer;
valid :integer;
spoilt :integer;
special: integer;
BEGIN
v:=-1;
WNA:=0;
PDR:=0;
DAP:=0;
PLM:=0;
valid:=0;
spoilt:=0;
while v <> 0 DO begin
Writeln('WNA(1), PDR(2), DAP(3), PLM(4)');
writeln;

If(v <1) or (v>4) Then begin
writeln('Invalid vote: ', v);
spoilt :=spoilt+1;
end

· ·
• Posts: 11Member
if there is a tie?
two students with the same grade.

how do you incorporated that in the code?
· ·
• Posts: 31Member
Then you can

First, find out the highest mark among the students.
Second, use For loop to search the winner(s) and list out all winners with a If statement.
· ·
• Posts: 31Member
[code]
PROGRAM solution2;

USES crt;

VAR data_amt, count, winner, highest, winner_count: integer;
data: ARRAY[1..40] OF integer;

BEGIN

data_amt:= 0; winner:= 0; highest:= 0; winner_count:= 0; FOR count:= 1 TO 40 DO data[count]:= 0;

write('How many pieces of data you are going to enter? (2~40) '); readln(data_amt);

FOR count:= 1 TO data_amt DO
BEGIN write('What is the score of student ', count, ': '); readln(data[count]);
IF data[count]>highest THEN BEGIN winner:= count; highest:= data[count] END END;

writeln; writeln('---Winner List---');

FOR count:= 1 TO data_amt DO
IF data[count]=highest THEN
writeln('The winner is student ', winner, ' and he has a score of ', highest, '!');

END.[/code]
· ·
• Posts: 11Member
program winner;

type
student=record
Name : string;
end;

var
i : integer;

begin

for i:=Low(list) to High(list) do
begin
WriteLn('Enter Student Name ');

end;
end;

procedure PrintResult(var list:array of student);
var
i : integer;

begin
begin
WriteLn('Winner is : ' , list[0].name);
end
begin
WriteLn('Winner is : ' , list[1].name);
end
begin
WriteLn('Winner is : ' , list[2].name);
end
begin
WriteLn('Winner is : ' , list[3].name);
end;

end;

procedure Main();
var
marks : array[0..3] of student;
begin
PrintResult(marks);

end;

begin
Main();
end.
· ·
«1