C Programming Simple Array Processing This assignment consists of two parts. The first part focuses on array read accesses and computational logic. The second part focuses on array read/write access and a bit of integer data representation. Q1. For this question, you will implement the following C function: / Given an array of nonnegative integers, a mesa is a sequence of 5 or more identical values. The length of a mesa is the number of values in the sequence, and the mass of a mesa is the sum of the values in the sequence. bigmesa() determines the mesa of maximum mass in the array that is passed into it. bigmesa() reports its results by setting three extern variables: mesastart, mesaend and mesamass. Pre: A[0:A_Size - 1] are nonnegative integers. Post: mesastart = index of first element of most massive mesa mesaend = index of last element of most massive mesa mesamass = sum of elements in most massive mesa Restrictions: You may not use a second array. / void bigmesa(int A[], int A_Size); Begin by downloading the posted files: driver.c bigmesa.h bigmesa.c sample test driver; modify as you like C header file for compiling with test driver --- do not modify! C source file for implementation of bigmesa() function You can compile these files by using the following command in a Linux shell: gcc o driver std=c99 -Wall driver.c bigmesa.c Using this command with the supplied files will yield an executable named driver. You can execute the testing code by using the following command in a Linux shell:./driver (If you've added the current directory to your Linux path, you can omit the "./" from the command.) Although the given code will compile correctly, the resulting program will not satisfy the requirements of the assignment, since the supplied implementation of the required function doesn't do anything. So, you must correctly complete the implementation of the function. You may, and should, modify the supplied testing code since it's not exactly thorough. But, if your solutions don't compile with the supplied testing code, they won't compile with the real testing code either. It's not likely, but you may need to add include directives to your.c file, as needed for any C Standard Library features you use. You may write secondary "helper" functions if you like; if so, those must be defined and declared within the supplied bigmesa.c file. Such functions should be declared as static. 1
Here are some samples of test data and correct results. The first row shows array indices, the second shows the array contents, and the values that should be set by bigmesa() are shown after that. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 33 28 28 96 96 96 96 96 77 77 77 77 77 77 77 77 77 77 77 77 31 31 31 27 74 56 56 56 51 51 mesastart: 8 mesaend: 19 mesamass: 924 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 91 91 91 91 15 15 15 15 15 15 44 44 44 1 1 1 1 1 1 1 1 1 1 1 1 1 1 mesastart: 4 mesaend: 9 mesamass: 90 Do not consider the test data above to be comprehensive, but it does illustrate some of the logic issues that arise in this assignment. What to Submit You will submit your modified version of the file bigmesa.c to the Curator, via the collection point HW05Q1. This assignment will be graded automatically. You will be allowed up to ten submissions for this assignment, so use them wisely. Test your programs thoroughly before submitting them. Make sure that your programs produce correct results for every logically valid test case you can think of. Do not waste submissions on untested code, or on code that does not compile with the supplied code from the course website. The Curator will assign a score based on runtime testing of your submission; your best score will be counted; the TAs will later verify that your best submission meets the stated restrictions, and assess penalties if not. The Student Guide and other pertinent information, such as the link to the proper submit page, can be found at: http:www.cs.vt.edu/curator/ 2
Q2. For this question, you will implement part of a C library to emulate basic hardware operations on signed 32-bit integer values. You should begin by reviewing your notes on integer data representation. For this question, we will use 32-element arrays of uint8_t values to represent 32-bit signed integers: integer: 84193 binary representation: 0000 0000 0000 0001 0100 1000 1110 0001 BinInt[]: 1 0 0 0 0 1 1 1 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Note that we represent the bits of the integer in the array so that the 2 k bit is stored at index k in the array. That simplifies some coding. For this question, we will only emulate three basic operations: addition, negation and subtraction. You will also write a binary to decimal conversion function. You will find a C header file on the course website containing the following function declarations: / Initializes BinInt[] so that the elements correspond to the 2's complement representation of the 32-bit integer Src. Pre: BinInt[] is of dimension 32 Post: For i = 0 to 31, BinInt[i] == 2^i bit of Src / void BI_Create(uint8_t BinInt[], int32_t Src); / Converts a BinInt[] to its decimal representation. Pre: Num[] is of dimension 32 Num[] stores a 2's complement representation integer Ret: The decimal value. / int32_t BI_ToDecimal(const uint8_t Num[]); / Computes sum of 2's complement representations of integer values. Pre: Sum[], Left[] and Right[] are of dimension 32 Left[] and Right[] store 2's complement representations Post: Sum[] == Left[] + Right[], if possible Ret: false if overflow occurs when computing sum of Left[] and Right[]; true otherwise / bool BI_Add(uint8_t Sum[], const uint8_t Left[], const uint8_t Right[]); / Computes difference of 2's complement representations of integer values. Pre: Diff[], Left[] and Right[] are of dimension 32 Left[] and Right[] store 2's complement representations Post: Diff[] == Left[] - Right[], if possible Ret: false if overflow occurs when computing difference of Left[] and Right[]; true otherwise / bool BI_Sub(uint8_t Diff[], const uint8_t Left[], const uint8_t Right[]); 3
/ Computes negation of 2's complement representation of integer value. Pre: Neg[] and Right[] are of dimension 32 Right[] stores a 2's complement representation Post: Neg[] = -Right[] Ret: false if negation cannot be correctly represented; true otherwise / bool BI_Neg(uint8_t Neg[], const uint8_t Right[]); / Prints 2's complement representation, with formatting. Pre: fp is open on an output stream BinInt[] is of dimension 32 and stores a 2's complement representation prefix and suffix are each NULL or point to a C-string Post: the bits represented in BinInt[] have been written, preceded by prefix (if not NULL) and followed by suffix (if not NULL) / void BI_fprintf(FILE fp, const uint8_t BinInt[], char prefix, char suffix); Pay close attention to the given pre- and post-conditions and the specified return values. You will also find a C source file containing trivial bodies for the four functions you need to write. You should download the header and source files and use them as your starting point. There is one important restriction on your implementations: you must not convert a BinInt[] to a C type, like int32_t, or then use the native C arithmetic operations on those values, with the exception of the BI_ToDecimal() function. You may, of course, use the native C arithmetic operations on the bit representations in a BinInt[]. The C source file also contains implementations for the first and last functions whose declarations are shown above; those functions require the use of some bitwise operations we are about to introduce. You do not have to use bitwise operations for the other functions, but you should examine the way they are used in the given functions. What to Submit You will submit your solution in a single.c file to the Curator, via the collection point HW05Q2. You may use any valid Linux file name. This assignment will be graded automatically. You will be allowed up to ten submissions for this assignment, so use them wisely. Test your programs thoroughly before submitting them. Make sure that your programs produce correct results for every logically valid test case you can think of. Do not waste submissions on untested code, or on code that does not compile with the supplied code from the course website. The Curator will assign a score based on runtime testing of your submission; your best score will be counted; the TAs will later verify that your best submission meets the stated restrictions, and assess penalties if not. The Student Guide and other pertinent information, such as the link to the proper submit page, can be found on the course website. 4
Pledge: Each of your program submissions must be pledged to conform to the Honor Code requirements for this course. Specifically, you must include the following pledge statement in the submitted file: On my honor: - I have not discussed the C language code in my program with anyone other than my instructor or the teaching assistants assigned to this course. - I have not used C language code obtained from another student, or any other unauthorized source, either modified or unmodified. - If any C language code or documentation used in my program was obtained from an allowed source, such as a text book or course notes, that has been clearly noted with a proper citation in the comments of my program. <Student Name> Failure to include this pledge in a submission will result in the submission being disallowed during code review. An Example of Simple Binary Addition The simple way to perform addition is to add corresponding bits and propagate carries: carry bits: 1111111111111111111111111111111111111111111111111110011111111110 7321598: 0000000000000000000000000000000000000000011011111011011111111110 + -4332805: 1111111111111111111111111111111111111111101111011110001011111011 ------------------------------------------------------------------------------- 0000000000000000000000000000000000000000001011011001101011111001 You will have to detect the occurrence of overflow. For our purposes, it's sufficient to define overflow as the condition that the mathematically-correct result of an operation cannot be represented correctly using the allowed number of bits (and so, the value that is computed is incorrect). See the course notes for additional information. 5