CMSC 15200 - Summer 2016

Lab 2: Testing & Iteration Patterns

Preliminaries

Create and svn add a lab2 directory inside your repostiory.

You will write all your code for this lab in a file named lab2.c

Good coding practices are part of your grade (comments, code formatting, variable names, etc.)!


Part I: Writing Test Cases

In class, we talked about writing test cases for the functions you implement. You may or may not have done this for your functions in homeworks #1 and 2 (yet), but now you will. Go grab and paste your gcd, get_digit, and sum_of_digits functions in the lab2.c file (or write them now if you haven't already for hw2). Using the check-it.h functions, write test cases for these three methods inside the main function. Make sure you test all cases for your functions (i.e., boundary cases and normal cases).


Part 2: Iteration Patterns

This part of the lab requires solving a number of relatively simple problems using specific iteration patterns. You will likely notice that the code in the functions for each pattern is very, very (extremely!) similar. That's because you will be using common patterns. Patterns are good; they allow programmers to think "I need to do something like that, but with different values or with different operations" and then do it by changing the parts that are actually different.

Map Patterns

Each of the functions for this part of the lab are to be implemented using the map pattern. In this pattern, each value in the result array is determined by a computation on the value in the corresponding position (i.e., at the same index) in the input array.

You will implement the following functions inside the lab2.c file:

  • square_all: The square_all function computes, in the result array, the square of each value in the input array

    void square_all(const double input[], int size, double result[]);
    
  • add_n_all: The add_n_all function computes, in each element of the result array, the sum of parameter n and the corresponding value in the input array.

    void add_n_all(double n, const double input[], int size, double result[]);
    
  • scale_n_all: The scale_n_all function computes, in each element of the result array, the product of parameter n and the corresponding value in the input array. The 2D is a square 5x5 matrix.

    void scale_n_all(double n, const double input[][5], int rowSize, int colSize, double result[][5]);
    

Filter Patterns

Each of the functions for this part of the lab are to be implemented using the filter pattern. In this pattern, the values in the result array are determined by a conditional test on each value in the in the input array. Only those values that satisfy the condition will be copied to the result array. Recall that these functions must return the number of values that satisfy the condition; this is required so that the caller can use this information when using the result array. In addition, it is conventional for the values in the result array to be in the same relative order as in the input array.

Again, you will implement the following functions inside the lab2.c file:

  • are_positive: The are_positive function copies to the result array all positive values from the input array.

    int are_positive(const double input[], int size, double result[]);
    
  • are_greater_than: The are_greater_than function copies to the result array all values from the input array that are greater than the n parameter.

    int are_greater_than(double n, const double input[], int size, double result[]);
    
  • find_n_all: The find_n_all function assigns true or false in the resulting array if an element is equal to the n parameter inside the input array. The 2D is a square 5x5 matrix.

    void find_n_all(double n, const double input[][5], int rowSize, int colSize, _Bool results[][5]);
    
You will need to write two test cases for each of the 6 functions above inside your main function. Make sure to svn add and svn commit your files when you're done.

So What's Next?

We encourage you now to finish your HW #2 assignment and start on HW #3 while you have teaching staff present.

Acknowledgements

Matthew Rocklin for creating the write-up for the assignment.