determine the winner

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

Comments

  • : 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...
  • [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.');
    readkey
    END.[/code]

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

    I was wondering if an 'array' can be used?
  • Can arrays be used in this algorithm?
  • 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, '!');

    readkey

    END.[/code]
  • 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




  • if there is a tie?
    two students with the same grade.

    how do you incorporated that in the code?
  • 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.
  • [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, '!');

    readkey

    END.[/code]
  • 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.
  • I have fixed a bug. You can find it working just after a compilation.
    You can use Dev-Pascal.


    [code]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.[/code]
Sign In or Register to comment.

Howdy, Stranger!

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

Categories