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

  1. Write a C function bool arrayEqual that compares two 2D arrays. It accepts the following arguments int a[ROW][COL], int b[ROW][COL], m, n (m and n are the size of the rows and columns correspondingly). It returns true if all the corresponding elements are equal in a and b, otherwise false. You may have to include the <stdbool.h> header file.
  2. Write the main function. Initialize two arrays (e.g., 3 by 4 dimension).
  3. Test your function with these two arrays and print the proper output. Make sure you pass the arguments correctly. Try different elements on a and b to produce both true and false results.
  4. Optimize the arrayEqual function. It can decide to return the result as soon as it finds a mismatched element.
  5. Optionally restructure your implementation into a header file and a separate C source file and create a simple Makefile
  6. 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

  1. Define a2 and b2 of type int** or int* (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.
  2. Copy and paste your arrayEqual function in the source file, name it arrayEqual2 and change the input arguments to int** (or int*) as well. You do not need the constants ROW and COL 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.
  3. Check for memory leaks with Valgrind as shown in lecture
  4. 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.

Green Marinee theme adapted by David Gilbert, powered by PmWiki