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 1 (Propositions and Equivalence)

  1. Performance of C and C++ Programs:
    Background:

    Exercises for this Problem: Note that these questions require no specific knowledte of either C or C++ — just the above history.

    1. Indicate which of the following statements are propositions.

      1. Use C++ to solve a given problem.
      2. The C++ language was developed before C.
      3. The C language is a subset of the C++ language.
    2. Consider these propositions regarding a program A.

      • Proposition P: A is written in C.
      • Proposition Q: A can be compiled and run as a C program.
      • Proposition R: A can be compiled and run as a C++ program.
      • Proposition S: A's performance running as a C++ program is better than A's performance running as a C program.

      Write the following statements as compound propositions, using the above simple propositions (P, Q, R, and S) as appropriate.

      1. If A is written in C, then it can be compiled and run as a C++ program.
      2. If A is written in C, then A can be compiled and run as either a C or C++ program, and A's performance will be no worse when running as a C program.
  2. Rewriting/Manipulating Boolean Expressions from computer programs):
    Background/Motivation In writing computer programa, the programmer often makes assumptions about conditions that must be satisfied when a procedure is called. For example, parameters are expected to meet specified conditions when a procedure begins. Such conditions are called pre-conditions. For safety, code often is placed at the beginning of a procedure to check that the preconditions hold. (If not, typically the procedure may return, an exception may be thrown, or the program may exit.)

    Although the code to test preconditions will vary according to the syntax of the programming language, one way to write this code could have the form:

        if (condition) 
           continue with normal processing
        else
           indicate an error
        end if
        

    Although this code can work well, it can be awkward, because the code for normal processing can be extensive, so the error handling in the else can get lost at the end of a procedure. For this reason, a common approach is to rewrite this code using the negative of the condition. This alternative likely will the following form:

        if (not (condition)(
           indicate an error
        end if
    
        continue with normal processing
        

    Here, the error is handled quickly, and the code that follows can assume the precondition is satisfied. The main challenge can be to rewriting condition in its negative form not (condition)
    In practice, studies have shown that many programmers (particularly those in the first few programming courses) make errors in this rewriting of conditions.

    Starting Propositions for this Problem:

    proposition condition
    P1 (0 ≤ x)
    P2 (x < 10)
    P3 (y ≤ 10)
    P4 (x < y)
    P5 (a[x] ≤ a[x+1])
    P6 (a[x] > item)
    P7 (a[x] < a[y])

    Specific Instructions: For each of the compound conditions that follow (CP21, CP22, CP23, CP24),

    1. (example completed in detail below—not to be turned in)
      (CP21): (0 ≤ x) && (x < y)
    2. (CP22): (0 ≤ x) && (x < 10) && (a[x] > item)
    3. (CP23): (0 ≤ x) && ((a[x] ≤ a[x+1]) || (y ≤ 10))
    4. (CP24): ((0 ≤ x) && (a[x] > item)) || ((y ≤ 10) && (a[x] < a[y]))

    Solution to Part a: (since this is already stated, you need not turn in the solution to this problem:)

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.