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.