Question: C language. int readPuzzle(const char *filename, int **puzzle, int **constraints, int *size); The job of this function is to (a) open the file, (b) read
C language.
int readPuzzle(const char *filename, int **puzzle, int **constraints, int *size);
The job of this function is to (a) open the file, (b) read the puzzle description and create the
data structures, (c) use the pointer arguments to return those data structure to the caller, and (d) close the file. The return value is 1 if the file was read successfully, and 0 otherwise. For this assignment, you should assume that the formatting of the file is always correct. You dont have to check for errors in how the puzzle is represented. Therefore, the only error that you might see is a failure to open the file. A brief discussion of file I/O is in the Appendix, but we will certainly discuss this in class well before the program is due. The caller provides pointers that readPuzzle uses to communicate the new data structures and size information. There are three pieces of information needed: size is the size of the puzzle. The value is an integer, so the caller provides a pointer to an integer variable, where the value must be stored. puzzle is the puzzle box array, described above. Since the size of the array is not known until the file is read, it is the job of readPuzzle to dynamically allocate the array. (Use malloc -- see the Appendix.) What you end up with is a pointer to the first array element. The caller provides a pointer to an int* variable (hence the type int** -- pointer to an int pointer), where the pointer must be stored. The array must contain zeroes for blank boxes, and numbers for any boxes specified by the file. constraints is the puzzle constraints array, described above. This also needs to be dynamically allocated and stored using the int** provided by the caller. The array must contain zeroes for boxes with no constraints, and the encoded constraints for those boxes for which relational constraints are specified in the file.
The text file:
4 |- - - -| | v| |- - - -| |^ ^ | |- - 2 -| | | |->- - 3|
We are going to use a text file, that just contains ASCII characters, to represent the puzzle. For each grid row, we will have two rows of text. One row contains the content of the boxes (number or blank), as well as relationships between boxes in the row. The next row contains relationships between boxes in this row and the row below. The description below refers to the puzzle shown earlier in the figure. The first line of the file contains a number, between 4 and 9, which is the size of the puzzle. The next line is the representation of the first row of boxes. We start and end the line with a vertical bar (|) -- this helps verify consistent spacing between the rows. For each box, we put either a digit (e.g., 1) or a hyphen (-) if the box is blank. Next is either an empty space, if there is no relation specified, 3 or a greater-than (>) or less-than (<). This repeats for each box in the row. Note that there is no space or symbol after the last box. Next comes a row that contains only the vertical relationships between boxes on different rows. There are no hyphens, just spaces. The carat (^) represents that the upper box is less than the lower box, and the lower-case v indicates that the upper box is greater than the lower box. (Look at it sideways!) This repeats for each row in the puzzle. For the final row, there is only the row of boxes, because there cant be a relation specified for a lower box.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
