10.3 Files (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
Explain, with examples, why files are a fundamental concept in computer science and essential for storing and managing data within computer systems. Your answer should discuss the advantages of using files over other methods of data storage.
Files are a fundamental concept in computer science because they provide a structured and persistent way to store data on a computer system. Without files, managing and accessing data would be significantly more complex and inefficient. Here's a breakdown of why files are essential:
- Data Persistence: Files allow data to be saved and retrieved even after the computer is turned off. This persistence is crucial for applications that need to retain information between sessions.
- Organization: Files provide a mechanism for organizing data into logical units. This makes it easier to manage large amounts of information and locate specific data quickly.
- Modularity: Files enable the separation of different parts of a program or a system. This modularity improves code maintainability and reusability. For example, a large software project might be divided into multiple files, each containing a specific set of functions or data.
- Sharing and Collaboration: Files can be easily shared between different users and systems, facilitating collaboration. This is a core aspect of modern computing, with cloud storage and network file sharing relying heavily on the file concept.
Advantages over other methods:
- Flexibility: Files can store various types of data, including text, images, audio, video, and executable code.
- Efficiency: File systems are designed for efficient storage and retrieval of data, using techniques like indexing and data structures.
- Standardization: File formats provide a standardized way to represent data, ensuring compatibility between different applications and systems.
Examples:
- A text editor saves your document as a text file (.txt).
- An image editing program saves your artwork as a JPEG file (.jpg).
- A program's source code is typically stored in multiple text files (.c, .java, .py).
- A database stores data in various files, often with specific file formats like CSV or XML.
2.
Describe, using pseudo-code, how you would handle a text file that contains student names and their corresponding marks, separated by a comma. The format of each line in the file is "Name,Mark". Your pseudo-code should include steps to read each line, split the line into name and mark, and store the data in a suitable data structure. Also, outline how you would calculate the average mark for all students.
Algorithm: Student Marks Processing
Data Structure: A list of dictionaries. Each dictionary will store a student's name (key) and their mark (value). For example: [{"Alice": 85}, {"Bob": 72}, {"Charlie": 91}]
- Start
- Open the text file for reading.
- Create an empty list called
studentData
. - For each line in the file:
- Split the line into two parts using the comma (
','
) as a delimiter. - Extract the name and the mark.
- Create a new dictionary with the name as the key and the mark as the value.
- Append the dictionary to
studentData
.
- Calculate the total mark:
- Initialize a variable
totalMarks
to 0. - For each dictionary in
studentData
:- Add the mark (value) to
totalMarks
.
- Calculate the number of students:
- Get the number of elements in
studentData
and store it in a variable numStudents
.
- Calculate the average mark:
- Divide
totalMarks
by numStudents
.
- Print the average mark.
- Close the text file.
- End
3.
Write pseudo-code to outline an algorithm that reads a text file line by line, stores each line in a list, and then prints the lines in reverse order. Your pseudo-code should clearly indicate how the file is opened, read, and closed.
Algorithm: Reverse File Lines
- Start
- Open the text file for reading.
- Create an empty list called
fileLines
. - Read the file line by line:
- While there are more lines to read:
- Read the next line from the file.
- Append the line to
fileLines
.
- Create a new empty list called
reversedLines
. - For each line in
fileLines
:- Append the line to the beginning of
reversedLines
.
- Print the contents of
reversedLines
, with each line on a new line. - Close the text file.
- End