Chap 8 - pseudocode Flashcards

(48 cards)

1
Q

features of pseudocode

A

-all command words are in UPPERCASE
-DECLARE all variables at the start of the code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

rules for variables

A

-has to start with a character
-assignee using ←
-case insensitive
Poop & poop are the same variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

declaring variables

A

DECLARE variable: data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

outputting

A

OUTPUT variable
OUTPUT “hello”, “world”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

taking input

A

OUTPUT “question/ statement”
INPUT variable
eg.
OUTPUT “name?”
INPUT name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

how to comment on code

A

// this is a comment
// slashes must be added for each line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

data types

A

-integer
-boolean
-real
-string - delimited by “ “
-char- delimited by ‘ ‘

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

arithmetic operation for
-addition
-subtraction
-multiply
-divide
-power
-squared
-remainder - returns remainder
-integer division - returns quotient
-rounding
-give random int

A
  • +
  • -
  • *
  • /
  • ^(1/2)
    -MOD (num, divide by)
    -DIV (num, divide by)
    -ROUND (num, decimal place)
    -RANDOM( )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

logical operators for:
-greater than
-greater than or equal
-smaller than
-smaller than or equal
-equal (comparison)
-not equal (comparison)

A
  • >
  • > =
  • <
  • <=
  • =
  • <>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Boolean operators

A

AND
OR
NOT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

assigning variables

A

-use ←
eg.
age ← 24
pop ←”poop”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

if, else conditional statement
(else is not necessary)

A

IF ( )
THEN
action
ELSE
action
ENDIF

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

nested ifs

A

IF ( )
THEN
action
ELSE
IF ( )
THEN
action
ELSE
action
ENDIF
ENDIF

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

case statement

A

CASE OF variable
“__” : action
“__” : action
OTHERWISE action
ENDCASE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

note: for loops in pseudocode, both values are included
eg. 0 TO 4 = 5 runs
so always start with 1

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

for loop

A

FOR i ← 1 TO x
action
NEXT i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

while loop

A

i ← x
WHILE i <10
action
i ← i + 1
ENDWHILE
-use for validation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

repeat until loop

A

i ← x
REPEAT
action
i ← i + 1
UNTIL i <10
-use for validation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How does a post condition loop work

A

-initialize loop counter
-execute action
-update counter
-check condition
-if condition met, stop
else repeat

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

nested loops

A

-2 counters for inner and outer loops
-outer loop only loops after inner loop is finished
eg.
FOR i ← 1 TO 10
action
FOR j ← 1 To 5
action
NEXT j
NEXT i

21
Q

loop/if with a list as its range

A

n= [“A”, “B”]
-for i in n:
-while i in n:
-if variable[i] in n:
note: i changes to elements in list, list can be any data type

Alter

22
Q

index values in list

A

-all char has an index value
-first element = 1 - positive
-space also has an index value

23
Q

finding length of characters

A

LENGTH(“hello”)
LENGTH(poop)

24
Q

finding substring of characters

A

SUBSTRING (“hello”, 1, 3)
SUBSTRING(poop, 1, 3)
-first parameter = string
-second parameter = position of starting character
-third parameter = length of wanted substring

25
uppercasing all string
UCASE("hello") UCASE(poop)
26
lowercasing all string
LCASE("hello") LCASE(poop)
27
extract a certain char from a list (string manipulation)
num = [6, 7, 8, 9] -print(num[ 0 ]) = 6 -print(num[ 0: ]) = 6, 7, 8, 9 -print(num[ 0:2]) = 6, 7 (index after : not included) -print(num[:2]) = 6, 7 (index after : not included) -print(num[-1]) = 9 -print(num[i:i+ n ]) = value with i index & n no.s after it alter
28
create a blank 1D array
DECLARE variable: ARRAY[1:5] OF DATA TYPE includes upper bound so 5 spots created
29
note: before making 2D array - make a table look alike of it mark row = 1, column = 2
30
order of row & column in 2D array when: 1)creating array 2)printing array 3)traversing mark row = 1, column = 2
1) row, column 1, 2 2) row, column 1, 2 3) row, column 1, 2
31
how to create empty 2D array
DECLARE variable: ARRAY [1:2, 1:3] OF DATA TYPE
32
how to use a 2D list with variables to print row or element
for a row: OUTPUT groceries [row] for a single element: OUTPUT groceries [row, column]
33
code for linear search
INPUT num Found ← FALSE counter ← 1 REPEAT IF num = list[counter] THEN Found ← TRUE ELSE counter ← counter + 1 ENDIF UNTIL Found OR counter>size IF found = TRUE THEN OUTPUT "found" ELSE OUTPUT "not found: ENDIF
34
Code for bubble sort
n ← LENGTH (array) FOR i ← 1 TO n-1 FOR j ← 1 TO n-1-i IF array [j]> array[j+1] THEN temp ← array[j] array[j] ← array[j+1] array[j+1] ← temp ENDIF NEXT j NEXT i
35
swap elements with a temporary variable
a ← 5 b ← 10 temp ← 0 ------------------------ temp ← a a ← b b ← temp
36
code for traversing - accessing individual elements in 2D array
groceries = [["apple", "orange", "banana", "coconut"], ["celery", "carrots", "potatoes"], ["chicken", "fish", "turkey"]] FOR r ← 1 TO 3 FOR c ← 1 TO 4 OUTPUT groceries [r:c}
37
code for sum of reach row in 2D array
FOR row sum ← 1 TO 10 sum ← 0 FOR column ← 1 TO 10 INPUT mark sum ← sum + array[row, column] NEXT j OUTPUT sum NEXT i
38
code for totaling
total ← 0 FOR count ← 1 To size INPUT mark total ← total + mark NEXT count
39
code for counting
count ← 0 FOR num ← 1 TO size INPUT mark count ← count + 1 NEXT num
40
code for finding min, max, avg
total ← 0 maxMark ← 0 minMark ← 100 FOR count ← 1 TO size INPUT mark IF mark> maxMark THEN maxMark ← mark ENDIF IF mark< minMark THEN minMark ← mark ENDIF total ← total + mark NEXT count avg ← total/ size -set initial min value as highest value -set initial max value as lowest value
41
code to put highest/ lowest/ avg value from 2D array in 1D array
DECLARE highest_store : ARRAY [1:3] OF INTEGER DECLARE salary : ARRAY [1:3, 1:4] OF INTEGER FOR r ← 1 TO 3 highest_salary ← salary [r:c] FOR c ← 1 TO 4 IF salary [r:c]> highest_salary THEN highest_salary ← salary [r:c] ENDIF highest_store [r] ← highest_salary NEXT c NEXT r -declare the first element in each row as highest salary & compare it with others -if others are bigger, highest salary is replaced by it
42
how to use procedure with parameters
PROCEDURE addition (a: INTERGER, b: INTERGER) DECLARE sum: INTERGER sum ← a + b OUTPUT sum ENDPROCEDURE CALL addition (1, 2) - 1 & 2 are arguments which are called -put OUTPUT within the procedure to return cuz procedure can't return by itself
43
how to use procedure without parameters
PROCEDURE default CALL LINE (60) END PROCEDURE
44
how to use function with parameters
FUNCTION addition (a, b) RETURNS INTEGER DECLARE sum: INTEGER sum ← a + b RETURN sum ENDFUNCTION OUTPUT addition(1, 2) - 1 & 2 are arguments which are called -put OUTPUT outside the function cuz value is already returned
45
file
-simple text file which can be opened, read from & written to -has ___.txt
46
file operations command words
OPENFILE - opens file CLOSEFILE - closes file READ - opens an existing file WRITE - opens/ creates a blank file -never WRITE a file with data as it erases everything
47
example of file operations
OPENFILE store.txt FOR WRITE WRITEFILE store.txt, "KISC" CLOSEFILE store.txt -KISC is written in store.txt OPENFILE store.txt FOR READ READFILE store.txt, poop OUTPUT poop CLOSEFILE store.txt -the variable poop stores the first line of data from store.txt & is outputted
48
transferring data btw files using file operations
DECLARE extracted: STRING OPENFILE store.txt FOR WRITE OPEN FILE animals.txt FOR READ READ animals.txt, extracted WRITE store.txt, extracted CLOSEFILE animals.txt CLOSEFILE store.txt