The Student Room Group

AQA Computer Science Records and Files question

Could I have some help on how to do this question please?
Any help would be much appreciated.

Question:

There are some records on the file. Write an algorithm which reads the names and marks and prints the name and mark of the student with the highest mark. (Assume no two students achieved the same mark.)
Break the problem down into smaller pieces - it sounds like the first thing you need to do is be able to read the file and parse its data, so start by trying to write a program which is simply able to read that data into memory.

The first thing to do before writing any code is analyse the layout of the records in the file and describe the 'rules' which determine what the shape of each of your records looks like. i.e.
Where does a record begin?
Where does a record end?
Where does the student name begin?
Where does the student name end?
Where does the student mark begin?
Where does the student mark end?

Using those rules you should be able to write a program - start with something smalll - initially you may just write some code which can open the file and read out just a single record. -- it's good to solve the smallest possible problem first; don't try to solve everything at once. If you can write a program which reads just one single record, then try writing a program which reads 2, then 3, then 4.

When you can do something once in a program, doing that same thing multiple times is a matter of repetition (Which is something which programming languages are good at using "While" and "For") -- so really there should be no difference between the first time you read a record and the "N'th" time.

Consider using StackOverflow and Google to search for some example snippets of code for how to do these small tasks in a programming language. e.g.
- "How do open a file in <language>"
- "How to read a line of text from a file in <language>"
- "How to split a string in <language>"

Also consider the concepts of data in whichever language you're using. Every language tends to have the same data types - i.e. Number, String, Boolean, List/Array, Record/Structure, etc.

Before writing code, make sure you think carefully about the variables you will use and the data types you want. Make sure you choose the most appropriate types for the data you're reading. If you've thought about the problem, then the data types should probably be obvious.
Reply 2
Original post by winterscoming
Break the problem down into smaller pieces - it sounds like the first thing you need to do is be able to read the file and parse its data, so start by trying to write a program which is simply able to read that data into memory.

The first thing to do before writing any code is analyse the layout of the records in the file and describe the 'rules' which determine what the shape of each of your records looks like. i.e.
Where does a record begin?
Where does a record end?
Where does the student name begin?
Where does the student name end?
Where does the student mark begin?
Where does the student mark end?

Using those rules you should be able to write a program - start with something smalll - initially you may just write some code which can open the file and read out just a single record. -- it's good to solve the smallest possible problem first; don't try to solve everything at once. If you can write a program which reads just one single record, then try writing a program which reads 2, then 3, then 4.

When you can do something once in a program, doing that same thing multiple times is a matter of repetition (Which is something which programming languages are good at using "While" and "For") -- so really there should be no difference between the first time you read a record and the "N'th" time.

Consider using StackOverflow and Google to search for some example snippets of code for how to do these small tasks in a programming language. e.g.
- "How do open a file in <language>"
- "How to read a line of text from a file in <language>"
- "How to split a string in <language>"

Also consider the concepts of data in whichever language you're using. Every language tends to have the same data types - i.e. Number, String, Boolean, List/Array, Record/Structure, etc.

Before writing code, make sure you think carefully about the variables you will use and the data types you want. Make sure you choose the most appropriate types for the data you're reading. If you've thought about the problem, then the data types should probably be obvious.

Thank you so much for the help and tips. :smile: I have thought about what you said and broken up the problem:


#Read names and mark

markFile.openRead ("markFile.txt") #Opens mark file in "read" mode

markRecord <- markFile.readLine()

maxMark <- 0



WHILE NOT markFile.endOfFile()

Split the file into individual fields name, mark

IF mark>maxMark THEN #To find the highest mark

maxMark <- mark

bestStudent <- name

ENDIF


markRecord <- markFile.readline()

Split fields into two fields name, mark


ENDWHILE



OUTPUT bestStudent,maxMark #Displays student with best the mark and the best mark

CLOSE markFile #Closes file
Original post by Reviami1
Thank you so much for the help and tips. :smile: I have thought about what you said and broken up the problem:

#Read names and mark
markFile.openRead ("markFile.txt") #Opens mark file in "read" mode
markRecord <- markFile.readLine()
maxMark <- 0

WHILE NOT markFile.endOfFile()
Split the file into individual fields name, mark
IF mark>maxMark THEN #To find the highest mark
maxMark <- mark
bestStudent <- name
ENDIF

markRecord <- markFile.readline()
Split fields into two fields name, mark

ENDWHILE

OUTPUT bestStudent,maxMark #Displays student with best the mark and the best mark
CLOSE markFile #Closes file


Good one :smile: That looks right to me
Reply 4
Original post by winterscoming
Good one :smile: That looks right to me

Thank you :smile:

Quick Reply

Latest

Trending

Trending