Problem 4 30 Points Dynamic Allocation O Prof Lumetta Needs Help Function Designed Read Tw Q43822967
please, explain for each blank
Problem 4 (30 points): Dynamic Allocation and I’O Prof. Lumetta needs your help with a function designed to read a two-dimensional array of integers (called a “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 46 -2 4 1 200 42 0 1 2 72 4 Page 7 ProblemA, fondnued: int* readIntMatrix (FILE f, int heightPtr, int widthPtr) char buf(100]; int m; nElts; i; int int if (NULL =-f I| NULL = heightPtr || NULL == widthPtr) { return NULL; if (NULL = fgets (buf, 100, f) || “, heightPtr, widthPtr) |I 2 !- sscanf (buf, ” 0 > *heightPtr || 100 < *heightPtr II 0 > *widthPtr I| 100 < *widthPtr) { return NULL: nElts – * nElts): m – malloc ( if ( return NULL: for (i – 0; nElts > i; i++) { if (1 !- fscanf (f, “id”, Page 6 Problem 4 (30 points): Dynamic Allocation and I/O Prof. Lumetta needs your help with a function designed to read a two-dimensional array of integers (called a “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 I 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 £, 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. 3 5 123 4 6 -2 41 200 42 0 12 72 4 Problem 4, continued: int readIntMatrix (FILE £, int heightPtr, int widthPtr) char buf(100]: int m; int nElts: int i: if (NULL – f || NULL – heightPtr || NULL — widthPtr) ( return NULL: if (NULL – fgets (buf, 100, £) 11 2 !- sscanf (buf, “, heightPtr, widthPtr) II 0 > *heightPtr || 100 < *heightPtrII 0 > *widthPtr I| 100 < *widthPtr) { return NULL; nElts m – malloc ( nElts); if ( return NULL: for (i – 0; nElts > i; i++) { if (1 !- fscanf (f, “d”, Show transcribed image text Problem 4 (30 points): Dynamic Allocation and I’O Prof. Lumetta needs your help with a function designed to read a two-dimensional array of integers (called a “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 46 -2 4 1 200 42 0 1 2 72 4
Page 7 ProblemA, fondnued: int* readIntMatrix (FILE f, int heightPtr, int widthPtr) char buf(100]; int m; nElts; i; int int if (NULL =-f I| NULL = heightPtr || NULL == widthPtr) { return NULL; if (NULL = fgets (buf, 100, f) || “, heightPtr, widthPtr) |I 2 !- sscanf (buf, ” 0 > *heightPtr || 100 *widthPtr I| 100 i; i++) { if (1 !- fscanf (f, “id”,
Page 6 Problem 4 (30 points): Dynamic Allocation and I/O Prof. Lumetta needs your help with a function designed to read a two-dimensional array of integers (called a “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 I 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 £, 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. 3 5 123 4 6 -2 41 200 42 0 12 72 4
Problem 4, continued: int readIntMatrix (FILE £, int heightPtr, int widthPtr) char buf(100]: int m; int nElts: int i: if (NULL – f || NULL – heightPtr || NULL — widthPtr) ( return NULL: if (NULL – fgets (buf, 100, £) 11 2 !- sscanf (buf, “, heightPtr, widthPtr) II 0 > *heightPtr || 100 *widthPtr I| 100 i; i++) { if (1 !- fscanf (f, “d”,
Expert Answer
Answer to Problem 4 (30 points): Dynamic Allocation and I’O Prof. Lumetta needs your help with a function designed to read a two-d…
OR