Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please assist me to debug the following errors i am having with my very first Lua program and let me know you are

Can you please assist me to debug the following errors i am having with my very first Lua program and let me know you are to successfully compile this program on your side?
i would greatly appreciate any help and assistance you can provide me with to correct the below issues.
lua: exam.lua:70: attempt to index field '?'(a nil value)
stack traceback:
exam.lua:70: in main chunk
[C]: ?
lua: main.lua:78: attempt to index field '?'(a nil value)
stack traceback:
main.lua:78: in main chunk
[C]: ?
lua: exam.lua:90: attempt to index local 'city' (a number value)
stack traceback:
exam.lua:90: in main chunk
[C]: ?
lua: exam.lua:96: attempt to index field '?'(a nil value)
stack traceback:
exam.lua:96: in main chunk
[C]: ?
lua: exam.lua:104: attempt to call global 'match' (a nil value)
stack traceback:
exam.lua:104: in main chunk
[C]: ?
---
function loadgrid(filename)
local file = io.open(filename ,"r")
if not file then
print("Error: Unable to open file")
return
end
local grid ={}
for line in file:lines() do
local row ={}
for num in line:gmatch("%S+") do
table.insert(row, tonumber(num))
end
table.insert(grid, row)
end
file:close()
return grid
end
function areneighbours(city1, city2, grid)
if city1>=0 and city1< #grid and city2>=0 and city2< #grid[city1+1] then
return grid[city1+1][city2+1]>0
else
return false
end
end
function ispath(grid, cities)
for i =1, #cities -1 do
if grid[cities[i]][cities[i+1]]==0 then
return false
end
end
return true
end
function pathsfromto(city1, city2, grid)
--
end
function mostconnected(grid)
--
end
local grid = loadgrid("grid.txt")
while true do
local command = io.read("*line")
if command == "areneighnours" then
local city1, city2= io.read("*n","*n")
local result = areneighbours(city1, city2, grid) and "Yes" or "No"
print(result)
elseif command == "ispath" then
local cities ={}
for city in io.read():gmatch("%S+") do
table.insert(cities, tonumber(city))
end
if ispath(grid, cities) then
print("Yes")
else
print("No")
end
elseif command == "pathlength" then
local total_distance =0
local city = tonumber(io.read("*number"))
while city >=0 do
total_distance = total_distance + grid[city][city +1]
city = tonumber(io.read("*number"))
end
print(total_distance)
elseif command == "listneighbours" then
local city = tonumber(io.read("*number"))
local neighbour ={}
for i =0, #grid -1 do
if grid[city][i] ~=0 then
table.insert(neighbours, i)
end
end
table.sort(neighbours)-- in ascending order
for _, neighbour in ipairs(neighbours) do
print(neighbour)
end
elseif command == "largestdistance" then
local city = tonumber(io.read("*number"))
local largest_distance =0
for i =0, #grid -1 do
largest_distance = math.max(largest_distance, grid[city[i]])
end
elseif command == "isvalid" then
local is_symmetric = true
for i =0, #grid -1 do
for j = i +1,#grid -1 do
if grid[i][j] ~= grid[j][i] then
is_symmetric = false
break
end
end
end
print(is_symmetric and "Yes" or "No")
elseif command == "pathsfromto" then
local startcity , endcity = tonumber(io.read(match("(%S+)%s*(%S+)")))
print(pathsfromto(tonumber(startcity), tonumber(endcity)), grid)
elseif command == "mostconnected" then
mostconnected(grid)
print(mostconnected)
elseif command == "exit" then
break
end
end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

In what ways do personal and social media change how we think?

Answered: 1 week ago

Question

How do virtual communities diff er from physical communities?

Answered: 1 week ago