9.2 Algorithms (3)
Resources |
Revision Questions |
Computer Science
Login to see all questions
Click on a question to view the answer
1.
Convert the following pseudo-code into a flowchart. The pseudo-code calculates the sum of numbers from 1 to n, where n is provided by the user.
Pseudo-code:
- Begin
- Input n
- Set sum to 0
- For i = 1 to n do
- sum = sum + i
- End For
- Display sum
- End
Flowchart:
Begin |
Input n |
|
|
sum = sum + i |
End For |
Display sum |
End |
2.
You are designing a database to store information about students in a university. The database needs to hold details such as student ID, name, course of study, and contact information. Explain how you would choose appropriate identifier names for the data fields in this database. Provide examples of both good and bad identifier names, justifying your choices. Then, create an identifier table to represent the key attributes of a student.
Choosing Identifier Names for Student Database Fields: The principles for choosing good identifier names are the same as described above. Clear, descriptive names are essential for database design. Using consistent naming conventions is also important for database management and querying.
- Avoid Abbreviations: While abbreviations can save space, they often make the code harder to understand. For example, using
stud_id
is less clear than studentID
. - Use Camel Case or Snake Case: Camel case (e.g.,
studentID
) or snake case (e.g., student_id
) are common conventions for database field names. Consistency is key. - Avoid Reserved Words: Be careful not to use database reserved words as field names (e.g.,
order
, group
).
Examples of Good and Bad Identifier Names:
Bad: id , num , info | Good: studentID , firstName , lastName , courseOfStudy , emailAddress , phoneNumber |
Bad: sid , cname | Good: studentID , courseName |
The good examples are self-explanatory and clearly indicate the data they represent. This makes the database schema easier to understand and maintain.
Identifier Table:
Attribute Name | Identifier Name |
Student ID | studentID |
First Name | firstName |
Last Name | lastName |
Course of Study | courseOfStudy |
Email Address | emailAddress |
Phone Number | phoneNumber |
3.
Consider the problem of finding the largest number in an unsorted list of integers. Explain how an algorithm provides a solution to this problem. Describe the key characteristics of an algorithm that make it suitable for solving this type of problem.
An algorithm provides a solution to the problem of finding the largest number in an unsorted list by outlining a sequence of steps that systematically compare the elements of the list and identify the one with the highest value. The algorithm's steps are designed to ensure that every element is considered and the largest is correctly identified.
Key characteristics of an algorithm that make it suitable for this problem include:
- Definiteness: Each step is clearly defined and unambiguous. For example, "compare element A with element B" is a definite step.
- Finiteness: The algorithm must terminate after a finite number of steps. It cannot run indefinitely.
- Input: The algorithm must accept a well-defined input – in this case, the unsorted list of integers.
- Output: The algorithm must produce a well-defined output – the largest number in the list.
- Effectiveness: Each step must be practically executable. The comparisons and assignments involved are basic operations that a computer can perform.
A simple algorithm for this would involve iterating through the list, keeping track of the largest number encountered so far, and updating that largest number whenever a larger element is found.