Please send questions to st10@humboldt.edu .

CS 131 - Week 11 - Lecture 1

Introduction to 1-dimensional (static) arrays in C++

...a C++ array is a lower-level, more limited analogue
   to Racket's lists;

...in Racket, you make new lists using cons to
   create a new list that adds a first element to another
   list;

...for a C++ array, an array is of a set size, that
   you have to give in advance;
   (so the C++ compiler can set aside a contiguous
   consecutive block of memory for that many elements...)

...in Racket, the elements in a single list can be
   of different types;
...in C++, every element in a single array is of
   the SAME type;

To declare an array in C++ (static 1-dimensional array)
you must tell the compiler 3 things: 
1. the array's name 
2. the type of its elements
3. how many elements there are

<type> <array_name>[<INT_SIZE>];

course style: WHEN you know the size of the array in
advance (you don't always...), and it is a set value,
you SHOULD use a named constant for its size...

const int NUM_STUDENTS = 37;
int quiz_grades[NUM_STUDENTS];

const int NUM_BOAS = 10;
boa preserve_pop[NUM_BOAS];

const int NUM_ROWS = 3;
const int NUM_COLS = 5;
int two_dims[NUM_ROWS][NUM_COLS];

*   accessing array elements

    in racket, we accessed the elements in a
    list (at the most basic level) using first
    and rest;

    in C++, the most basic way to access an 
    array's elements is to give the name of
    the array, and which element you want in
    that array, indicated by giving an INDEX into
    the array
    (essentially the number of steps from the beginning
    that element is...)

    (under the hood, C++ really just stores, for each
    array, WHERE the array begins --
    it then COMPUTES the address of an array element
    using the index you give it!)

    array_name[index]

    so, if it stores how many steps from the beginning
    the element is --
    the FIRST element in every C++ array has index 0

    (so, notice: the LAST element's index is one less
    than its size!!)

*   I can now set the value in this array element,
    or use it.. or both...

    quiz_grades[0] = 97; // set the element at index 0
                         //   in array quiz_grades to
			 //   the value 97
    ...
    return quiz_grades[0];

    ...I'm somewhere else now, and have redeclared
       quiz_grades...

    quiz_grades[0] = 97;
    quiz_grades[1] = quiz_grades[0] - 3;
    quiz_grades[1]++;  // quiz_grades[1] is now 95
    quiz_grades[1] = 37;

*   just like you can't reasonably assume what is
    in a C++ variable that you have declared but not
    set,

    ...you can't (or at least shouldn't) 
    reasonably assume what is in
    a C++ array that you have declared but not set;

    ...you need to set its elements to initial values,
    or INITIALIZE it.

    *   what if I wanted to set all the averages in
        stu_avgs to initially be 100.0?
    
stu_avgs[0] = 100.0;
stu_avgs[1] = 100.0;
stu_avgs[2] = 100.0;
...this is getting OLD!

...we want REPETITION!
...we used recursion for this in Racket,
   we'll use iteration right now in C++
   (and you can use iteration in Racket,
    and you can use recursion in C++,
    but this is another way to repeat...)

most basic C++ repetition statement:

while statement/while loop

while ( <bool_expr> )
   <statement>;

or more typically, replacing that single statement with
a block,

while ( <bool_expr> )
{
    <statement>;
    ...
    <statement>;
}

the semantics: look at the flowchart we drew on
the board...

or: trying to say it in prose:
1. the bool_expr is evaluated
2. if it is true, do its statement (or block of
   statements)
   ...then return to 1

   else, you're done -- continue with the next statement
   after the loop...

here is an example while loop, setting every box
in stu_avgs to 100.0...

// set up a local variable, a counter, to keep track
//    of how far I've gotten

int count = 0;

while (count < NUM_AVGS)
{
    stu_avgs[count] = 100.0;
    count = count + 1;
}

...this is a classic count-controlled loop,
called that because a local variable called
a counter is controlling the loop, essentially;

...see how changing the count variable makes
   this work?