9.2.2 Planning and designing Flashcards
Name the four basic data types
Integer, float, string, boolean.
Name the 2 basic data structures
 Arrays, which could be multidimensional (arrays of arrays).
 Records, which is a single data item consisting of a number of named fields.
True or False  in this course, arrays must contain the same data type.
True!
In some languages like C and Java when the array is declared so must the data type of all the elements in the array.
However, many realworld languages like Python and JavaScript allow for arrays to be composed of many different data types.
Name and describe the 3 control structures used in algorithms and the various subtypes of each.
 Sequence  operations in an algorithm are performed in order.

Decision (branching)  different actions are taken depending on a particular condition.
 Binary decision (IF…ELSE…ENDIF)
 Multiway decision (CASEWHERE…ENDCASE)

Iteration (looping)  the same series of steps are
 Pretest loop (WHILE…ENDWHILE)
 Posttest loop (DO…WHILE)
 Counting loop (FOR…NEXT)
Draw the following algorithm flowchart symbols:
 Terminators (BEGIN/END)
 Process
 Decision
 Input/output
 Subroutine call
How would the following algorithm snippet be represented with a flowchart?
FOR i = start to finish
DISPLAY arr(i) NEXT i
Since there is no counting loop in a flowchart, the algorithm would have to be implemented with a pretest loop.
Write an algorithm to find and return the minimum value of an array called numbers
, given that each element in the array could take on any value.
BEGIN minimum min = numbers(1) FOR i = 2 to length of numbers IF numbers(i) < min THEN min = numbers(i) ENDIF NEXT i RETURN min END minimum
Write an algorithm to find and return the maximum value of an array called numbers
, given that each element in the array could take on any value.
BEGIN maximum max = numbers(i) FOR i = 2 to length of numbers IF numbers(i) > max THEN max = numbers(i) ENDIF NEXT i RETURN max END maximum
Write an algorithm to return the sum of all the elements in an array called numbers
.
BEGIN sum sum = 0 FOR i = 1 TO length of numbers sum = sum + numbers(i) NEXT i RETURN sum END sum
Write an algorithm to return the average of all the elements in an array called numbers
.
BEGIN average sum = 0 FOR i = 1 TO length of numbers sum = sum + numbers(i) NEXT i RETURN sum / length of numbers END average
Write an algorithm SimpleFind(item, arr)
that searches for a value item
in an unsorted array arr
and returns True or False, depending on whether the item was found. The algorithm need not terminate once the element has been found the first time.
Because the data are unsorted, a linear search must be used.
BEGIN Find(item, arr) found = False FOR i = 1 TO length of arr IF arr(i) = item THEN found = True END IF NEXT i RETURN found END
Write an algorithm LinearSearch(item, arr)
that returns True if item
is found in arr
or False otherwise. The algorithm should terminate once the item is found and only contain a single RETURN statement.
BEGIN LinearSearch(item, arr) found = False index = 1 WHILE index <= length of arr AND found = False IF arr(index) = item THEN found = True END IF index = index + 1 ENDWHILE RETURN found END LinearSearch
What are the two types of file access and how do they differ?
 Seqential  data is stored in a continuous stream, and must be accessed from beginning to end.
 Random access  Used to store records, an application is able to jump straight to any individual record.
What is a priming read?
In a sequential file, a pretest loop is used to repeatedly read from the file, which exits when EOF or a sentinel value is found. There needs to be a priming read before this condition is checked so that the algorithm will work if the file is empty.
What is a sentinel value?
A sentinel value is a “dummy value” used to indicate the end of data within a file.
Note: While it is more commonly applied to sequential files, a sentinel may also be used when getting an unknown number of inputs from the user. For example, in a CLI application the user might be asked to repeatedly enter data, or a blank value to exit. In this case, the blank value is the sentinel.