Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please let me know if the image assisat to larify all your questions i followed your suggestions but down to two sets of errors left,

Please let me know if the image assisat to larify all your questions
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:54: attempt to get length of field '?'(a nil value)
stack traceback:
main.lua:54: in function 'listneighbours'
main.lua:127: in main chunk
[C]: ?
lua: main.lua:68: attempt to get length of field '?'(a nil value)
stack traceback:
main.lua:68: in function 'largestdistance'
--load a grid from a file
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
--check to see if they are neighbouring cities
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
--check to see if it forms a path
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
--determine the distance of a path
function pathlength(grid, cities)
local total_distance =0
for i =1, #cities -1 do
total_distance = total_distance + grid[cities][i][cities[i +1]]
end
print(total_distance)
end
--find the neighbour of a given city
function listneighbours(city , grid)
local neighbours ={}
for i =1, #grid[city] do
if grid[city][i] ~=0 then
table.insert(neighbours, i -1)
end
end
table.sort(neighbours)
for _, neighbour in ipairs(neighbours) do
print(neighbour)
end
end
--determine largest distance from a city
function largestdistance(city, grid)
local largest_distance =0
for i =1, #grid[city] do
largest_distance = math.max(largest_distance, grid[city][i])
end
print(largest_distance)
end
--check to see if grid is symmetrical
function isvalid(grid)
local is_symmetric = true
for i =1, #grid do
for j = i +1, #grid[i] do
if grid[i][j] ~= grid[j][i] then
is_symmetric = false
break
end
end
end
print(is_symmetric and "Yes" or "No")
end
--functin to find paths from one city to another
function pathsfromto(startcity, endcity, grid)
--
end
--function to find the most connected city
function mostconnected(grid)
--
end
--main program
local grid = loadgrid("grid.txt")
while true do
local command = io.read("*line")
if command == "areneighbours" 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 cities ={}
for city in io.read():gmatch("%S+") do
table.insert(cities, tonumber(city))
end
pathlength(grid, cities)
elseif command == "listneighbours" then
local city = tonumber(io.read("*number"))
listneighbours(city, grid)
elseif command == "largestdistance" then
local city = tonumber(io.read("*number"))
largestdistance(city, grid)
elseif command == "isvalid" then
isvalid(grid)
elseif command == "pathsfromto" then
local startcity, endcity = string.match(io.read("*line"),"(%d+)%s+(%d+)")
pathsfromto(tonumber(startcity), tonumber(endcity), grid)
elseif command == "mostconnected" then
mostconnected(grid)
elseif command == "exit" then
break
end
end
\table[[,0,1,2,3,4
image text in transcribed

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

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions