| CS 242, Section 002 | Sonoma State University | Spring, 2026 |
|
Discrete Structures for Computer Science
|
||
|
Instructor: Henry M. Walker
Lecturer, Sonoma State University | ||
Notes:
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.
Indicate which of the following statements are propositions.
Consider these propositions regarding a program A.
Write the following statements as compound propositions, using the above simple propositions (P, Q, R, and S) as appropriate.
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),
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 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |