Reviami1
Badges: 5
Rep:
?
#1
Report Thread starter 1 year ago
#1
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.)
0
reply
winterscoming
Badges: 19
Rep:
?
#2
Report 1 year ago
#2
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.
1
reply
Reviami1
Badges: 5
Rep:
?
#3
Report Thread starter 1 year ago
#3
(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. 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
1
reply
winterscoming
Badges: 19
Rep:
?
#4
Report 1 year ago
#4
(Original post by Reviami1)
Thank you so much for the help and tips. I have thought about what you said and broken up the problem:

Code:
#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 That looks right to me
0
reply
Reviami1
Badges: 5
Rep:
?
#5
Report Thread starter 1 year ago
#5
(Original post by winterscoming)
Good one That looks right to me
Thank you
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
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

New lockdown - Do you agree schools and universities should remain open?

Yes (50)
37.31%
No (70)
52.24%
I don't know (14)
10.45%

Watched Threads

View All