Answered step by step
Verified Expert Solution
Question
1 Approved Answer
i followed your suggestions but down to two sets of errors left, are you able to assist? i followed all your advice and even used
i followed your suggestions but down to two sets of errors left, are you able to assist?
i followed all your advice and even used a text comparator but still have these two issues,can you please check to see if i am missing something in my Lua progran source code
lua: main.lua:: attempt to get length of field a nil value
stack traceback:
main.lua:: in function 'listneighbours'
main.lua:: in main chunk
C:
lua: main.lua:: attempt to get length of field a nil value
stack traceback:
main.lua:: in function 'largestdistance'
load a grid from a file
function loadgridfilename
local file ioopenfilenamer
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
check to see if they are neighbouring cities
function areneighbourscity city grid
if city and city #grid and city and city #gridcity then
return gridcitycity
else
return false
end
end
check to see if it forms a path
function ispathgrid cities
for i #cities do
if gridcitiesicitiesi then
return false
end
end
return true
end
determine the distance of a path
function pathlengthgrid cities
local totaldistance
for i #cities do
totaldistance totaldistance gridcitiesicitiesi
end
printtotaldistance
end
find the neighbour of a given city
function listneighbourscity grid
local neighbours
for i #gridcity do
if gridcityi ~ then
table.insertneighbours i
end
end
table.sortneighbours
for neighbour in ipairsneighbours do
printneighbour
end
end
determine largest distance from a city
function largestdistancecity grid
local largestdistance
for i #gridcity do
largestdistance math.maxlargestdistance, gridcityi
end
printlargestdistance
end
check to see if grid is symmetrical
function isvalidgrid
local issymmetric true
for i #grid do
for j i #gridi do
if gridij ~ gridji then
issymmetric false
break
end
end
end
printissymmetric and "Yes" or No
end
functin to find paths from one city to another
function pathsfromtostartcity endcity, grid
end
function to find the most connected city
function mostconnectedgrid
end
main program
local grid loadgridgridtxt
while true do
local command ioreadline
if command "areneighbours" 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 cities
for city in ioread:gmatchS do
table.insertcities tonumbercity
end
pathlengthgrid cities
elseif command "listneighbours" then
local city tonumberioreadnumber
listneighbourscity grid
elseif command "largestdistance" then
local city tonumberioreadnumber
largestdistancecity grid
elseif command "isvalid" then
isvalidgrid
elseif command "pathsfromto" then
local startcity, endcity string.matchioreadlinedsd
pathsfromtotonumberstartcity tonumberendcity grid
elseif command "mostconnected" then
mostconnectedgrid
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