Main
Lab 2
Objectives: practice with pointers, multidimensional arrays, function arguments (passing by value), dynamic memory management. Try to work as much as possible on the Unix command line.
In your uoregon-cis330 Bitbucket repository create a new directory named lab2
and go to it (cd lab2)
.
Compare two static arrays
- Write a C function
bool arrayEqual
that compares two 2D arrays. It accepts the following argumentsint a[ROW][COL], int b[ROW][COL], m, n
(m and n are the size of the rows and columns correspondingly). It returnstrue
if all the corresponding elements are equal in a and b, otherwisefalse
. You may have to include the <stdbool.h> header file. - Write the
main
function. Initialize two arrays (e.g., 3 by 4 dimension). - Test your function with these two arrays and print the proper output. Make sure you pass the arguments correctly. Try different elements on
a
andb
to produce both true and false results. - Optimize the
arrayEqual
function. It can decide to return the result as soon as it finds a mismatched element. - Optionally restructure your implementation into a header file and a separate C source file and create a simple Makefile
- Optionally, encapsulate the array and its dimensions in a struct IntMatrix. Modify the comparison function accordingly.
- Show your code and the result to your instructors.
Comparing dynamically allocated arrays
- Define a2 and b2 of type
int**
orint*
(your choice). Dynamically allocate memory for them. Set the values for 2*3 arrays. (Yes you need to set 6 values for each array). If you used a struct in part one, just modify your struct for this problem. - Copy and paste your
arrayEqual
function in the source file, name itarrayEqual2
and change the input arguments toint**
(orint*
) as well. You do not need the constantsROW
andCOL
in this function. The body of the function might be the same! Test this function with dynamically allocated arrays and output the result of the comparison. Try to produce both true and false results. - Check for memory leaks with Valgrind as shown in lecture
- Optionally restructure your implementation into separate functions, and separate the array functions and main into two different files (you may also want to create a header file for the array functions), then create a simple Makefile.
- Show your code and the result to your instructors.