Each exercise this quarter is rated on a integer scale of 1 – 5, inclusive, with 1 being the "least time-consuming" and 5 being the "most time-consuming".
This difficulty scale is meant as a rough guide for you in predicting the amount of time to set aside for each exercise as you balance the work required for 333 with your other obligations. However, it is necessarily imperfect as everyone's set of circumstances and experiences with the exercises differ. If your experience with an exercise does not align with its rating, that is not a reflection of you or your abilities.
As discussed in lecture, arrays and pointers are closely related:
ar[i] ↔ *(ar+i).Write a C program (ex2.c) that does the following:
DumpBytes with prototype
void DumpBytes(void* pData, int32_t byteLen).
The void pointer can be thought of as a generalized
form of an array of bytes and byteLen can be thought
of as the length of the "array."
This function should print out the length, the address passed in,
and the bytes of memory as exactly two digits each in
lowercase hexadecimal, e.g.,The 4 bytes starting at 0x7fff1081856c are: ff 01 30 4e
byteLen = 0, it's ok
to have a space at the end of the output.byteLen > 0, there should
not be a space at the end of the output.CopyAndSort that accepts,
in order, two arrays of uint8_t's (i.e.,
two arrays of bytes) and an array length as arguments; you should
assume the length of the two arrays are the same.
The function should (1) call DumpBytes on the first
array and its sizeof and (2) iterate through the
entries of the first array and copy the entries into the second
array in non-descending (i.e., ascending with duplicates
allowed) sorted order.
qsort()),
however you may use any type of sort.
We would recommend using insertion sort.main function found below that sorts a
local array {3, 2, 0, 8, 17, 6, 10, 7, 8, 1, 12} and dumps
the bytes of the arrays along with some other variables using the
two functions that you wrote.
Your main should match the provided code exactly,
with the exception of filling in the missing arguments:
int main(int argc, char* argv[]) {
int32_t int_val = 1;
float float_val = 1.0f;
uint8_t arr_unsorted[] = {3, 2, 0, 8, 17, 6, 10, 7, 8, 1, 12};
uint8_t arr_sorted[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
DumpBytes(&int_val, sizeof(int_val));
DumpBytes(&float_val, sizeof(float_val));
DumpBytes(arr_unsorted, _____);
CopyAndSort(arr_unsorted, arr_sorted, _____);
DumpBytes(arr_sorted, _____);
return EXIT_SUCCESS;
}
void*.printf
to print out a pointer value as well as a uint8_t in
lowercase hexadecimal.
As a hint, take inspiration from the following
code:
uint8_t a_byte = 0xD1;
printf("The byte is: %02" PRIx8 " -- enjoy!\n", a_byte);
int.'sizeof' on array function parameter...
.
We encourage you to stop and think why gcc believes it is worth
warning you about this.
It is fine for your submission to generate this warning when
compiled; however, you should fix any other compiler warnings you
get in your code.For the sake of our autograder, make sure that your function names match the specifications exactly, including capitalization. You should write comments explaining the behavior and purpose of functions you define.
As with the previous exercise, make sure that you organize and comment your functions in such a way that follows the best C practices.
Utilize the correct format specifiers to avoid implicit casts and to increase the portability of your code.
Avoid "magic numbers" (i.e., unnamed numerical
constants) where possible.
Use predefined constants (e.g., EXIT_SUCCESS),
if available, or use #define to define/name any
integer constants that have a clear and specific use.
Use sizeof to increase the portability of your code.
Submit the following file(s) by creating an ex2-submit tag in your exercise repo before the assignment deadline. The file(s) should be located in the exact directory listed below (there should be a folder titled ex2 with the ex2.c file within that folder), including capitalization:
ex2/ex2.cFor full credit, your code must:
attu, or CSE home VM).gcc and valgrind).$ gcc -Wall -g -std=c17 -o ex2 ex2.c
.c file with
your name(s) and CSE or UW email address(es).cpplint.py --clint).