CS 31 Lab 4: C Pointers and IA32

Both Parts Due 11:59pm Tuesday, October 9


Handy References

Lab 4 Goals:

Part 1. C Pointers and Memory Allocation.

Experimental scientists collect data from experiments and often want to compute some simple statistical analyses over the set of experimental data. A useful tool would be a program that could compute these statistical results for any size data set (i.e. it would work for 10 data values or for 10 million without re-compilation).

For this program you will implement the program started in stats.c that takes a single command line argument, which is the name of a file of data values (floats, one per line), and computes and prints out a set of statistics about the data values.

The starting point code comes with two input files that you can use to test your solution:

./stats small.txt
Results:
--------
num values:         16
      mean:      1.812
    median:      1.500
   std dev:      1.031
unused array capacity: 4
(Note: Much like you use \n to insert a new line, you can use \t to insert a tab character for pretty output formatting like this.)
./stats large.txt
Results:
--------
num values:         94
      mean:      1.161
    median:      1.000
   std dev:      0.788
unused array capacity: 66

This program includes the readfile library code that it links in as well as linking in the math library: use the makefile to compile. You can see how the executable is built from a .c, a .o, and explicitly linking in the math library (-lm), by reading the Makefile. By including the math library, you gain access to the sqrt function, which you'll need for your standard deviation calculations.

stats.c is the starting point into which you will put your code. It contains the prototype for the getvalues function that you need to implement and has some code to get the filename command line argument.

You should structure your program in the following way:

  1. Make a call to getvalues, passing in the filename of the file containing the data values, and passing in two pointers: the address of an int variable to store the size of the array (number of values read in) and the address of an int variable to store the total array capacity.

    The function will return an array of float values that stores the values read in from the file, or NULL on error (e.g., malloc() fails or the file cannot be opened).

  2. Compute the mean (average), the median (the middle value), the standard deviation of the set of values and print them out. Note: You will likely need to sort the values prior to computing the median. Luckily, you already wrote code to sort floats in lab 2!

  3. Print out the results, plus information about the number of values in the data set and the amount of unused capacity in the array storing the values.

Statistic Functions

The statistics you need to compute on the set of values are the following:
  1. mean: the average of the set of values. For example, if the set is: 5, 6, 4, 2, 7, the mean is 4.8 (24.0/5).
  2. median: the middle value in the set of values. For example, if the set is: 5, 6, 4, 2, 7, the median value is 5 (2 and 4 are smaller and 6 and 7 are larger). If you have an odd number of values, the median is the middle value, as in the previous example. If you have an even number of values, use the average (arithmetic mean) of the two middle values. For example, if your numbers were 1, 3, 6, and 10, your median would be the average of the two middle values: (3+6) / 2 = 9 / 2 = 4.5.
  3. stddev: is given by the following formula:

  4. Where N is the number of data values, X_i is the ith data value, and X-bar is the mean of the values.

Feel free to discuss the math on Piazza or in person with your classmates. While we're using it to ensure that your pointers are working correctly, the math itself is not the focus of this assignment...

Requirements

Tips


Part 2. Writing a Swap Function in IA32.

In the file named swap.s finish the implementation of the a swap function that swaps two int values:

void swap(int *x, int *y);

This function takes two ints passed as pointers (the parameters point to the storage location of their argument variables). After the call, the two int variables passed to swap should have their values swapped. A call to swap would look like (see mainswap.c for another example):

 int a, b;
 a = 10;
 b = 8;
 printf("%d %d\n", a, b);  // prints: 10 8
 swap(&a, &b);  // swap the values stored in a and b
 printf("%d %d\n", a, b);  // prints: 8 10

The swap.s file already contains some IA32 instructions that set up up and tear down the stack frame associated with the swap function. You will add IA32 instructions to the body of this IA32 function to perform swapping operation.

In mainswap.c is code to test your swap implementation. Run make to build an executable, mainswap, that links in your swap.s solution and calls it. Use this to test your solution for correctness.

Requirements

Tips

Submitting

Please remove any debugging output prior to submitting.

To submit your code, simply commit your changes locally using git add and git commit. Then run git push while in your lab directory. Only one partner needs to run the final push, but make sure both partners have pulled and merged each others changes. See the section on Using a shared repo on the git help page.