Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4098
Number of posts: 14002

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
determine the winner Posted by kellzkellz on 23 Jan 2011 at 8:35 PM
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

thanks
Report
Re: determine the winner Posted by _Atex_ on 24 Jan 2011 at 9:06 PM
: 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...
Report
Re: determine the winner Posted by blackr1234 on 26 Jan 2011 at 7:00 AM
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.');
readkey
END.


Modify it to determine 'valid input' (example: score is in range 1~100).
Report
Re: determine the winner Posted by kellzkellz on 26 Jan 2011 at 5:04 PM
thank you very much, this is working good.

I was wondering if an 'array' can be used?
Report
Re: determine the winner Posted by kellzkellz on 20 Feb 2011 at 8:17 PM
Can arrays be used in this algorithm?
Report
Re: determine the winner Posted by blackr1234 on 26 Feb 2011 at 5:09 AM
Yes, you can apply ARRAY in this case.

And a suggested solution will be:

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, '!');

readkey

END.

Report
Re: determine the winner Posted by kellzkellz on 26 Feb 2011 at 2:58 PM
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)');
read(v);
writeln;

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




Report
Re: determine the winner Posted by kellzkellz on 1 Mar 2011 at 5:12 PM
if there is a tie?
two students with the same grade.

how do you incorporated that in the code?
Report
Re: determine the winner Posted by blackr1234 on 8 Mar 2011 at 5:02 AM
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.
Report
Re: determine the winner Posted by blackr1234 on 8 Mar 2011 at 5:05 AM
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, '!');

readkey

END.

Report
Re: determine the winner Posted by ROCK_ on 24 Apr 2011 at 7:13 AM
program winner;

type
student=record
Name : string;
Grade : double;
end;

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

begin

for i:=Low(list) to High(list) do
begin
WriteLn('Enter Student Name ');
ReadLn(list[i].name);
WriteLn('Enter grade of student ');
ReadLn(list[i].grade);

end;
end;

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

begin
ReadResult(list);
if (list[0].grade>list[1].grade) and (list[0].grade>list[2].grade) and (list[0].grade>list[3].grade) then
begin
WriteLn('Winner is : ' , list[0].name);
end
else if (list[1].grade>list[0].grade) and (list[1].grade>list[2].grade) and (list[1].grade>list[3].grade) then
begin
WriteLn('Winner is : ' , list[1].name);
end
else if (list[2].grade>list[0].grade) and (list[2].grade>list[1].grade) and (list[2].grade>list[3].grade) then
begin
WriteLn('Winner is : ' , list[2].name);
end
else if (list[3].grade>list[0].grade) and (list[3].grade>list[1].grade) and (list[3].grade>list[2].grade) then
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.
Report
Re: determine the winner Posted by blackr1234 on 11 Jun 2011 at 2:38 AM
I have fixed a bug. You can find it working just after a compilation.
You can use Dev-Pascal.


PROGRAM final;

USES crt;

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

BEGIN

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

write(' Students in total (2~40): '); readln(data_amt);

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

writeln; writeln(' Winner(s):');

FOR count:= 1 TO data_amt DO
IF data[count]=highest THEN
writeln(' Student ', count, ' with score, ', highest, '!');

readkey

END.




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.