Lecture #28: Review
December
9, 1999
Bob
Pitha
pre-midterm review
The
final is cumulative
everything
builds on everything else
so I cant
avoid testing everything
if you dont
understand the material from the first part of the class, you almost certainly
wont do well
but you must
also know the later material
lecture #18
was the midterm review
go back over
that as well
lecture 19: programming style
characteristics
of a good program are many, including:
maintainability
readability
elements
of programming style:
naming
formatting
consistency
avoiding
magic numbers
using
comments
lecture 19 (cont.)
naming
A good name
should be:
informative
it should tell you something
concise no
longer than necessary
memorable
you should remember what it is
pronounceable
most people subvocalize
formatting
Good
formatting can make a huge difference
use spaces
carefully
indent lines
to show structure
use
parentheses to resolve ambiguity
lecture 20: programming style 2
consistency
find a style
you like, then stick to it
avoiding
magic numbers
#define
preprocessor directive
defines
symbols we can use in our code
comments
lecture 21: pointers
memory
and addresses
my locker
analogy:
memory is
like a long hallway with a lot of lockers in a row
a pointer is
like having a locker number
its a silly
analogy, but it actually works in a lot of cases
pointers:
indirection
declarations
declaring a
pointer variable
data types
address-of
(&) and dereference (*) operators
and some
examples
lecture 22: more pointers
NULL
pointer
pointers
and function arguments
pointers let
us get values back from functions
we pass a
pointer to a function
the function
stores data at that address
we look
there when the function is done and find whatever the function stored there
scope
and extent
lifetime of
variables
we can have
a pointer to a variable that doesnt exist any longer
we need to
be careful of that
it is a Bad
Thing
lecture 23: arrays
What
is an array?
a bunch of
things of the same type, adjacent in memory
Array
declarations
simple
declarations
intitialization
implied-length
array declarations
lecture 23 (cont.)
Subscript
expression
the square
brackets []
used to
refer to a specific array element by number
numbers go
from 0 to N-1
not 1 to
N!!!
The
Rule
In a value context, the
name of an array of type array N of T becomes a pointer to T
this rule
will help simplify things
lecture 24: arrays 2
passing
arrays to functions
The Rule
says we actually pass a pointer
to the first
element
pointer
arithmetic
intended to
move around within an array
pointer
addition: pointer + integer
moves some
number of elements further on in memory
lecture 25: arrays 3
pointer
subtraction
pointer -
integer
moves some
number of elements back in memory
pointer
pointer
calculates
the number of elements between the two addresses
scores
example
PrintStats()
ComputeAverage()
InputScores()
lecture 26: strings
String
are stored in arrays of char
but theyre
more than just an array of char values
the end of a
string is signalled by a null character
ASCII value
0
\0
String
literals (string constants)
A sequence
of zero or more characters enclosed in double quotes
declaring
a string vs. declaring a pointer to a literal
similar, but
not identical
lecture 27: more strings
String
operations
things we
might want to do with strings
C doesnt
provide any as part of the language
we (or
somebody) has to write functions
The
C standard library
somebody
wrote those functions
textbook sections
Chapter
5
5.1 Pointers
and Addresses
5.2 Pointers
and Function Arguments
5.3 Pointers
and Arrays
5.4 Address
Arithmetic
this might
get a bit confusing
the example
is of a way to dynamically allocate memory
this is a
common thing to do in C
weve only
seen allocation via variable definitions
5.5
Character Pointers and Functions
5.6 Pointer
Arrays: Pointers to Pointers
we touched
on this a bit; read it!
textbook sections
Chapter
1
much more of
this should make sense now
1.1 to 1.3
are all pre-midterm material
1.4
discusses #define
1.5
Character Input and Output
this
involves some functions (like getchar()) that we never really talked about
but what it
does with strings is very appropriate
the subsections
on character, line, and word counting (1.5.2 1.5.4) are very useful
1.6 Arrays
this is all
useful
textbook sections
Chapter
1 continued
1.7
Functions
good reading
1.8
Arguments
1.9
Character Arrays
1.10
External Variables and Scope
this gets
into some of the scope and extent issues
exercises
You
may find the following exercises to be good practice:
chapter 1
these might
be hard, but you should be able to do them
chapter 2
we didnt
talk about bitwise operators (section 2.9)
but the
other exercises are reasonable
chapter 3
chapter 4
through
section 4.4
chapter 5
for the
sections we discussed
review questions
Questions
to help you review:
what is the
difference between the constants 7, 7, and 7?
how many
elements does the array
int a[5];
contain? How do we refer to the
first element? The last element?
what is
printed when the following code is executed?
char *format = %d %d %d %d\n;
int i = 3;
int *p = &i;
printf (format, p[0], *p+7, 3 * **&p, 7 * (p(p-2)));
review questions
Questions
to help you review:
What errors
can you find in the following declarations?
#define N 4
int a[N] = {0,
1, 2, 3, 4};
int b[N-5];
int c[N]
int d[2.0] =
{12, 24};
double *pd = a;
int *pi = c[1];
Double d = 3.1;
int N;
review questions
Questions
to help you review:
What will
this program print?
#include <stdio.h>
int main (void)
{
int
a[] = {0, 2, 4, 6, 8};
int
*p = a + 3;
printf (%d %d\n, *p, *p+1);
return 0;
}
review questions
Questions
to help you review:
It is very
convenient to use an array to handle a vector when doing geometric
computations. A vector in 3 dimensional space has 3 coordinates, and thus can
be represented by an array of three values. Given a vector (x, y, z), we might
declare an array like this:
double v[3];
The following two questions involve
vectors, and will use arrays like the ones declared above.
1) When
working with vectors, it is very common to have to compute a quantity called
the inner product, or dot product. For any two vectors, (ax, ay,
az) and (bx, by,
bz), the inner product is defined as:
<a, b> = ax*bx
+ ay*by + az*bz
Here is the declaration of a
function to compute and return the inner product. Write the code (function
definition) for this function.
double InnerProduct (double a[], double b[]);
2) The inner
product is a scalar quantity; another product of two vectors that is very
useful is the cross product, which is a vector.
a ΄ b = c
where:
cx = ay*bz
az*by
cy = az*bx
ax*bz
cz = ax*by
ay*bx
Here is the declaration of a
function to compute and return the cross product. Write the code for this
function.
void CrossProduct (double a[], double b[], double
c[]);
review questions
Questions
to help you review:
Here is a
general function to sort an array:
void swap (int
*, int *);
void bubble
(int a[], int n)
{
int i, j;
for (i = 0; i < n-1; ++i)
for (j = n-1; j > i; ==j)
if (a[j-1] > a[j])
swap(&a[j-1], &a[j]);
}
The sort
function uses a function named swap(), which we defined in lecture #22:
void
swap (int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
1) Modify the function bubble() so that it terminates after the first pass
in which no two elements are exchanged
2) Rewrite the call to swap() to use pointer addition, rather than the
subscript operator.
review questions
Questions
to help you review:
A palindrome
is a string that reads the same both forward and backward. Some examples are:
ABCBA
123343321
otto
C
Write a
function that takes a string as an argument and returns the int value 1 if the
string is a palindrome, and 0 otherwise.
review questions
Questions
to help you review:
Can you
explain why this program prints NO?
include
<stdio.h>
int
main (void)
{
char *p = abc;
char *q = abc;
if (a == b)
printf (YES\n);
else
printf (NO\n);
return 0;
}