> For the complete documentation index, see [llms.txt](https://www.csprinciples.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.csprinciples.com/25-26/session-18.md).

# Session 18

## Source Code

```lua
local myName = ""
print("Name: " .. myName)

-- define main function
function main()
    print("Hello Main Function")
    print(myName)
    local choice = nil
    print("Menu")
    print("E1 - Example 1")
    print("Q - Quit")
    io.write("Choice: ")
    choice = io.read()
    if choice == "E1" then
        -- Example 1 Function call
        example1()

    elseif choice == "E2" then
        -- Example 2 Function call
        example2()

    elseif choice == "E3" then
        -- Example 3 Function call
        example3()

    elseif choice == "E4" then
        -- Example 4 Function call
        example4()

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

-- define Example 1 Function
function example1()
    print("Example 1")
    -- define friends list
    

    -- print the number of elements
   

    -- print the element one by one
    

    -- using a for loop, print the index and elements
    

    -- assign value to first friend
    

    -- assign value to the last friend
    

    -- print the friends
    

end

-- define Example 2 Function
function example2()
    -- create an empty list
    

    -- add an element to the end of the list (append)
    

    -- add an element to the end of the list (append)
    

    -- add an element to the beginning of the list (insert)
    

    -- print using for in loop
    

    -- print using for in loop with index
    

    -- delete a fruit
    

    -- print using for in loop
    

end

-- define Example 3 Function
function example3()
    
end

-- define Example 4 Function
function example4()
    
end

-- call to main function, do not delete!
main()
```

## s18problem1.lua

```lua
-- function definition
function problem1()

end

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

end

-- call to main function
main()

```

## s18problem2.lua

```lua
-- function definition
function problem2()

end

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

end

-- call to main function
main()

```

## s18problem3.lua

```lua
-- function definition
function problem3()

end

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

end

-- call to main function
main()

```

## s18problem4.lua

```lua
-- function definition
function problem4()

end

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

end

-- call to main function
main()

```

## s18problem5.lua

```lua
-- function definition
function problem5()

end

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

end

-- call to main function
main()

```

## s18problem6.lua

```lua
-- function definition
function problem6()

end

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

end

-- call to main function
main()

```

## s18problem7.lua

```lua
-- function definition
function problem7()

end

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

end

-- call to main function
main()
```

## s18problem8.lua

```lua
-- function definition
function problem8()

end

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

end

-- call to main function
main()
```

## s18problem9.lua

```lua
-- function definition
function problem9()

end

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

end

-- call to main function
main()
```

## playlist.lua

```lua
-- main function
function main()
    -- Initialize playlist with 10 songs
    local playlist = {}

    local done = false
    while not done do
        print("My Playlist Manager")
        print("1. View playlist")
        print("2. View playlist with index positions")
        print("3. Add a song")
        print("4. Remove a song")
        print("5. Select a random song")
        print("6. Sort playlist")
        print("7. Shuffle playlist")
        print("8. Reorder playlist")
        print("9. Clear playlist")
        print("10. Exit")
        io.write("Choose an option (1-10): ")
        local choice = io.read("*n")
        io.read()
        if choice == 1 then
            print("My current playlist")


        elseif choice == 2 then
            print("My current playlist with index positions")
            
            
        elseif choice == 3 then
           print("Add a song")


        elseif choice == 4 then
            print("Remove a song")


        elseif choice == 5 then
            print("Here's a random song!")
            

        elseif choice == 6 then
            print("Sort my playlist alphabetically")
            
            
        elseif choice == 7 then
            print("Shuffle playlist")
            -- Loop through the list from the last index to the second index. 

                -- Generate a random index between 1 and i
                
                -- Swap elements at index i and j
                


        elseif choice == 8 then
            print("Reorder playlist")
            

        elseif choice == 9 then
            print("Playlist cleared.")
            

        elseif choice == 10 then
            print("Goodbye!")
            done = true
        else
            print("Invalid choice. Please try again.")
        end
    end
end

-- Call the main function
main()
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.csprinciples.com/25-26/session-18.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
