Chapter 7 Algorithm Design And Problem Solving: Unit 7.4: Standard Methods Of Solution Flashcards
What are the 5 standard methods of solution?
Totalling
Counting
Finding the max, min and average (mean) values
Searching using a linear search
Sorting using a bubble sort
Describe totalling.
Keeping a total that values are added to.
Example:
Total <– 0
FOR Counter <– 1 TO ClassSize
Total <– Total + StudentMark[Counter]
NEXT Counter
Describe counting.
Keeping a count of the number of times an action is performed.
Example:
PassCount <– 0
FOR Counter <– 1 TO ClassSize
INPUT StudentMark
IF StudentMark > 50
THEN
PassCount <– PassCount + 1
NEXT Counter
Count <– Count + 1
Can also be used to count down.
Describe finding the max, min and average (mean) values.
Finding the largest and smallest values in a list.
Example:
MaximumMark <– 0
MinimumMark <– 100
FOR Counter <– 1 TO ClassSize
IF StudentMark[Counter] > MaximumMark
THEN
MaximumMark <– StudentMark[Counter]
ENDIF
IF StudentMark[Counter] < MinimumMark
THEN
MinimumMark <– StudentMark[Counter]
ENDIF
NEXT Counter
Calculating the average (mean) of all values in a list is an extension of the totalling method.
Example:
Total <– 0
FOR Counter <– 1 TO ClassSize
Total <– Total + StudentMark[Counter]
NEXT Counter
Average <– Total / ClassSize
Describe linear search.
A search is used to check if a value is stored in a list, performed by systematically working through the items in the list. Linear search inspects each item in a list in turn to see if the item matches the value searched for.
Example:
OUTPUT “Please enter name to find “
INPUT Name
Found <– FALSE
Counter <– 1
REPEAT
IF Name = StudentName[Counter]
THEN
Found <– TRUE
ELSE
Counter <– Counter + 1
ENDIF
UNTIL Found OR Counter > ClassSize
IF Found
THEN OUTPUT Name, “ found at position “, Counter, “ in the list.”
ELSE
OUTPUT Name, “ not found.”
ENDIF
Describe bubble sort.
Each element is compared with the next element and swapped if the elements are in the wrong order, starting from the first element and finishing with the next-to-last element.
Example:
First <– 1
Last <– 10
REPEAT
Swap <– FALSE
FOR Index <– First TO Last - 1
IF Temperature[Index] > Temperature[Index + 1]
THEN
Temp <– Temperature[Index]
Temperature[Index] <– Temperature[Index + 1]
Temperature[Index + 1] <– Temp
Swap <– TRUE
ENDIF
NEXT Index
Last <– Last - 1
UNTIL (NOT Swap) OR Last = 1