| CS 242, Section 002 | Sonoma State University | Spring, 2026 |
|
Discrete Structures for Computer Science
|
||
|
Instructor: Henry M. Walker
Lecturer, Sonoma State University | ||
Notes:
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.
| Version | C/C++ Code | Python 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;
|
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:
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.
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?
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.
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))
|
created October 25, 2025 revised October-November, 2025 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |