Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please explain Page 6 Problem 4 (30 points): Dynamic Allocation and I/O Prot. Lumetta needs your help with a function designed to read a two-dimensional

image text in transcribed
image text in transcribed
please explain
Page 6 Problem 4 (30 points): Dynamic Allocation and I/O Prot. Lumetta needs your help with a function designed to read a two-dimensional array of integers (called "matrix") from a file. The height and width of the matrix are stored on the first line of the file, followed by the (height x width) elements of the matrix. The function must read the height and width, check that they are valid (from 1 to 100 each), allocate space for the array, then read all elements of the matrix from the file into the dynamically-allocated array. If anything goes wrong while reading the matrix from the file f, the function must free any dynamically allocated memory and return NULL. The function also takes two other parameters, heightPtr and widthPtr. If the matrix is read successfully from the file, the function must write the height and width of the matrix to these pointers, respectively. Otherwise, the values pointed to by the pointers are not relevant (they can be changed, but they will be ignored by the caller). COMPLETE THE CODE on the next page by filling in the blanks appropriately. An example of the matrix file format appears below. 35 1 2 3 4 6 -2 4 1 200 42 0 1 2 724 Problem 4, continued: int readintMatrix (FILE: fint height Ptr. int. widthPtr char buf(100); intmi int nElts: int i; if (NULL - f 11 NULL = heightPtr 11 NULL = widthPtr) { return NULL; if (NULL -- fgets (buf, 100, f) 11 2 !- sscanf (buf," " heightPtr, widthPtr) II 0 > "heightPtr II 100 *widthPtr 11 100 i; i++) { if (1 !- fscanf (f, "d

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

Students also viewed these Databases questions