Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write Program in C ***Any help at all is appreciated*** Part A: Two-Dimensional, Polymorphic, Unboxed Arrays Hanson provides an abstraction that implements unboxed one-dimensional arrays.
Write Program in C
***Any help at all is appreciated***
Part A: Two-Dimensional, Polymorphic, Unboxed Arrays Hanson provides an abstraction that implements unboxed one-dimensional arrays. For this part of the assignment, you'll adapt the unboxed-array abstraction to support two-dimensional arrays. Your adaptation will be called UArray2 and should define the type UArray2_T. Your adaptation should include the following changes when compared to an Array: Instead of a length, an UArray2 T will have a width and a height. Instead of being identified by a single index, an element of an UArray2_T will be identified by two indices: the column or x index measures the distance between the element and the left edge of the array, while the row or y index measures the distance between the element and the top row of the array. Thus the top left element is always indexed by the pair (0,0). Omit UArray2_put. Implement UArray2_at which is analogous to Array-get. Omit the resize and copy operations. You must define two analogs of the Bit_map function described on page 201: - UArray_map_row_major calls an apply function for each element in the array. Column indices vary more rapidly than row indices. - UArray2_map_col_major calls an apply function for each element in the array. Row indices vary more rapidly than column indices. The terms "row major" and "column major may be found in Bryant and O'Halloran as well as on Wikipedia. As in Hanson's code, a reference out of bounds should result in a checked run-time error. For part A, the problem you are to solve is define an interface and build an implementation for UArray2. My solution to this problem takes about 100 lines of C code. Hints: The key to this problem is to set up an implementation in which the elements of your two-dimensional array are in one-to-one correspondence with elements of one or more one-dimensional Array_Ts. The key question to answer is How do you relate each element in a two-dimensional array to a corresponding element in some one-dimensional array? If you have a precise answer to this question, the code is pretty easy to get right. If not, it's easy to get lost chasing pointers. . Don't worry about performance; aim for simplicity. If you feel compelled to worry about performance, you may make simple code improvements provided you justify them. Don't try anything radical; premature optimization is the root of much evil. Think carefully about what should be the prototype for the apply function passed to UArray2_map_col_major and UArray_map_row_major. The pixels in a portable gray map are stored in row-major order, so one way to test your UArray2 mapping functions is to write a simple program that reads and writes a graymap by calling UArray_map_row_major with a function argument that calls Pnmrdr_get from the Pnmrdr interface. If you compare results with diff -bu you should be able to get the same output as pnmtoplainpnm. If you read with UArray_map_col_major and write with UArray_map_row_major, you should be able to duplicate the effect of pnmflip -transpose. Unpleasantness to watch out for: . When working with void * pointers, it's easy to get confused about the correct number of levels of indirection. Draw diagrams. If you assign the result of Array_get(a, i) to a pointer p, it must be the case that sizeof(*p) == Array_size(a) Violating this specification results in an unchecked run-time error. Some- times, but not always, you can catch such errors using valgrind's default "memcheck tool". It's therefore good to check this property with an asser- tion, e.g., element *p = Array_at(a, i); assert (sizeof(*p) == Array_size(a)); *p = f(); Your life will be much easier if you follow the programming idioms for storing values into Hanson's arrays, which deal with most of these issues. Part A: Two-Dimensional, Polymorphic, Unboxed Arrays Hanson provides an abstraction that implements unboxed one-dimensional arrays. For this part of the assignment, you'll adapt the unboxed-array abstraction to support two-dimensional arrays. Your adaptation will be called UArray2 and should define the type UArray2_T. Your adaptation should include the following changes when compared to an Array: Instead of a length, an UArray2 T will have a width and a height. Instead of being identified by a single index, an element of an UArray2_T will be identified by two indices: the column or x index measures the distance between the element and the left edge of the array, while the row or y index measures the distance between the element and the top row of the array. Thus the top left element is always indexed by the pair (0,0). Omit UArray2_put. Implement UArray2_at which is analogous to Array-get. Omit the resize and copy operations. You must define two analogs of the Bit_map function described on page 201: - UArray_map_row_major calls an apply function for each element in the array. Column indices vary more rapidly than row indices. - UArray2_map_col_major calls an apply function for each element in the array. Row indices vary more rapidly than column indices. The terms "row major" and "column major may be found in Bryant and O'Halloran as well as on Wikipedia. As in Hanson's code, a reference out of bounds should result in a checked run-time error. For part A, the problem you are to solve is define an interface and build an implementation for UArray2. My solution to this problem takes about 100 lines of C code. Hints: The key to this problem is to set up an implementation in which the elements of your two-dimensional array are in one-to-one correspondence with elements of one or more one-dimensional Array_Ts. The key question to answer is How do you relate each element in a two-dimensional array to a corresponding element in some one-dimensional array? If you have a precise answer to this question, the code is pretty easy to get right. If not, it's easy to get lost chasing pointers. . Don't worry about performance; aim for simplicity. If you feel compelled to worry about performance, you may make simple code improvements provided you justify them. Don't try anything radical; premature optimization is the root of much evil. Think carefully about what should be the prototype for the apply function passed to UArray2_map_col_major and UArray_map_row_major. The pixels in a portable gray map are stored in row-major order, so one way to test your UArray2 mapping functions is to write a simple program that reads and writes a graymap by calling UArray_map_row_major with a function argument that calls Pnmrdr_get from the Pnmrdr interface. If you compare results with diff -bu you should be able to get the same output as pnmtoplainpnm. If you read with UArray_map_col_major and write with UArray_map_row_major, you should be able to duplicate the effect of pnmflip -transpose. Unpleasantness to watch out for: . When working with void * pointers, it's easy to get confused about the correct number of levels of indirection. Draw diagrams. If you assign the result of Array_get(a, i) to a pointer p, it must be the case that sizeof(*p) == Array_size(a) Violating this specification results in an unchecked run-time error. Some- times, but not always, you can catch such errors using valgrind's default "memcheck tool". It's therefore good to check this property with an asser- tion, e.g., element *p = Array_at(a, i); assert (sizeof(*p) == Array_size(a)); *p = f(); Your life will be much easier if you follow the programming idioms for storing values into Hanson's arrays, which deal with most of these issuesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started