Answered step by step
Verified Expert Solution
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:: attempt to index field a nil value
stack traceback:
exam.lua:: in main chunk
C:
lua: main.lua:: attempt to index field a nil value
stack traceback:
main.lua:: in main chunk
C:
lua: exam.lua:: attempt to index local 'city' a number value
stack traceback:
exam.lua:: in main chunk
C:
lua: exam.lua:: attempt to index field a nil value
stack traceback:
exam.lua:: in main chunk
C:
lua: exam.lua:: attempt to call global 'match' a nil value
stack traceback:
exam.lua:: in main chunk
C:
function loadgridfilename
local file ioopenfilename r
if not file then
printError: Unable to open file"
return
end
local grid
for line in file:lines do
local row
for num in line:gmatchS do
table.insertrow tonumbernum
end
table.insertgrid row
end
file:close
return grid
end
function areneighbourscity city grid
if city and city #grid and city and city #gridcity then
return gridcitycity
else
return false
end
end
function ispathgrid cities
for i #cities do
if gridcitiesicitiesi then
return false
end
end
return true
end
function pathsfromtocity city grid
end
function mostconnectedgrid
end
local grid loadgridgridtxt
while true do
local command ioreadline
if command "areneighnours" then
local city city ioreadnn
local result areneighbourscity city grid and "Yes" or No
printresult
elseif command "ispath" then
local cities
for city in ioread:gmatchS do
table.insertcities tonumbercity
end
if ispathgrid cities then
printYes
else
printNo
end
elseif command "pathlength" then
local totaldistance
local city tonumberioreadnumber
while city do
totaldistance totaldistance gridcitycity
city tonumberioreadnumber
end
printtotaldistance
elseif command "listneighbours" then
local city tonumberioreadnumber
local neighbour
for i #grid do
if gridcityi ~ then
table.insertneighbours i
end
end
table.sortneighbours in ascending order
for neighbour in ipairsneighbours do
printneighbour
end
elseif command "largestdistance" then
local city tonumberioreadnumber
local largestdistance
for i #grid do
largestdistance math.maxlargestdistance, gridcityi
end
elseif command "isvalid" then
local issymmetric true
for i #grid do
for j i #grid do
if gridij ~ gridji then
issymmetric false
break
end
end
end
printissymmetric and "Yes" or No
elseif command "pathsfromto" then
local startcity endcity tonumberioreadmatchSsS
printpathsfromtotonumberstartcity tonumberendcity grid
elseif command "mostconnected" then
mostconnectedgrid
printmostconnected
elseif command "exit" then
break
end
end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started