8 Subroutines, File Handling and Design Flashcards
What is a structure diagram?
- A modelling tool used to show the hierarchy of a system/program
- A graphical representation of top down design to show how a system is broken into subsystems
- They are used at the design stage of the software development lifecycle
What are library routines?
A collection of standard programs/subroutines available for immediate use
What is a subroutine?
- A self-contained piece of code that that is given a name and can called from within a program
- 2 types of subroutines
- A function - always returns a value
- A procedure - may or may not return a value
What is a procedure?
A subroutine that does not have to return a value
What is a function?
A subroutine that always returns a value
Describe the use of a subroutine in a program
- It is a block of code within a program that can be called when needed
- It breaks up the program to make it easier to read and understand
- It can be reused by another program
What is a library routine?
- A standard subroutine that is available for immediate use
- It can be called from many programs
- It is used often and makes programs easier/faster to write as the code is already written
- They make testing easier as they have already been used and debugged
What is top down design?
Breaks down a system into successively smaller pieces/sub systems
What are the benefits of top down design?
- allows several programmers to work at the same time on the software
- Development time is faster
- can test each subsystem independently
- Easier to debug
What are the advantages of using library routines?
- Makes writing programs faster as we are reusing code
- Makes testing easier as the code has already been tested.
The library routine DIV is used for integer division. What would the code below output?
i ← DIV(11, 4)
OUTPUT i
i ← DIV(3, 2)
OUTPUT i
i ← DIV(2, 4)
OUTPUT i
2
1
0
The library routine MOD is used to find the remainder. What would the code below output?
i ← MOD(11, 4)
OUTPUT i
i ← MOD(3, 2)
OUTPUT i
i ← MOD(2, 4)
OUTPUT i
3
1
2
The library routine ROUND is used to round a decimal number to a certain number of places. What would the code below output?
r ← ROUND(2.6789, 3)
OUTPUT r
r ← ROUND(3.142, 0)
OUTPUT r
r ← ROUND(1.111111, 4)
OUTPUT r
r ← ROUND(1.227, 2)
OUTPUT r
2.679
3
1.1111
1.23
The library routine RANDOM returns a random number between 0 and 1 inclusive. What is the smallest and largest random number that could be generated by the pseudocode statement below.
value ← ROUND(RANDOM() * 100, 0)
Smallest = 0
Largest = 100
The string handling library routine LENGTH returns the number of characters in a string. What would the call below return?
length = LENGTH(“Happy Days”)
10
The string handling library routine LCASE returns the string with all characters in lower case.. What would the call below return?
word = LCASE(“LovEly”)
lovely
The string handling library routine UCASE returns the string with all characters in upper case.. What would the call below return?
word = UCASE(“LovEly”)
LOVELY
2The string handling library routine SUBSTRING returns a substring of a specified length starting at the position specified.
What would the calls below return?
s1 = SUBSTRING(“Happy Days”, 1, 5)
s2 = SUBSTRING(“Happy Days”, 2, 2)
s1 = “Happy”
s2 = “ap”
What are the advantage of subroutines?
- Can sometimes be reused in other programs
- Breaks down code and makes it easier to read and debug
- Can be called many times, so code is not repeated
What are parameters in a subroutine?
The variables in a procedure or function declaration that store the values passed from the main program to a procedure or function
Write a procedure that accepts an integer as a parameter and a string. It should then output that string that number of times
PROCEDURE OutputSentence(numTimes: INTEGER, sentence: STRING) FOR int i ← 1 TO numTimes STEP 1 OUTPUT sentence ENDFOR ENDPROCEDURE
Write a function that when given a radius (REAL) calculates and returns the area of a circle.
FUNCTION CalcCircleArea(radius: REAL) RETURNS REAL REAL area \<- 0 area \<- 3.14159 \* radius \* radius RETURN area ENDFUNCTION
What are the 4 main stages of the Software Development Lifecycle
Analysis
Design
Coding
Testing
Describe analysis in terms of the software development lifecycle
- Identification of the problem and the requirements specification (what a program is required to do)
- Involves abstraction and decomposition of the problem to identify exactly what is required.
Describe design in terms of the software development lifecycle
- Uses the requirements specification from the analysis stage to show to how the program should be developed.
- This can be formally documented using structure charts, flowcharts and pseudocode.
Describe coding in terms of the software development lifecycle
- The program or set of programs are developed.
- Each module of the program is written using a suitable programming language and then tested.
- Iterative testing is conducted on modules and and code amended until the module performs as expected,
Describe testing in terms of the software development lifecycle
- Systematic checks are done on a program to make sure that it works under all conditions.
- The completed program is run many times with different sets of test data.
What is abstraction?
The key elements required for a solution to the problem are kept and any unnecessary details and information that are not required are discarded
What is decomposition?
A complex problem is broken down into smaller parts, which can then be sub divided into even smaller parts that can be solved more easily
What is top down design?
- The breaking down of a computer system into a set of sub-systems.
- Then breaking each sub-system down into a set of smaller sub-systems, until each sub-system just performs a single action
- This hierarchy can then be represented using a structure diagram
What are the 4 major components of a computer system?
- inputs -the data used by the system that needs to be entered while the system is active.
- processes - the tasks that need to be performed using the input data and any stored data
- outputs - information that needs to be displayed or printed for the users of the system
- storage - data that needs to be stored in files on an appropriate medium for use in the future
When writing computer programs why do we store data in a file?
- Any data stored in RAM will be lost when the computer is switched off
- When data is saved to a file it is stored permanently
- The data can then be accessed by the same program at a later date or accessed by another program
Write some pseudocode that asks the user to enter their firstname and surname and then writes these to a text file called “UserDetails.txt” on separate line.
DECLARE surname : STRING DECLARE firstName: STRING DECLARE fileName : STRING filename <- "UserDetails.txt" OUTPUT "Please enter first name" INPUT firstName OUTPUT "Please enter surname" INPUT surname OPEN fileName FOR WRITE WRITEFILE fileName, firstName WRITEFILE fileName, surname CLOSEFILE (filename)
A text file contains a username on the first line and a password on the second line. Write some pseudocode that reads the username and password from a text file called “UserDetails.txt” and then displays them on the screen
DECLARE username : STRING DECLARE password: STRING DECLARE fileName : STRING filename <- "UserDetails.txt" OPEN fileName FOR READ READFILE fileName, userName READFILE fileName, password CLOSEFILE (filename) OUTPUT username OUTPUT password
Write some pseudcode to read the first line of a text file called “info.txt”
DECLARE fileName : STRING DECLARE line : STRING filename <- "info.txt" OPEN filename FOR READ READFILE fileName, line CLOSEFILE (filename)
Write an algorithm to write the numbers 1 to 1000 to a text file named Numbers.txt
DECLARE fileName : STRING fileName <- "Numbers.txt" OPENFILE fileName FOR WRITE FOR i <- 1 TO 1000 STEP 1 WRITEFILE fileName, i NEXT i CLOSEFILE fileName
Write an algorithm to read the first 100 student names from a text file named Names.txt. The data read in should be stored in an array of a suitable size
DECLARE studentNames : ARRAY[1:100] OF STRING DECLARE fileName : STRING fileName <- "Names.txt" OPENFILE fileName FOR READ FOR i <- 1 TO 100 STEP 1 READFILE fileName, studentNames[i] NEXT i CLOSEFILE fileName