This discussion is closed.
Timotei
Badges: 0
#1
Report Thread starter 17 years ago
#1
need help with the following assignment! It must be done in pascal, that lovely programming language...

---------
YOUR TASK
---------

There are 60 students enrolled on a hypothetical course, you must assign each
student one of six lab classes, taking into account the capacity of each class.
Each student has four choices, none of which are guaranteed to be satisfied.


There are two files for input to the program

1) A file called students.txt

The file contains details of 60 students, a FORENAME of type 'string', a
SURNAME of type 'string' and their four choices of lab session, each option
an integer, separated by a space, on a single line. a couple of records
might look like this:

West
Timothy
1 2 5 4
Moore
Andrew
2 5 6 1
....

2) A file called classes.txt

Where the details of six classes are held. Each class has a number, of type
integer, a day, a time, both of type string, and a maximum capacity, of type
integer. For example:

1
Monday
1:30
15
2
Monday
3:30
12
....


Your job is to read in the data, store them in appropriate data structures and
assign students to lab classes ensuring that no class has more students than
it can sit. You must then print one class at a time to the screen, detailing
the class number, the day and time, an alphabetical list of all the students
in that class and a "MISERY FACTOR" for that class. The program should stop
printing after each class so the user can read one class at a time.

The Misery Factor for a class is calculated thus,

A student assigned to their 1st choice has a Misery Factor of 0.

A student assigned to their 2nd choice has a Misery Factor of 5.

A student assigned to their 3rd choice has a Misery Factor of 10.

A student assigned to their 4th choice has a Misery Factor of 15.

A student assigned to none of their choices has a Misery Factor of 20.

-----------
ASSUMPTIONS
-----------

You may assume no class has a capacity of more than 15 Students.
You may assume that the files exist and that there are no errors in them.


I have made a valiant attempt, aided by several invaluable people, but I just can't get this one done, and the deadline is fast approaching, if anyone knows a decent bit of pascal language, an example program, or sections of code, or some ideas on how to accomplish this task would be greatly appreciated!
0
Camford
Badges: 12
Rep:
?
#2
Report 17 years ago
#2
I wonder why it has to be in txt format. Can't you use record instead? The way I would be doing would be to convert txt to Pascal Record Type first and then deal with it. That way, it speeds up the search a little.

First you have to find out a way to arrange the student of which the misery factor is minimised. Then computerise the algorithm you used to achieve such stituation. You may be able to find out the answer to that one from your Maths teacher. Probably D module teachers are better.

Have to say, when I tried to convert txt to rec, Pascal gave me loads of trouble. but using eoln and Chr(ord(space)), you should be able to extract the data successfully. And the all important eof as well.

by the way, you shouldn't really be asking other people to help you to do all the homework. The least you can do is to put what you have done up here for us to have a look.
0
Timotei
Badges: 0
#3
Report Thread starter 17 years ago
#3
(Original post by Camford)
by the way, you shouldn't really be asking other people to help you to do all the homework. The least you can do is to put what you have done up here for us to have a look.
I wasn't expecting someone to do it for me, that is just wrong, I understand that.

Program AssignmentOne

{$APPTYPE CONSOLE}

uses
SysUtils;

Type
student = record
forename : string;
surname : string;
choices : string;
end;

session = record
Number : string;
Time : String;
Day : String;
Capacity : string;
end;

var
students : array[1..60]of student;
source : text;
source2 : text;
sessions : text;
s : string;
s2 : string;
x : integer;
xx : integer;
n : integer;
nn : integer;

Procedure ReadStudents;

var
i : integer;

begin
assignfile(source,'students.txt' );
reset(source);

x:= 1;
n:= 1;

for i := 1 to 180 do begin
readln(source2,s);
if n = 1 then
begin
students[x].forename := s;
n := 2;
end
else if n = 1 then
begin
students[x].surname := s;
n := 3;
end
else if n = 1 then
begin
students[x].choices := s;
n := 1;
x := x + 1;
closefile(source);
end;
end;
end;

Procedure ReadSessions;

var
i : integer;

begin
assignfile(source,'sessions.txt' );
reset(source);

xx:= 1;
nn:= 1;

for i := 1 to 24 do begin
readln(source,s2);
if nn = 1 then
begin
sessions[xx].Number := s2;
nn := 2;
end
else if nn = 1 then
begin
sessions[xx].Time := s2;
nn := 3;
end
else if nn = 1 then
begin
sessions[xx].Day := s2;
nn := 4;
end
else if nn = 1 then
begin
sessions[xx].Capacity := s;
nn := 1;
xx := xx + 1;
closefile(source);
end;
end;
end;

begin
ReadStudents;
//procedure will be ReadSessions
//procedure will be AllocateStudents
//procedure will be PrintResults
Readln;
end.


That's what I have so far, but it won't compile, something in the reading of the sessions file (see first post) and I can't get my head around why... that line in bold is what the compiler highlights saying

[Error] assignmentONE.dpr(84): Array type required
[Error] assignmentONE.dpr(89): Array type required
[Error] assignmentONE.dpr(89): Incompatible types: 'TDateTime' and 'String'
[Error] assignmentONE.dpr(94): Array type required
[Error] assignmentONE.dpr(99): Array type required


all of that... I'm really at a loss.
0
Camford
Badges: 12
Rep:
?
#4
Report 17 years ago
#4
looks like you are doing in delphi...I wonder which version it is. I had the same problem when I tried to do rec type things. The only thing in your code I think needs change to make the student's choices into four parts instead of just one string. Or if you like and make it "Arrary[1..4] of char". As far as I know, it is better not to use for loop in a situation like this. You should consider the

while not eof do
begin
while not eoln do
begin
read(source, char);
student[x].forename:=studentforename+char;
end;
do the same for the other parts.

The other problem I see in your code is that you are using n= number rather than n mod 4 = number (from 0 to 3). that's probably where you have gone wrong.

I can't put a guarantee on my code. I was never intersted in record files. But now I really wish I did.

Hope it helps.

Camford

p.s. I personally would make the output go into a list box. Since you are using Delphi then why not make it windows oriented and allow users to choose source files? Besides, using the standard open file dialog box isn't that hard.
0
X
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Following the government's announcement, do you think you will be awarded a fair grade this year?

Yes (535)
51.1%
No (512)
48.9%

Watched Threads

View All