CS 242, Section 002 Sonoma State University Spring, 2026
 
Discrete Structures for Computer Science
Instructor: Henry M. Walker

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

Notes:


Assignment 6 (Functions and Boolean Algebra)

  1. Loops, Counting, Functions, and Efficiency: A Preview:
    A major consideration in some upper-level courses, such as CS 415, Analysis of Algorithms, involves how algorithms will perform on large data sets. For example, how might the time of program execution change if a data set doubled in size or increased by a factor of k. This problem may provide a hint of how such an analysis might proceed.

    Consider the six code segments in the following table, given in equivalent C/C++ and Python versions. In each case, the real-number/double variable n and integer variable k should be considered as positive constants which have been initialized before the code segments and which do not change values during execution. Also, the designation loop body should be considered as a block of code to perform a specified task—details are irrelevant to this problem and thus are omitted.
    Note: Since the intent is to increase the loop control variable(s) i (and j by varying amounts, the C/C++ versions utilize simple for loops, while the Python versions use equivalent while constructions.

    VersionC/C++ CodePython Code
    A
            for (int i = 1; i <= n; i++) {
               loop body
            }   
          
            i = 1;
            while (i <= n) :
               loop body
               i+=;      
    B
            for (int i = 1; i <= n; i+=k) {
               loop body
            }        
            i = 1;
            while (i <= n) :
               loop body
               i+=k;      
    C
            for (int i = 1; i <= n; i++) {
               for (int j = 1; j <= n; j++) {
                  loop body
               }
            }         
            i = 1;
            while (i <= n) :
               j = 1;
               while (j <= n) :
                  loop body
                  j+=1;
               i+=1;      
    D
            int inc = int (n/k);
            for (int i = 1; i <= n; i+=inc) {
               loop body
            }         
            i = 1;
            inc = int(n/k);
            while (i <= n) :
               loop body
               i+=inc;      
    E
            for (int i = 1; i <= n; i*=2) {
               loop body
            }         
            i = 1;
            while (i <= n) :
               loop body
               i*=2;      
    F
            for (int i = 1; i <= n; i++) {
               for (int j = 1; j <= n; j*=10) {
                  loop body
               }
            }   
          
            i = 1;
            while (i <= n) :
               j = 1;
               while (j <= n) :
                  loop body
                  j*=10;
               i+=1;
          
    1. Counting Iterations within the Loop:
      For each of the versions above (i.e., A, B, C, D, E, F), determine the exact number of times the loop body is executed, based on the pre-defined variables n and k.
      Notes:

      • Although you should not assume the variable n is an integer (e.g., n might be 10.5), you should assume 1 < n.
      • The number of loop iterations (i.e.,the number of times the loop body is executed) must be an integer. (For example, if n=10.5 and k=3, then the loop is executed 3 times, not 10.5/3 or 3.5 times. Thus, you may need to use a floor or ceiling function to round a numeric value up or down.
    2. Timing Code within a Loop:
      For the each of the loops in the Table, suppose the loop body takes 1/1000 (or 0.001) seconds to execute.
      Assuming n is 50,000 and k is 10, compute the amount of time required for processing the loop body for each of the loops in the Table.

    3. Timing Code with n Doubled:
      Now assume the value of n is doubled (i.e., to 100,000), with the value of k unchanged (i.e., still equal 10). What is the time required for processing the loop body with this new n value?

    4. Observing How Timing Scales as the Data Set Size Doubles:
      Between parts c and d, the value of n doubles. What can you say about the how times compare? (E.g., Do the times also double, increase by a factor of 2 or 3 or 4 or ..., increase rather little, etc.?)
      Note: In case you are interested, 1000 seconds equals about 16.7 minutes, 10,000 seconds equals about 2.8 hours, and 100,000 seconds equals about 1.2 days.

  2. Manipulating Boolean Expressions and Boolean Algebra:
    Consider the following three Boolean conditions (in C/C++) that might be found in a code segment—each with a label x, y, or z:

    Although these comparisons are Boolean conditions, the C and C++ languages consider these as yielding the value 0 for false, and some non-zero number (often 1) represents true. Thus, these conditions fit nicely within the framework of Boolean algebra (from Sections 5.1 and 5.2 in the textbook), as well as within the context of conditions in programs (at least for C and C++). With this relationship, this problem begins with Boolean conditions that might arise in C/C++ code, but then interprets x, y, and z either as propositions or Boolean variables, as seems helpful. Now consider the following conditional Boolean expression.

             ((i <= j) && (a[i] < a[j])  && (a[i] == value))
          || ((i <= j) && (a[i] >= a[j]) && (a[i] != value))
          || ((i > j)  && (a[i] < a[j])  && (a[i] != value))
       
    1. Express this conditional expression using Boolean algebra, including Boolean variables x, y, and z, multiplication for the "and" operation, addition for "or", and the bar notation (i.e., x, y, z) for the complement (or negative) of a variable.
    2. Given your expression from part a, draw an input/output table for this function f(x, y, z), following the model of Table 5.2.1 in the textbook.
    3. From your answer to part b, add a column that shows the complement (or negative) of this expression (f(x,y,z)). Likely you will want to change 0s to 1s and 1s to 0s from the f(x,y,z) column to get the new column.
    4. Based on your answer to part c, write a Boolean expression that is equivalent to the complement f(x,y,z).
    5. Since the Boolean variables x, y, and z also represent the Boolean conditions at the start of this problem, use your function for f(x,y,z) to obtain an expression which is the complement (or negative) of the Boolean expression at the start of this problem. (Your answer to this part should be an expression that could be used as a condition in an if, for, or while statement in a program.)

created October 25, 2025
revised October-November, 2025
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.