Lecture 2
From C to C++: arrays and functions
- Start by doing the whole lab 1.
Discussion.
- About lab and homework submissions:
You are responsible for making sure you submit all
the files that were requested. You must name them exactly as
specified in the handout. Later in the semester I will no longer
specify routine details. The typescript has to be clean and readable.
- More Unix tricks:
- File completion and the arrow keys to navigate in history.
- How to pipe in data from a file. Do Part I of
lab 2. Submit when
done.
- C++ comes with an enhanced discipline of programming,
which is vital for large programming projects. This is
captured in the object-oriented model.
- Software Engineering principles help you design
rapidly good, readable and reliable code, as well as debug and
maintain it easily. We start today with some basic principles for
good software design.
- Criteria for readability. Header: name, class
account, hwk number, what the program does. Comments.
Discussion of an example: what is good and
what is not with this homework solution (it doesn't work, so do
not use it as a model for later homeworks!!). The same
program
(still not working) but with improved readability.
- Organization of the program: functions before main; function
prototypes before main, then main, then function code.
-
Programming discipline:
- Working with separate directories for each homework, lab, and
for various versions of the program.
mkdir Lab2
cd Lab2
- 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 Lab2
cp Template/template.cpp Lab2/.
cd Lab2
cp template.cpp prog1.cpp
emacs prog1.cpp (to edit and start creating the program)
- A (brief) review of arrays in C and C++.
- Declaring arrays. Space allocation for arrays. Using
#define to set up the maximum (allocated) array size.
- Using program variables to set up the actual dimesnions of an
array: you may name them nrrownrcol (number of columns), or simply nr and
nc.
- When reading input into an array: first get the actual sizes of the
array from the user into the nr and nc
variables. The use a for loop, constrained by these
values, to read the actual elements.
- All operations on arrays must be done on actual array
entries, not on all the allocated array. Hence the for loops
should use the nr and nc variables, not the maximum allocated
values defined with #define.
- Example, with
typescript.
- A (brief) review of functions in C and C++.
- Defining a function: name, parameters.
- Using (calling) a function: passing actual values to the parameters.
- In, Out and In/Out parameters.
- Example, with typescript.
- In-class exercise: the IN and OUT parameters of the functions
in the previous example.
- In-class lab: start working on
Hwk 1, which is reviewing arrays
and functions in C in the context of the transition to C++.
- Handout: The Game of Life. Go over the handout by
Tuesday to understand the game. You can also "play" it on-line,
e.g.
here.
You will work on it in class on Tu and Th, and
your next homework assignment will be based on it.