CSC 161 Grinnell College Spring, 2013
 
Imperative Problem Solving and Data Structures
 
 

Laboratory Exercise on Addressing and the Address Operator

The & Operator

Certain variable types, such as arrays, are always passed in by reference. Primitive data types, such as ints, chars, and doubles, are passed by value by default. However, you can also pass in primitive data types by reference using the & operator.

Primitives By Reference

  1. Copy program rand-beep.c into your directory for this lab and run it a few times to see what it does.

  2. Write a few sentences explaining what the program is doing, making sure include the following points:

    • Point out every time a primitive value is passed in by reference to a function.

    • Note how values passed in by reference are called within the function.

    • What durations are possible for the robot to beep for?

    • What frequencies are possible for the robot to beep for?

    • When will the loop end?

Addresses

  1. Copy get-ir.c into your directory for this lab and write a few sentences explaining what the program is doing.

  2. Complete the parts of the program which say "code here!". It should only require one line of code at each spot, and there are three locations where you must add code.

    • Hint: If you are getting the warning "assignment makes pointer from integer without a cast", try putting * (an asterisk) in front of the integer you are assigning to.

Indicating Function Success

  1. Write a simple function which finds the square root of a number. It will have the following signature: int safe_sqrt (double * num).

    • It will use the double sqrt(double num) function from the math.h library to find the square root of num. Make sure you compile with the -lm flag when using sqrt().

    • If num is zero or positive, your function will modify num to be its square root and return 1 to indicate success.

    • If num is negative, your function will not modify num and return 0 to indicate failure.

    • You will have to use the & operator when calling your function so that num is passed in by reference.

Testing

Pre- and Post-Conditions

  1. The program motors-test.c sets the motor speeds of the Scribbler to the given leftspeed and rightspeed.

    1. Initialize the variables leftspeed and rightspeed to 1.

    2. Initialize the variables leftspeed and rightspeed to -1 and -1 respectively.

    3. Initialize the variables leftspeed and rightspeed to 2 and -1. respectively.

    4. Now try 6 and 5 respectively.

    5. Try other numbers that you might need to figure out what works and what doesn't.

    6. Explain why these results turned out the way they did. Why did some of the numbers work and why did some not work?

  2. Add the preconditions and postconditions of rMotors to the appropriate place.

    Hint: You may want to use MyroC.h

The assert Function in C

  1. Modify the same code, motors-test.c, to use assert  so that it will test the precondition(s) you wrote for rMotors in motors-test.c.

Choosing Test Cases

  1. Copy the program object-avoid.c to your working directory. This program attempts to control the robot in avoiding obstacles. Run the program a few times and observe what it does.

  2. Develop two test plans for object-avoid.c to figure out if the program works correctly. That is, apply both black-box and white box testing by identify tests cases that will cover a full range of situations that might be encountered in executing the program object-avoid.c . Remember that:

    • Black-box testing is when the problem is examined to determine the logical cases that might arise. Test cases are developed without reference to details of code.

    • White-box testing is when the code is examined to determine each of the possible conditions that may arise, and tests are developed to exercise each part of the code.

  3. You should have found that the program is not working properly. Now fix the program so that it performs in the intended way and the robot avoids obstacles.

Debugging

  1. Develop two test plans for follower-test.c. This program should control the robot in following a sheet of paper which is held in front of the robot. Apply both black-box and white box testing by identify tests cases that will cover a full range of situations that might be encountered.

  2. Run follower-test.c  with all the cases from your test plan to be sure it works correctly.

  3. Now fix the errors you found in the program.

  4. Run the program again with all the cases from your test plan to be sure that it now works correctly.