Partial preview of the text
Download Lab Exercises: Absolute Value & Newton-Raphson in CPS 196 and more Lab Reports Computer Science in PDF only on Docsity!
CPS 196 Lab 15 Fall 2007
I. Experimenting with local variables in functions:
A. Start a new item in your project and copy this program from here or from the web page:
/* absolutevaluefunction.c */
/* demonstrates the absolute value function */
#include <stdio.h>
/* function prototype */
double absoluteValue (double x);
int main ( )
double x, answer;
printf("What number would you like to get the absolute value
of? ");
scanf("%lf", &x);
/* call to the absolute value function */
answer = absoluteValue(x);
printf("The absolute value of %6.2lf is: %6.2lf\n", x,
answer);
return(0);
/* compute the absolute value of a real number */
double absoluteValue (double x)
if (x < 0)
x = -x;
return (x);
B. Test this program with both a positive number and a negative number. Now using the same two
numbers, use Debug to Step Into and Step Over the main program, recording the values of variables
in the chart below.
When you get to the line “answer = absoluteValue(x)”, Step Into the function and record the values
of its variable as well as you Step Over the function.
While you are in the function, observe the right hand window, which should be labelled Call Stack,
and observe which functions the debug tool is showing. What are the names of the top two
functions? Write them here:
At the end of the function, continue to Step Over the main program and record any changes to the
values of the variables there.
Variables Value1 Value2 (as many as necessary )
x (main)
answer (main)
x (absoluteValue)
Note that x in the function is a local variable , assignments to it do not affect the
variable in main. (This will be different when we pass arrays as arguments to functions.)
B. Again, use Debug to observe the values of variables in this program. This time, you will need
to use Step Into both to step over the squareRoot function, and in the squareRoot function to step
into the absoluteValue function.
While you are in the absoluteValue function, observe the top three names in the Call Stack. What
are they?
While using Step Over in the while loop, only record up to 4 iterations of variables that change
values, then continue to use Step Over to observe what happens in the loop, but don’t write down
any further values of variables in the squareRoot function until it goes back to the main.
Variables Value1 Value2 (as many as 4 )
number (main)
x (squareroot)
epsilon (squareroot)
guess (squareroot)
testdiff (squareroot)
x (absolutevalue)
C. Does the program work for positive number less than 1? (Try .25, for example.)
D. This program is not supposed to work for negative numbers. (If you test a negative number, be
prepared to wait – the loop will run on forever. To get the program to quit, type Ctrl-C in the
console window. (This means to hold down the Ctrl key and type C.))
Modify the function to make sure that the number given as an argument is not negative and print an
error message if it is.
Test this version of the program. What values did you use?
E. Compare the result from this program with the result from the sqrt program in math.h. Make a
new version of the program that has
#include <math.h>
Declare a new double variable for the new result, something like mathanswer and add the line:
mathanswer = sqrt (number);
Add another printf statement to print this variable. In this printf statement and in the original one,
change the output formatting code to be
%10.8lf
Test this program for some values, including something like 36 that should have a precise answer.
What values did you test?
What do you conclude? Consider the role of the variable epsilon.
When you are done with the lab, print your last squareroot program and show it to me, with the rest
of your lab.