You may assign the value o to the elements

You may assign the value o to the elements in your array to initialize the elements — whether you are creating the array or appending to the array. 0 will indicate an “empty” element. • Recall, in pre-allocation, we allocate more space in memory than what is requested by the user. rows and cols are the number of rows and columns the user has requested for in their 2D matrix. For example, if rows is 10 and cols is 10, and the user appends a value such that the number of rows increase by one, then you increase rows to 11. o reserved_rows and reserved_cols are the number of rows and columns that are reserved for the user i.e. this is the capacity of your 2D array. For example, currently, the rows and reserved_rows are 10 and cols and reserved_cols are 10. If the user appends a value such that the number of rows should increase, then you increase rows to 11 and double the size of reserved_rows to 20 along with the number of rows allocated in memory for your 2D array. Review preallocation for more details. These functions would work around the following user defined data structures (see toimgr.h): /* Structure type that encapsulates our image: 2D array. * the rows represent the indices of the main array, * the cols represent the indices of the arrays pointed to by the pointers * in the elements of the main array. */ typedef struct { uint8_t** pixels; unsigned int rows; unsigned int cols; unsigned int reserved_rows; unsigned int reserved_cols; } imgr_t; /* A type for returning status codes */ typedef enum { IMGR_OK, IMGR_BADCOL, IMGR_BADROW, IMGR_BADALLOC, IMGR EMPTY } imgr_result_t; HINTS • Don’t forget to keep adding appropriate function calls to your test driver as you go along. reserved_rows should always represent the amount of memory allocated to all the row arrays in imgr_t. This means that if you doubled the length of one row, you need to do the same for all other rows. reserved_cols should always represent the amount of memory allocated to the main col array. This means that if you double your original array, you also need to double the amount of rows you have in imgr_t. TESTING: you can test your program by running: $ make to # OR gcc -Werror -Wfatal-errors -g -o to to.c toimgr.c (see Makefile) $ ./to (see? Makefiles saves your gcc command so you don’t have to type this long thing over and over again, convenient huh :D) The following is a breakdown of the tasks. REMINDER: • write comments!! Test and debug your code! Prompt the user for what they should enter by printing messages with printf , e.g. “Enter an integer: “, and let the user know what the output is by printing a message, e.g. “Here is the result: Task 01.1 REQUIREMENT: write a function in toimgr.c with the following declaration: imgr_t* imgr_create(unsigned int rows, unsigned int cols); • INPUT: the number of rows and cols of the desired array. • OUTPUT: ingr_create() returns the POINTER to a new instance of data structure imgr_t. • BEHAVIOUR: if malloc() fails (i.e. returns NULL ), imgr_create() returns NULL . o The reserved_rows and reserved_cols should be initialized to be the same as rows and cols respectively. These fields represent the capacity or the available space in your 2D array. In the front end, the user should not have to deal with this. You use them in your functions to perform preallocation. Task 01.2 REQUIREMENT: write a function in toimgr.c with the following declaration: void imgr_destroy(imgr_t* im); • INPUT: the pointer of a imgr_t variable im. • BEHAVIOUR: ingr_destroy() should free() the memory allocated to im.

Tags: No tags