Week 02


Week 021/35

Things to Note ...

In This Lecture ...

Coming Up ...


Nerds You Should Know #22/35

The next in a series on famous computer scientists ...
 
 

The guy in the middle invented something you used today ... what?


... Nerds You Should Know #23/35

Doug
Engelbart

 
  • 1940's, trained as Electrical Engineer
  • 1950's, worked at NASA Ames and SRI labs
  • 1960's and 1970's, invented ...
    • the mouse
    • 2-dimensional display editing   (e.g. vi, emacs)
    • windows-based interfaces   (e.g. Mac, Windows)
    • etc. etc. etc.


The First Mouse4/35


 

 
Photo circa 1963


Arrays5/35

An array is

Examples:

int a[20];     // array of 20 integer values/variables
char b[10];    // array of 10 character values/variables

printf("%d", a[0]);  // print first value in a[] array


... Arrays6/35

Larger example:

#define SIZE 20

int i;         // integer value used as index
int v[SIZE];   // array of 20 integer values

for (i = 0; i < SIZE; i++) {
	v[i] = twoToPower(i);
}
for (i = 0; i < SIZE; i++) {
	printf("v[%02d] = %d\n", i, v[i]);
}


Exercise: Powers of Two7/35

Convert code on previous slide into a complete program:


Iteration over Arrays8/35

Two approaches to array iteration:


Array Initialisation9/35

Arrays can be initialised by code (as in previous example).

Alternatively, can specify an initial set of values in declaration.

Examples:

int v[8]  = {1, 2, 4, 8, 16, 32, 64, 128};

char s[4] = {'a', 'b', 'c', '\0'};

int w[]   = {5, 4, 3, 2, 1};

In the last case, C infers the array length   (as if we declared w[5]).


Exercise: Array Initialisation10/35

Write a program to determine answers to the following:

Hint for last one: sizeof() operator (returns a value of type unsigned or unsigned long)


Array Storage11/35

What exactly does an array declaration produce in memory?

An array declaration   T a[N];    (where T is a type; N is a count)

E.g.   char buf[100];


... Array Storage12/35

Example array declarations:

[Diagram:Pic/arrays-small.png]


Exercise: Array Storage13/35

Write a program to determine the storage size of the following:

Types:

char   int   float   double

Variables:

char s[10];  int n;  int a[6];  char *cp;  int *ip;

Also, work out in memory where each variable is located.

Do this for variables declared as local, then as global.


Arrays and Pointers14/35

An analogy of how arrays are represented:

[Diagram:Pic/array-small.png]


The array name acts like a pointer to the first element   (s == &s[0])


... Arrays and Pointers15/35

On the previous slide, we suggested   s == &s[0]

Types:   s is char[],   &s[0] is char *

There is a close relationship between pointers and arrays

Some consequences of this relationship:


Arrays and Functions16/35

When an array is passed as a parameter to a function

Example:

int total, vec[20];
...
total = sum(vec);

Within the function ...


... Arrays and Functions17/35

Since functions do not know how large an array is:

So, the previous example would be more likely done as:

int total, vec[20];
...
total = sum(vec,20);

Also, since the function doesn't know the array size, it can't check whether we've written an invalid subscript (e.g. in the above example 100 or 20).


Exercise: Array functions18/35

Implement a C function to sum the values in an integer array.

Use the following function prototype:

int  sumArray(int array[], int nelems);

Implement an array to print a C string, character at a time.

Use the following function prototype:

void  printString(char string[]);


Aside: Pointer Arithmetic19/35

A pointer variable holds a value which is an address.

C knows what type of object is being pointed to

Example:

int a[6];   // address 0x1000
int *p;
p = &a[0];  // p contains 0x1000
p = p + 1;  // p now contains 0x1004


... Aside: Pointer Arithmetic20/35

For a pointer declared as   T *p;   (where T is a type)

The value of k can be positive or negative.

Example:

int a[6];   (addr 0x1000)       char s[10];   (addr 0x2000)
int *p;     (p == ?)           char *q;      (q == ?)
p = &a[0];  (p == 0x1000)       q = &s[0];    (q == 0x2000)
p = p + 2;  (p == 0x1008)       q++;          (q == 0x2001)


Arrays and Pointers21/35

An alternative approach to iteration through an array:

Example:

int *p;
int a[6];
for (p = &a[0]; p < &a[6]; p++) {
	printf("%2d ", *p);
}


... Arrays and Pointers22/35

Pointer-based scan written in more typical style

[Diagram:Pic/pointerscan-small.png]


Note: because of pointer/array connection   a[i] == *(a+i)


Exercise: Pointer-based Array Scans23/35

Rewrite the two function from before:

int  sumArray(int array[], int nelems);

void  printString(char string[]);

to use pointer-based scans.

Do the function interfaces need to change for this?


Arrays of Strings24/35

One common type of multi-d array is e.g. char *argv[]

[Diagram:Pic/argv-small.png]

Each element of argv[] is


... Arrays of Strings25/35

More detail on how argv is represented:

[Diagram:Pic/argv2-small.png]


argv[] is in the stack; the strings are in the global data area


... Arrays of Strings26/35

Normally, we treat the strings as atomic values, e.g.

for (i = 0; i < argc; i++) {
    printf("%s ",argv[i]);
}

However, because argv is an array of arrays, we could access the individual characters in the strings, e.g.

for (i = 0; i < argc; i++) {
    for (j = 0; argv[i][j] != '\0'; i++) {
        putchar(argv[i][j]);
    }
    putchar(' ');
}

argv[i] is a character array; argv[i][j] is one character from that array.


Exercise: The ohce Command27/35

Write a program called ohce that

Examples of use:

$ ./ohce these are some args
sgra emos era eseht
$ ./ohce even 123 gets  reversed
desrever steg 321 neve
$ ./ohce able was I ere I saw elba
able was I ere I saw elba


2d Arrays (Matrices)28/35

An array of N arrays, each of size M, is a matrix.

Examples:

[Diagram:Pic/matrices-small.png]


Note:   q[0][1]==2.7    r[1][3]==8    q[1]=={3.1,0.1}


... 2d Arrays (Matrices)29/35

Storage representation of matrices:


[Diagram:Pic/matrices2-small.png]


... 2d Arrays (Matrices)30/35

Iteration can be done row-by-row or column-by-column:

int row, col;   int m[NROWS][NCOLS];

//row-by-row
for (row = 0; row < NROWS; row++) {
    for (col = 0; col < NCOLS; col++) {
        ... m[row][col] ...
    }
}
// colum-by-column
for (col = 0; col < NCOLS; col++) {
    for (row = 0; row < NROWS; row++) {
        ... m[row][col] ...
    }
}

Row-by-row is the most common style of iteration.


Exercise: Matrix Operations31/35

Consider a datatype for square matrices defined as:

#define N 10
typedef int Matrix[N][N];

Write an operation on this datatype to:


3d Arrays32/35

Arrays can be declared with more than two dimensions, e.g.

int cube[10][10][10];     // 3 dimensional
int hyper[4][5][6][7];    // 4 dimensional

The definition of cube can be interpreted as:


... 3d Arrays33/35

Iteration over an N-d array requires N nested loops:

int i, j, k;   int cube[N][N][N];

// for each element (matrix)
for (i = 0; i < N; i++) {
    // for each row of current matrix
    for (j = 0; j < N; j++) {
        // for each element in current row
        for (k = 0; k < N; k++) {
            // process cube[i][j][k] (an integer)
        }
    }
}


Exercise: Cube Operations34/35

Consider a datatype for data "cubes" defined as:

#define N 4
typedef int Cube[N][N][N];

Write an operation on this datatype to:


Tips for Next Week's Lab35/35


Chars, strings and other arrays


Produced: 11 Aug 2016