CS 415, Section 001 Sonoma State University Spring, 2023
 
Algorithm Analysis
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College

Although much of this course is well developed, some details can be expected to evolve as the semester progresses.
Any changes in course details will be announced promptly during class.

Laboratory Exercise on Consequences of Data Representation

This laboratory exercise explores some practical consequences of the representation of data in program processing.

Integer Overflow

Recall from your prior work in C/C++ that constants variables INT_MIN and INT_MAX in C/C++ contain the smallest and largest int values available in C/C++.

  1. Suppose i and j are two non-negative integers, and a program is supposed to find their average (as an integer). (In case the arithmetic average is a real number ending in .5, then the average may be rounded either up or down. Thus, the actual average 7.5 of 6 and 9 may be rounded to either 7 or 8.)

    Five approaches are proposed to find this average:

          avg1 = (i + j) / 2;
          avg2 = i/2 + j/2;
          avg3 = (i+1)/2 + j/2;
          avg4 = i/2 + (j+1)/2;
          avg5 = (i+1)/2 + (j+1)/2;
        
    1. Which, if any, of these approaches will work reliably for all non-negative integers i and j? Explain.
    2. Suppose i and j may be any integers—positive, negative, or zero. In this general case, which, if any, of these approachs will work reliably for all values of i and j? Explain.
  2. Consider the program integer-average.c.

    1. Compile and run the program, and record what int values are possible within C proprams.
    2. Review the program to determine how the values of arr1 are computed, and how the value of sum compares to INT_MAX
    3. Check the program output. Is the computation of the average of values for arr1 correct?
    4. Answer parts b and c for array arr2. What is different in the processing? To the extent that you can, explain why the average computation for this array yields an incorrect result.

Associativity of Addition for Real Numbers

  1. Consider program arithmetic-series.c

    1. Compile and run this program, and observe what happens.
    2. Explain why the output of the first loop is obtained.
    3. Why do you think the termalways seems to be a power of 10 in the first loop?
    4. Explain why the first loop terminates. (That is, why is there a point when oldSum == sum?)
    5. Although all values of term are printed as 0's to 17 decimal places, the values of sum do not always end in 0's. Explain how this could happen.
    6. Review the second loop. Explain why the sequences of steps of the second loop do (or do not) parallel the steps in the first loop (except perhaps in the opposite order).
    7. Why do you think that some printed values of term are not always an exact power of 10 in the second loop ?

Compounding of Numeric Error

Our discussions of the representation of real numbers (doubles and floats) have identified at least three factors that can cause errors in processing—particularly if the errors can accumulate as processing continues.

Be sure to take these potential troubles into account in answering Steps 4 and 5.

  1. Given that start < end, suppose a loop is to begin at start and finish at (or near) end in exactly n+1 iterations. Within this loop, suppose the control variable will increase by a computed value increment = (end-start)/n with each iteration.

    Two loop structures are proposed:

          // approach 1
          increment = (end - start)/n;
          for (i = 0; i <= n; i++){
               value = start + i * increment;              
              /* processing for value */
            }
        
          // approach 2
          value = start;
          increment = (end - start)/n;
          while (value <= end) {
             /* processing for value */
             value += increment;             
          }
        

    Although the first approach requires somewhat more arithmetic within the loop than the second, it likely will provide better accuracy. Identify two distinct reasons why the first approach should be preferred over the second.

  2. Suppose y = f(x) is a function that decreases from x=a to x=b, on the interval [a, b], with a < b.

    Throughout this interval, assume f(x)>0, and assume the Trapezoidal Rule were to be used to approximate the area under y = f(x) on the interval [a, b].

    1. Should the main loop begin at a and go toward b or begin at b and go toward a, or is either order fine? Explain.
    2. Write efficient code that implements the Trapezoidal Rule for this function on this interval. (To be efficient, f(x) should be computed only once for each value of x. Also, multiplication and/or division by 2 should be done as little as possible, as discussed in class.)


created 31 March 2022
expanded 24 July 2022
expanded 3 January 2023
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.
ccbyncsa.png

Copyright © 2011-2022 by Henry M. Walker.
Selected materials copyright by Marge Coahran, Samuel A. Rebelsky, John David Stone, and Henry Walker and used by permission.
This page and other materials developed for this course are under development.
This and all laboratory exercises for this course are licensed under a Creative Commons Attribution-NonCommercial-Share Alike 4.0 International License.