rares_dumitrescu
asked on
Pascal Problem
People from the contest are identified by an id and they get points in diferent chanlenges. Write a pascal problem that reads the result from the contest in pairs (id, points) the end of the pairs will terminate when it meets (0, 0), and print the competitors in decreasing order in function the points they get
If you don't have any code post a word algorithm and what you think the data and functions will be.
mlmcc
mlmcc
The first decision would be, how to store the (ID,Score) pairs before sorting and printing them.
I see 2 choices, in a table, or in a list of records or objects.
The table has a maximum size limit, while the list of records is limited by available memory (but pointer operations skill is needed).
Table is a simpler solution, of course.
I see 2 choices, in a table, or in a list of records or objects.
The table has a maximum size limit, while the list of records is limited by available memory (but pointer operations skill is needed).
Table is a simpler solution, of course.
ASKER
type concurent=record
id,proba,punctaj:integer;
end;
type concurenti=array[1..100] of concurent;
var records:concurenti;
n,i:integer;
procedure writerecord(records:concur enti;n:int eger);
var i:integer;
begin
for i:=1 to n do
writeln('Numar concurs: ', records[i].id, '; Proba: ', records[i].proba, '; Nota: ', records[i].punctaj, ';');
end;
procedure readrecord(var records:concurenti;var n:integer);
var id,proba,punctaj:integer;
begin
read(id,proba,punctaj);
n:=0;
while (id<>0) or (proba<>0) or (punctaj<>0) do
begin
n:=n+1;
records[n].id := id;
records[n].proba := proba;
records[n].punctaj := punctaj;
read(id,proba,punctaj);
end;
end;
procedure sortrecordpunctaj(var records:concurenti;n:integ er);
var i,j:integer;
tmp:concurent;
begin
for i:=1 to n do
for j:=1 to n-1 do
if records[j].punctaj < records[j+1].punctaj then
begin
tmp:= records[j];
records[j] := records[j + 1];
records[j + 1] := tmp;
end;
end;
begin
readrecord(records,n);
sortrecordpunctaj(records, n);
writerecord(records,n);
end.
id,proba,punctaj:integer;
end;
type concurenti=array[1..100] of concurent;
var records:concurenti;
n,i:integer;
procedure writerecord(records:concur
var i:integer;
begin
for i:=1 to n do
writeln('Numar concurs: ', records[i].id, '; Proba: ', records[i].proba, '; Nota: ', records[i].punctaj, ';');
end;
procedure readrecord(var records:concurenti;var n:integer);
var id,proba,punctaj:integer;
begin
read(id,proba,punctaj);
n:=0;
while (id<>0) or (proba<>0) or (punctaj<>0) do
begin
n:=n+1;
records[n].id := id;
records[n].proba := proba;
records[n].punctaj := punctaj;
read(id,proba,punctaj);
end;
end;
procedure sortrecordpunctaj(var records:concurenti;n:integ
var i,j:integer;
tmp:concurent;
begin
for i:=1 to n do
for j:=1 to n-1 do
if records[j].punctaj < records[j+1].punctaj then
begin
tmp:= records[j];
records[j] := records[j + 1];
records[j + 1] := tmp;
end;
end;
begin
readrecord(records,n);
sortrecordpunctaj(records,
writerecord(records,n);
end.
I have tested your code and seems to be working... what seems to be the problem???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Remember his is probably a school assignment so he has to follow the rules set down by the instructor.
The reason to pass the data to the procedures and functions is because they are being taught to do it that way. It would be better to pass pointers but they haven't gotten to them yet.
Also a function or procedure should stand on its own rather than relying on the using program to provide the data and properly named. In that way a procedure can be reused in another program.
mlmcc
The reason to pass the data to the procedures and functions is because they are being taught to do it that way. It would be better to pass pointers but they haven't gotten to them yet.
Also a function or procedure should stand on its own rather than relying on the using program to provide the data and properly named. In that way a procedure can be reused in another program.
mlmcc
Perhaps you are right.
But, I learned to preserve as much of the processor stack, as possible. It is a very useful habit when programming in Pascal.
Other cleaning proposition is to remove the global declaration of the variable "i". As, it is declared in every proper procedure.
So,:
var records:concurenti;
n:integer;
instead of:
var records:concurenti;
n,i:integer;
But, I learned to preserve as much of the processor stack, as possible. It is a very useful habit when programming in Pascal.
Other cleaning proposition is to remove the global declaration of the variable "i". As, it is declared in every proper procedure.
So,:
var records:concurenti;
n:integer;
instead of:
var records:concurenti;
n,i:integer;
Please post the code you have thus far and ask a specific question.
mlmcc