Lecture 3
Simple function calls in C. The
swap function.
Parameter passing in C and in C++. References versus pointers, a first
example (we'll get back to this topic, in more detail, later in
the semester).
Brief review of arrays in C and C++.
Today: Demo on how to start working on a hwk, illustrated
with a program for Selection Sort. Application for arrays,
function calls and preparation for Hw2
Programming discipline:
- Working with separate directories for each homework, lab, and
for various versions of the program.
mkdir V1
cd V1
- Using a template for the program
mkdir Template
cd Template
then create the template with emacs
.
Example.
- When working on a new homework/lab:
mkdir Hw4
cp Template/template.cpp Hw4/.
cd Hw4
cp template.cpp sort.cpp
emacs sort.cpp (to edit and start creating the program)
Now, I illustrate this on a concrete example, the Selection Sort.
Selection Sort
Input: an array of n integers.
Output: the array in sorted order (increasing)
Algorithm: Works in n iterations. At iteration
i it finds the minimum element among the elements
not yet sorted (which are all, at
the first step), then swaps it with the element at position
i. Thus after iteration i, the first i
elements are
sorted. After n steps, all are sorted.
Here's what we did in class. Try to find the
error in the last version of the program, which I put in V2Bad.
And here is the version I wrote before class, sort.cpp.
Hw 2