Week 20

Source Code

-- define main function
function main()
    print("Main Function")
    local done = false
    local choice = nil
    while not done do
        print("Menu")
        print("E1 - Example 1")
        print("Q - Quit")
        io.write("Choice: ")
        choice = io.read()
        if choice == "E1" then
            -- call Example 1 Function
            

        elseif choice == "E2" then
            -- call Example 2 Function

        elseif choice == "E3" then
            -- call Example 3 Function
            

        elseif choice == "E4" then
            -- call Linear Search


        elseif choice == "E5" then
            -- call Binary Search


        elseif choice == "Q" then
            done = true
        else
            print("Invalid Choice")
        end
    end
end

-- define Example 1 Function


-- define Example 2 Function


-- define Example 3 Function


-- define Linear Search Function


-- define Binary Search Function
function binarySearch(array, target)
    local low = 1
    local high = #array

    while low <= high do
        local mid = (low + high) // 2 -- midpoint
        local guess = array[mid]

        if guess == target then
            return mid -- target found
        elseif guess > target then
            high = mid - 1
        else
            low = mid + 1
        end
    end

    return -1
end

-- call main function
main()

Last updated