Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

If i add the first line as suggested, i get the below error lua: main.lua: 1 : attempt to index global ' op ' (

If i add the first line as suggested, i get the below error
lua: main.lua:1: attempt to index global 'op'(a nil value)
stack traceback:
main.lua:1: in main chunk
[C]: ?
i also see these in my comments/dialog box on runtime
--- Program output ---
lua: main.lua:36: bad argument #2 to 'tonumber' (base out of range)
stack traceback:
[C]: in function 'tonumber'
main.lua:36: in main chunk
[C]: ?
Enter command:
so i have commented it out inorder to compile the program, however i am still not geting the correct program output.
just for reference, this is the grid which i am assuming is part of grid.txt. the 0-4 i am assuming is being omitted but i just used it as a point of reference for you
01234
00.00.20.40.50.3
10.20.00.00.70.8
20.40.00.00.60.0
30.50.70.60.00.9
40.30.80.00.90.0
For numbering purposes the rows and values are indexed on a 0-index basis (i.e. the
first row is row 0 and the first column is column 0)
can i please ask that you check the outputs before responding back, as this is the 6th iteration of the same question that i have been asking for assistance on. i am really appreciative of your help and guidance but i feel we are stuck on question1 and without me/us getting it correct, i am not sure how to even attempt the more complez questions being asked. i hope you can understand where i am coming from by requesting this and if possible you run the code in a compiler.
--local file = op.open(filename ,"r")
function loadgrid(filename)
local file = io.open("grid.txt","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("%d+") 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
local grid = loadgrid("grid.txt")
while true do
io.write("Enter command: ")
local command = io.read():lower()-- Convert to lowercase for case-insensitivity
if command == "areneighbours" then
local city1, city2= tonumber(io.read("*n")), tonumber(io.read("*n"))
if city1 and city2 then
print(areneighbours(city1, city2, grid))
else
print ("Invalid input for cities. Please enter valid numbers")
end
elseif command == "exit" then
break
else
print("Unknown command")
end
end
Enter command: areni eighbours
0
1
false
Enter command: Unknown command
Enter command: areneighbours
1
32
false
Enter command: Unknown command
Enter command:
THE EXPECTED RESULTS SHOULD BE AS below
Expected output on test data with input (0,1): Yes
Expected output on test data with input (1,2): No
Expected output on test data with input (3,4): Yes
Expected output on test data with input (0,4): Yes

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

Moving Objects Databases

Authors: Ralf Hartmut Güting, Markus Schneider

1st Edition

0120887991, 978-0120887996

More Books

Students also viewed these Databases questions

Question

For what value of is the following equation true? lim

Answered: 1 week ago

Question

Discuss the legal framework of HRM in Canada.

Answered: 1 week ago