CS 1711 Lab 4 - October 13 and 15, 1998
Functions and Real Numbers

Watching Program Variables }

It is often useful to set a ``Watch'' on program variables, to see if their values change during a program run. Watching a variable can be a productive way of uncovering bugs in a program.

  1. Move the cursor on top of any occurrence of the variable you want to watch.
  2. Choose ``Add Watch'' from the Debug menu. You should be given a dialog box with the variable name already in the box.
  3. Press Enter to set up a watch on the variable. (you can repeat this for other variables.)
  4. Move the Watch window to the right.
  5. Now step through the program, keeping an eye on the Watch window. You should see the values of of the watched variable change during a run of the program.

Euclid's Algorithm}

In the file GCD.CPP (in the usual directory) is the program GCD, which will output the greatest common divisor (GCD) of the two positive integers entered by the user. The GCD of two integers is the largest integer that divides both of them.

The program is based on Euclid's algorithm for finding the GCD of two positive integers m and n, i.e. GCD(m,n). This algorithm says that:

[a.] The GCD is n if n is the smaller number and n divides m.
[b.] If m is the smaller number, then the GCD determination should be performed with the arguments transposed (switched).
[c.] If n does not divide m, the answer is obtained by finding the GCD of n and the remainder of m divided by n.

For example, to find GCD(28,48): $$\vbox{\halign {\hfil # \hfil && \quad # \hfil \cr apply & to get: ~~~~~~~\cr \noalign{\medskip} b & GCD(48,28) ~~~~~~~\cr \noalign{\medskip} c & GCD(28, remainder of 48 divided by 28) = GCD(28,20) ~~~~~~~\cr \noalign{\medskip} c & GCD(20, remainder of 28 divided by 20) = GCD(20,8) ~~~~~~~\cr \noalign{\medskip} c & GCD(8, remainder of 20 divided by 8) = GCD(8,4) ~~~~~~~\cr \noalign{\medskip} a & 4 ~~~~~~~\cr }}$$

The program has a bug in it. Although you may be able to identify the bug without the use of your debugging tools, you should still follow the steps listed here, so that you can practice using the tools. The true value of the tools is discovered when you have a large program, thousands of lines long, that is hiding a small bug.

  1. Obtain the program from the file GCD.CPP.
  2. Test the program. What does it output as the GCD of 42 and 18 (enter 42 for the first number and 18 for the second number)? Is that correct?
  3. Test the program again. What does it output as the GCD of 18 and 42? Is that correct?
  4. Use the debugging tools to set up a Watch of the variables m and n. You want to determine each value that m becomes during an execution of the program. Run the program again with the input values of 48 and 28. Show the sequence of values for m and n:

    m:

    n:

  5. Compare your trace of the values of m and n, to the values shown in the table on the previous page. Study the program and determine the error. What line of the program needs to be changed?
  6. Fix the bug in the program and save the program. What is the GCD of 16380 and 1911? Before leaving the lab, you will be asked to load this program and point out the bug that was fixed.
  7. Use your debugger to answer the following questions about the corrected program: [a.] Write down the sequence of values for variable m if the input is 5913 and 7592.
    [b.] How many times is the then part of the if-then-else statement executed if the input is 5913 and 7592?

Accuracy

The section is really an experiment in which you are given the basic programs you need, and you are asked specific questions about the results. The objective of the experiment is to establish how many decimal places you can view of a real number on your system.

  1. Obtain the file TESTREAL.CPP. Look it over. You will see a new loop construct known as a `do-while' loop, it is described in Chapter 7 of the text if you are interested but essentially it works the same as a `while' loop.
  2. This program will output the value 2/3 repeatedly, each time using a higher value for the number of decimal places to display. The sleep(1) function causes the program to pause for 1 second before proceeding so you can view the output.
  3. Run the program and determine the maximum number of decimal places that your system reports to you.
  4. How many decimal places can you use?
  5. Turbo C++provides provides a few alternative types for representing real numbers. Now change the type `float' to be one of the following: double, and long double. How many decimal places can you use with each of these types?
  6. A theorem states: the limit as x approaches zero of sinx divided by x equals 1.
  7. The program in file TESTSINE.CPP investigates the computer's evaluation and reporting of this formula. Given an initial number of radians, the program reports sinx divided by x for that number, then for that number divided by 2, then divided by 4, then 8, and so on. The program reports the value in both a narrow format and a broad format. Obtain the file and study it. Edit the program and set the value of the constant numdecs on line 9 to whatever answer you got for question number 5 above (with the double type). This way, when you run the program, it will report as much information as possible on your system.
  8. Answer the following questions about the program:
    [a.] What is the purpose of the if statement on line 55?
    [b.] On what line is the radian number cut in half?
    [c.] On what line is the formula evaluated?
  9. Run the program. Use an initial value for the radians of 99.
    [a.] At what count does the computer show in the narrow format that the value of (Sin X)/X is 1?
    [b.] At what count does the computer show in the broad format that the value of (Sin X)/X is 1?
    [c.] What is the value of the radians at that point, i.e., when the value of (Sin X)/X is 1 in the broad format.
    [d.] At what count does the computer show that the value of the Boolean expression (Sin X)/X = 1.0 is true?
    [e.] At what count does the computer report the value of the radians as zero?

Purpose of Above Excercise

When dealing with real numbers (float or double):
  1. You can't always trust what you see. Just because it looks like a value of 1 doesn't mean it actually is.
  2. Even with all digits of accuracy switched on, 1 doesn't even equal 1! This problem will arise in a later assignment; it is very difficult to check two real numbers for equality.
  3. Keep in mind that the answer you see on the screen for sinx divided by x should have never been 1.
Question: Its obvious that if you want more accuracy, you would use double or long double. Why would you ever want to use the type float? CS1711 Lab 5

CS 1711 Lab 5 - October 20 and 22, 1998
Writing Functions and Driver Programs

\begin{document} \makeheader \section{Writing a Driver} In this section you will get some practice at taking an exsiting function and writing a driver program to test it.
  1. Load the file LABSUB.CPP.
  2. The main function does nothing. Modify the main function to test the function get_first.
  3. Test the program at least 6 times and record which values you use for the test.
  4. Save the program.
\section{Designing a function given the driver} In this section, you will be given a driver program and a function for which a stub has been inserted. You are to replace the stub with the real code for the function.
  1. Load the file MAXOF3.CPP.
  2. Try running the program to see the results.
  3. Modify the function so that the program works properly (you must NOT modify the main function).
  4. Save the program.
\section{Call-by-value and Call-by-reference Parameters} The objective of this section is to point out the differences between call by value and call by reference. This section assumes that you have already read the section in the text on call-by-reference parameters. If you have not done so, now would be a good time to browse it.
  1. Load the file PARAMS.CPP.
  2. DO NOT RUN THIS PROGRAM! (yet)
  3. Look through the program and predict on a peice of paper, what values you expect to see when the program is read. Each section will output three numbers. Predict what will happen in each section.
  4. Now run the program and record the results.
  5. Did you score 100\%? If you cannot determine why some of the actual outputs do not look correct, ask.
\section{End of Lab} No, you are not missing the other side of the lab. This lab was purposely left short for a chance to review/preview the differences between call-by-value and call-by-reference parameters. If you have finished early, please use the time to work on the current assignment if you have not already finished it. <\html> CS1711 Lab 8

CS 1711 Lab 8 - October 6 and 8, 1998
Arrays

\begin{document} \makeheader \thispagestyle{empty} \medskip\hfil {\large\bf !!Lab tests next week: November 18 and 20!!}\hfil \medskip

Storage of integers with binary digits} \medskip

This lab involves conversion of an array of {\tt char} to an array of integers with values limited to be 0 or 1. The array is going to be interpreted as a positive integer with binary digits as found in the array. Recall that any positive integer can be represented with place notation in any base, for example the the integer we write as 105 in decimal notation is actually $$ 1\times b^2 + 0 \times b^1 + 5\times b^0 $$ where $b$, the {\em base} is 10 in this case, and the coefficients of powers of $b$ must come from a set of $b$ symbols for digits: 0,1,2,...,$b-1$. We already considered $b = 8$ on an assignment. Here we will work with $b = 2$. What happens if $b$ is greater than 10? In that case we need more symbols than the usual digits $0,1,...,9$ so what is commonly done is to adopt the upper case Latin alphabet. An important case (Why?) is $b = 16 \stackrel{!}{=} 2^4$ where the digits are $0,1,...,9,A,...,F$. \vspace{2ex}

A collection of stub routines and a main driver program has been written for you. The main() found does not need to be modified (or should I say `DO NOT MODIFY THE MAIN FUNCTION!'), except for commenting function calls out for testing purposes. The function stubs are written just to let the program execute but not correctly. Your job is to `fill' in the missing statments; please follow the order below. \medskip

  1. Obtain the file from the directory for Lab 8 and carefully read the function prototypes.
  2. Write the function bin_2_int() which receives an array of integers consisting of 0's and 1's (and its size and number of elements used) and outputs the corresponding binary integer as a decimal integer. Test this function.
  3. The get_binstr() function reads characters from the terminal into a string (array of char) until the end of line is reached. The characters must be {\tt 0} or {\tt 1}. Be sure that you verify the input and require the user to modify any errors. You may need to look through Chapter 10 briefly to see how strings are handled (do not forget the NULL character at the end of the string).
  4. Using appropriate stub procedures, TEST your input function for appropriate input values. Write the inputs you used for testing at the bottom of this sheet to show to the lab instructor.
  5. The conversion function bins_2_binv() will produce an integer array with elements which are 0's and 1's. Write this function and TEST it with appropriate values, write the test values used at the bottom. You may want to use the function strlen() function (see Appendix or on-line help) to assist you with this function.
  6. Demonstrate your completed program to the lab instructor and be prepared to explain the test values used.
\medskip

Function {\tt input} Test Values: \vfill

Function {\tt convert} Test Values: \vfill \noindent Function {\tt output} Test Values: \vfil <\html> CS1711 Lab 9

CS 1711 Lab 9 - October 6 and 8, 1998
Records and Arrays

\begin{document} \makeheader \thispagestyle{empty}

Introduction} In this lab you will fill in the blanks to complete a fully operational address book that may be of some use to you. This program may look a little long, but a `large' program is required to give students a feeling for what can be accomplished with the use of structures. Most of the programming has been completed, you only need to fill in the missing lines. You should browse through the program to see what the various functions do. You may be asked to describe the operation of some of the functions before you leave. If you have questions about how a function works, ask.
  1. Load the file named address.cpp from the usual location.
  2. Add the necessary fields for the structure called Address. The function get_address() may help you to determine the field names.
  3. Add the necessary fields for the structure called PhoneNumber.
  4. Complete the missing lines in the display_phone_number function.
  5. Complete the missing lines in the display_address function.
It is suggested that you do not modify any other function in this program except for perhaps testing purposes.