Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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")
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

Database Concepts

Authors: David Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions