Computational Constructs Flashcards
What does declarative programming focus on?
Defining what a program should achieve rather than defining the method of achieving the desired output
What does Imperative Programs define?
Exactly the steps that a computer will execute to achieve a specific result
What do programs in Prolog consist of?
Facts and Rules
What is a knowledge base?
Combination of the facts and rules in prolog
What is Recursion?
A programming technique where a sub-program repeatedly calls itself
In what type of programming languages is recursion used?
Declarative and Imperative Programming Styles
What is passing by reference?
Passing the real copy of the parameter, changes to this parameter will be proper changes
What is passing by value?
Any changes to the parameter will not be carried onto the real value
Write the pseudocode for the Fibonacci sequence
def Fibonacci(n): If n < 2: return n else return Fibonacci(n-1) + Fibonacci(n-2)
Give some advantages of recursion
- It is a unique way of implementing a variable number of nested
loops. - Prevents repeating of code
Give some disadvantages of recursion
- Recursion uses more memory than iteration and looping
- Recursive methods will often throw a Stack Overflow Exception error
when processing big sets of data
What are the operations that can performed on a sequential file?
- Write data
- Read data
- Append
What is a sequential file identified by?
Identified by a path and a filename
Describe a sequential file
- Identified by a path and a filename
- Reading from the beginning
- Cannot be written and read to at the same time
- If file does not exist, must be created first
What is the difference between append and read/write functions for sequential file?
Append allows data to be added to the file without it being read into memory first
Write pseudocode for writing (a set of string values)to a new sequential text
file from an array
Procedure writeFile(name): DECLARE myFile INITIALLY "N://myFile.txt" CREATE myFile OPEN myFile FOR counter FROM 0 to End of Array DO SEND names[counter] to myFile END FOR CLOSE myFile END Procedure
Write pseudocode for reading (a set of string values) from a sequential text file into
an array
PROCEDURE ReadFile(name) DECLARE myFile INITIALLY "N://myFile.txt" OPEN myFile FOR counter FROM 0 to End of File DO Receive names[counter] from myFile END FOR CLOSE myFile END PROCEDURE
Describe a random file
A file which stores data in a way which allows it to be retrieved from any place in the file without needing to read through the entire file
Data is indexed in a way which allows each item to be uniquely identified
What are random files frequently used to store?
Records
Give three advantages to random files?
- Fast access to data since position of any item can be calculated from it’s index
- Data items can be accessed individually instead of having to read the entire file
- Random files can be read and written to simultaneously