✒️
Computer Science Principles
  • Introduction
  • Overview
  • Course at a Glance
  • Course Exam Description
  • Create Performance Task
  • Reference Sheet
  • Resources
  • Big Idea 1
    • 1.1 Collaboration
    • 1.2 Program Function and Purpose
    • 1.3 Program Design and Development
    • 1.4 Identifying and Correcting Errors
  • Big Idea 2
    • 2.1 Binary Numbers
    • 2.2 Data Compression
    • 2.3 Extracting Information from Data
    • 2.4 Using Programs with Data
  • Big Idea 3
    • 3.1 Variables and Assignments
    • 3.2 Data Abstraction
    • 3.3 Mathematical Expressions
    • 3.4 Strings
    • 3.5 Boolean Expression
    • 3.6 Conditionals
    • 3.7 Nested Conditionals
    • 3.8 Iteration
    • 3.9 Developing Algorithms
    • 3.10 Lists
    • 3.11 Binary Search
    • 3.12 Calling Procedures
    • 3.13 Developing Procedures
    • 3.14 Libraries
    • 3.15 Random Values
    • 3.16 Simulations
    • 3.17 Algorithmic Efficiency
    • 3.18 Undecidable Problems
  • Big Idea 4
    • 4.1 The Internet
    • 4.2 Fault Tolerant
    • 4.3 Parallel and Distributed Computing
  • Big Idea 5
    • 5.1 Beneficial and Harmful Effects
    • 5.2 Digital Divide
    • 5.3 Computing Bias
    • 5.4 Crowdsourcing
    • 5.5 Legal and Ethical Concerns
    • 5.6 Safe Computing
  • Code
    • Week 10
    • Week 11
    • Week 12
    • Week 13
    • Week 14
    • Week 15
    • Week 16
    • Week 17
    • Week 18
    • Week 19
    • Week 20
    • Week 21
    • Week 22
Powered by GitBook
On this page
  • Source Code
  • w19problem1.lua
  • w19problem2.lua
  • w19problem3.lua
  • w19problem4.lua
  • w19problem5.lua
  • w19problem6.lua
  • w19problem7.lua

Was this helpful?

Export as PDF
  1. Code

Week 19

Source Code

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

        -- After


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


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


    elseif choice == "E4" then
        -- Example 4 Function call


    elseif choice == "E5" then
        -- Example 5 Function call


    elseif choice == "E6" then
        -- Linear Search Function call
        

    elseif choice == "E7" then
        -- Binary Function call
        

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

-- define printList function


-- define Example 1 Function


-- define Example 2 Function


-- define Example 3 Function


-- define Example 4 Function


-- define Example 5 Function


-- define linearSearch Function


-- define binarySearch 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 to main function, do not delete!
main()

w19problem1.lua

-- function definition
function problem1()

end

-- function definition
function main()
    -- function call

end

-- call to main function
main()

w19problem2.lua

-- function definition
function problem2()

end

-- function definition
function main()
    -- function call

end

-- call to main function
main()

w19problem3.lua

-- function definition
function problem3()

end

-- function definition
function main()
    -- function call

end

-- call to main function
main()

w19problem4.lua

-- function definition
function problem4()

end

-- function definition
function main()
    -- function call

end

-- call to main function
main()

w19problem5.lua

-- function definition
function problem5()

end

-- function definition
function main()
    -- function call

end

-- call to main function
main()

w19problem6.lua

-- function definition
function problem6()

end

-- function definition
function main()
    -- function call

end

-- call to main function
main()

w19problem7.lua

-- function definition
function problem7()

end

-- function definition
function main()
    -- function call

end

-- call to main function
main()
PreviousWeek 18NextWeek 20

Last updated 3 months ago

Was this helpful?