Algorithms Flashcards
What cannot be divided?
Simple Variables
What is a top level (main) module do?
Expresses the task
What way do you get to the next “if’ statement?
If the first expression is not true.
What are the two basic loops?
Count Controlled and Event controlled
What is a count-controlled loop?
A loop that repeats itself a specific number of times.
what are the three distinct parts to the count-Controlled loop?
Initialization. Testing. Incrimination.
What is called a pretest loop?
While Loop
What is a loop that never terminates?
An infinity loop
What is an event-controlled loop?
Loops where the number of repetitions is controlled by an event that occurs with the body of the loop itself
When implementing an event-controlled loop using the While statement, what are the three parts to this?
Initialized. Tested. Updated
What is an algorithm with selections?
A pseudocode with selections to choose from
Read this pseudocode
If (temperature > 90)
Write “Texas weather: Wear shorts”
ELSE IF (temperature > 70)
Write “ideal weather: short sleeves are fine”
ELSE IF (temperature > 50)
Write “A little chilly: wear a light jacket”
ELSE IF (temperature > 32)
Write “Philadelphia weather: wear a heavy coat”
ELSE
Write “Stay inside”
If the temperature is above 90, it will tell you that it is Texas weather to wear shorts.
If the temperature is above 70 but below 90, it will tell you that it is ideal weather and it is ok to wear short sleeves.
If the temperature is above 50 but below 70, it will tell you that it is chilly and to wear a light jacket.
If the temperature is above 32 but less than 50, it will tell you that it is Philadelphia weather and to wear a heavy coat.
If none of these are true, it will tell you to stay inside.
What is the result of this code?
IF (myNum MOD 2 == 0)
Print “Even”
ELSE
Print “Odd”
Checks if a number (myNum) is even or odd, by using the modulo or odd. If the remainder when dividing by 2 is zero, the number is even, otherwise its odd
What is the result of this code?
IF (num1 > num2)
Print “num1 is greater”
ELSE
Print “num2 is greater”
Compares two numbers (num1 and num2) and prints which one is greater
What is the result of this code?
Set factorial to 1
Set counter to 1
WHILE (counter <= n)
Set factorial to factorial * counter
Set counter to counter + 1
Print “The factorial is “, factorial
Calculates the factorial of a number (n) by multiplying all integers from 1 to n
What is the result of this code?
Set sum to 0
FOR each element in array
Set sum to sum + element
Print “The sum of the array is “, sum
sums all the elements in an array and prints the results
What is the result of this code?
Set max to array[0]
FOR each element in array
IF (element > max)
Set max to element
Print “The largest number is “, max
Finds the largest number in the array by iterating through each element and updating the max variable if a larger number is found.