The Basics

Cards (22)

  • Finding the max of two number?

    Start
    Input num1
    Input num2
    If num1 > num2 then
    Set max = num1
    else
    Set max = num2
    Display max
    Stop
  • Calculate the factorial of a given number?
    Start
    Input num
    Set factorial = 1
    set i = 1
    while i <= num do
    set factorial = factorial *i
    set i = i + 1
    display factorial
    stop
  • Determine wether a given number is prime or not?

    Start
    Input num
    Set is_prime = true
    If num <= 1 then
    set is_prime = false
    set i = 2
    while i * i <= num and is_prime - true do
    if num % i == 0 then
    set is_prime = false
    set i + i + 1
    If is_prime = true then
    PRINT(”is prime“
    else
    PRINT(It is not prime)
    stop
  • Arrays?
    // Declare an array of integers
    INTEGER_ARRAY = [5, 10, 3, 8, 15, 2]
    // Initialize variables to store maximum and minimum values
    MAX_VALUE = INTEGER_ARRAY[0]
    MIN_VALUE = INTEGER_ARRAY[0]
    // Iterate through the array to find maximum and minimum values
    FOR i = 1 to LENGTH(INTEGER_ARRAY) - 1 DO
    IF INTEGER_ARRAY[i] > MAX_VALUE THEN
    MAX_VALUE = INTEGER_ARRAY[i]
    ENDIF
    IF INTEGER_ARRAY[i] < MIN_VALUE THEN
    MIN_VALUE = INTEGER_ARRAY[i]
    ENDIF
    ENDFOR
    // Display the maximum and minimum values
    PRINT("Maximum value:", MAX_VALUE)
    PRINT("Minimum value:", MIN_VALUE)
  • Iteration?
    // Input a positive integer N
    N = INPUT("Enter a positive integer N")
    // Initialize a variable to store the sum
    SUM = 0
    // Iterate from 1 to N and add each number to the sum
    FOR i = 1 to N DO
    SUM = SUM + i
    ENDFOR
    // Display the sum
    PRINT("The sum of numbers from 1 to", N, "is:", SUM)
  • Function to calculate the area of a rectangle
    1. Calculate area
    2. Return area
  • Function to calculate the perimeter of a rectangle
    1. Calculate perimeter
    2. Return perimeter
  • Input length and width of the rectangle
    1. Enter the length of the rectangle
    2. Enter the width of the rectangle
  • Call functions to calculate area and perimeter
    1. CalculateArea(length, width)
    2. CalculatePerimeter(length, width)
  • Display results
    1. Print "The area of the rectangle is:", area
    2. Print "The perimeter of the rectangle is:", perimeter
  • Recursion?
    // Recursive function to calculate the factorial of a non-negative integer
    Function CalculateFactorial(n):
    If n equals 0 then
    Return 1 // Base case: factorial of 0 is 1
    Else
    Return n * CalculateFactorial(n - 1) // Recursive call
    EndIf
    // Input a non-negative integer
    num = INPUT("Enter a non-negative integer")
    // Call the recursive function to calculate factorial
    factorial = CalculateFactorial(num)
    // Display the factorial
    PRINT("The factorial of", num, "is:", factorial)
  • NUMBER_LIST
    An empty list to store numbers
  • CalculateAverage(numbers)
    1. Initialize total to 0
    2. For each number in numbers, add it to total
    3. Divide total by length of numbers to get average
    4. Return average
  • Inputting and adding numbers to NUMBER_LIST
    1. Input the number of elements in the list
    2. For i = 1 to num_elements, input number i and append it to NUMBER_LIST
  • Calculating and displaying the average
    1. Call CalculateAverage(NUMBER_LIST) to get the average
    2. Print the average
  • Bubble sort algorithm to sort an array of integers
    1. Determine length of array
    2. Iterate through array
    3. Compare adjacent elements
    4. Swap elements if in wrong order
  • Linear search algorithm to find a target value in an array of integers
    1. Determine length of array
    2. Iterate through array
    3. Check if current element matches target
    4. Return index if found
    5. Return -1 if not found
  • Example usage
    • arr = [5, 3, 8, 2, 1, 9]
    • target = 8
  • Sort the array using bubble sort
    BubbleSort(arr)
  • Search for the target value using linear search
    index = LinearSearch(arr, target)
  • Target value found
    Print "Target value [target] found at index [index]"
  • Target value not found
    Print "Target value [target] not found in the array"