CS Behind the Curtain (CS195 2003S)

Lab: Pointers and Functions

Summary: In this laboratory, we will consider a number of issues pertaining to relationships between pointers and functions.




Exercise 0: Preparation

Create a new directory for this laboratory.

Exercise 1: Comparing Strings

Write a procedure stringLessThan(char *str1, char *str2); that holds if str1 is strictly less than str2, using some natural metric for inequality.

Exercise 2: Finding the Best Value

Make a copy of bestf.c.

a. Explain the purpose of the parameters to best.

b. What should the output of the program be?

c. Verify your answer experimentally.

d. Write the preconditions for best.

Exercise 3: Finding More Best Values

Add the following lines to main. You may find that they need to go at different places in the code.

  float beta[] = { -1.1 9.0 2.1 6.0 1.0 -5.6 };
  printf("The smallest value in beta is: %f\n", best(beta, 5, flt));
  printf("The largest value in beta is: %f\n", best(beta, 5, fgt));

a. What do you expect the revised output to be?

b. Verify your previous answer experimentally.

c. What do your results suggest?

Exercise 4: A Generalized Smallest Procedrue

Make a copy of smallest.c smalli.c, and smallf.c.

a. Explain the parameters to smallest (which appears in smallest.c).

b. Compile smalli.c and run the program.

c. Explain how smalli.c uses smallest.c.

d. Compile smallf.c and run the program.

e. Explain how smallest can be used in both smalli.c and smallf.c.

f. Write preconditions for smallest.

g. What do you think will happen if we change the line in smallest that reads

    if ((*lessThan)(values[i], small))


    if (lessThan(values[i], small))

h. Verify your result experimentally.

i. Reflect.

Exercise 5: Smallest String

Write a program that uses stringLessThan and smallest to find the alphabetically first string in an array of strings.

Exercise 6: Further Generalizations

We've seen that smallest can find the smallest integer in an array in one program and the smallest string in another array in another program.

a. Is there a way to use smallest to find both the smallest integer in an array and the smallest string in an array of strings in the same program.

b. If so, show how. If not, is there a way to write a generalized smallest? (You might have to change the naming, use macros, change the types of the parameters, or something else.) Note that you may find it useful to refer to K&R.



Wednesday, 26 February 2003 [Samuel A. Rebelsky]


Disclaimer: I usually create these pages on the fly, which means that I rarely proofread them and they may contain bad grammar and incorrect details. It also means that I tend to update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This document was generated by Siteweaver on Fri May 2 14:19:54 2003.
The source to the document was last modified on Wed Feb 26 13:49:48 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS195/2003S/Labs/pointers-functions.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky, rebelsky@grinnell.edu