DAY19
Monday, 25 June 2012
Today we will talk about:
-
some errors that occur with array programming: uninitialized data;
referencing an "out of bounds" entry.
-
character arrays, which are related to strings.
-
variable length arrays, which allow us to choose the size of an array
as the program is running.
-
how vectors from linear algebra can be defined as an array in C;
what simple vector operations look like when implemented in C,
including the vector norm, dot product, and angle.
Programs and functions we might talk about:
-
array_bounds.c shows that, even if you
define an array A to be of size 5, the expression "A[6]" or "A[-4]" actually
is legal, and you might be able to tell what it means. This suggests how
C thinks of variable names as "addresses" in memory; then indexes just tell
you how far to walk from the basic address to get to the value you want.
-
char_array.c sets up an array of characters.
Although an array of characters is similar to a string, we will have to
wait until chapter 10 to see how to handle strings in a general way.
-
grades.c uses an array to store up to 100 grades.
It computes the average, and prints a summary of the grades by letter.
My first version of the program printed out nonsense, because I did not
initialize one of the arrays!
-
grades.txt a file containing 53 grades, followed
by a "-1" to signal the end. The grading program can read this data, if you
use a command like ./a.out < grades.txt.
-
how_big.c, shows how an array, which is normally
given a numeric dimension, can be declared with a variable dimension that
is entered by the user.
-
spring_ode2.c, the source code.
-
spring_ode2_output.txt,
the output file, which contains just the graphics data.
-
vector_2d.c allows the user to specify two vectors
in 2D, and computes the norms, dot and cross product, cosine and angle.
-
vector_3d.c considers three vectors in 3D space,
and finds their lengths, dot products, pairwise cosines and angles.
-
xv_time.png,
a time plot, created by gnuplot, of the spring displacement x and velocity v.
-
xv_phase.png,
a phase plot, created by gnuplot, of the spring displacement x and velocity v.
Last revised on 25 June 2012.