Question: 1 9 : 5 4 exam _ 2 4 . 1 1 . 2 0 2 3 1 8 0 1 2 0 2 4

19:54
exam_24.11.202318012024
--Open the file
function readGrid(filename)
local file=io.open("grid.txt","r")
if not file then
print("File not found")
return nil
end
--Read the grid of numbers from a file
local grid ={}
local dimensions= file:read("*line")
local rows, cols = dimensions:match ((%d+)%s+(%d+))
rows, cols = tonumber(rows), tonumber(cols)
for i=1, rows do
grid [i]={}
for j=1, cols do
--Iniciate the grid with a default value
grid[i][j]=-1
end
end
for line in file:lines() do
local row, col, val = line:match (:(%d+)''}
row,col,val = tonumber(row), tonumber(col),
tonumber(val)
if row and col and val then
if grid[row] and grid[col] then
grid [row][col]= val -- Value in grid update
end
end
end
--Close the file
file:close ()
return grid
end
--Function to show values in a specific column of grid function showColumn(grid, col)
if col1 or col > #grid [1] then
nrint ("Invalid column numher")
 19:54 exam_24.11.202318012024 --Open the file function readGrid(filename) local file=io.open("grid.txt","r") if not

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!