Homework Assignment 2 CS 340d Unique Number: 50960 Spring, 2019 Given: February 4, 2019 Due: February 13, 2019 This homework concerns C-language types. Below are some diagrams, you must define a corresponding C-language type declaration. Later there are some problems about type specifications. Remember to respect the organization of the fields within the structures -- lower addresses are lower down on the page. For problems 1, 2, 3, 4, and 5, write down a C type specification for the data structure shown or described. Abbreviations used: c - char s - short w - int l - long p - pointer # - no specification <- 16 -> 1. +---+---+ | s | n +---+---+ ... +---+---+ | s | 1 +---+---+ | s | 0 +---+---+ Assume n has been defined using #define macro. <------------ 64 bits ----------> 2. +---+---+---+---+---+---+---+---+ | l | +---+---+---+---+---+---+---+---+ | c | # | s | w | +---+---+---+---+---+---+---+---+ | p --> l | +---+---+---+---+---+---+---+---+ 3. Define a type for a pointer to an array of elements of the type in 2. 4. Define a type for a linked-list type of integers. 5. <------------ 64 bits ----------> Field names +---+---+---+---+---+---+---+---+ | l | r +---+---+---+---+---+---+---+---+ | l | l +---+---+---+---+---+---+---+---+ | p | nx +---+---+---+---+---+---+---+---+ Define a type, using ``typedef'' named ``struct range'', of the structure above, where ``p'' is a pointer to the type of the object above. When defining such a structure, name it ``range'' with field names ``nx'', ``l'', and ``r'' -- and finally, with (type) name ``range_t''. 6. Given: int a[] = { 3, 5, 8, 4, 2, 6, 7 }; what is: a[*a + *(a + 4)] == ? 7. What is the difference between type definitions a and b (just below): a. int (*months)[12] b. int *months[12] c. Draw a diagram that exhibits the two types above. 8. Diagram (meaning draw a diagram like in problem 2, of the following types: a: char *x b: char *x() c: char (*x()) d: char (*x())[] e: char (*(*x())[]) f: char (*(*x())[])() 9. Is a freebie... 10. Define a subroutine that takes two ``long'' integer arguments, and uses ``malloc'' to allocate space for one structured object of the kind in problem 5 (above). The ``p'' field should be set to NULL, and this subroutine should return the address of the structure allocated. The problems above are each worth one point. The next problem is worth five points. 11. Consider a representation of a set that involves ranges of values on the number line. Our number line admits ranges from -2^63 to 2^63-1, inclusive. Here is an example representation for a set that includes a range from -20 to -10 (inclusive), 7, 9 to 11 (inclusive). <------------ 64 bits ----------> Field names +---+---+---+---+---+---+---+---+ | 11 | r +---+---+---+---+---+---+---+---+ | 9 | l +---+---+---+---+---+---+---+---+ | NULL | nx +---+---+---+---+---+---+---+---+ 7 6 5 4 3 2 1 0 byte offset ^ | +-------+ | +---+---+---+---+---+---+---+---+ | | 7 | | +---+---+---+---+---+---+---+---+ | | 7 | | +---+---+---+---+---+---+---+---+ | | * >------------ | >-----+ +---+---+---+---+---+---+---+---+ ^ | +-------+ | +---+---+---+---+---+---+---+---+ | | -10 | | +---+---+---+---+---+---+---+---+ | | -20 | | +---+---+---+---+---+---+---+---+ | | * >------------ | >-----+ +---+---+---+---+---+---+---+---+ ^ | Beginning of list >--------+ Given a list of pairs of numbers (each appearing on a separate line), create an ordered, compressed representation for the specified set. For example, the set above might have been specified (in a file) as: -14 -10 7 7 11 10 9 10 -20 -12 Write a program that reads such a file and stores these contents as a linked list where each ``struct range'' is created through a call of ``malloc''. After reading the input file, your program should "clean" the data structure so that it is ordered and so there are no range overlaps (between input pairs).. The program takes one command line argument -- the file name.