Week 02
Week 02 | 1/35 |
Nerds You Should Know #2 | 2/35 |
The guy in the middle invented something you used today ... what?
... Nerds You Should Know #2 | 3/35 |
Doug Engelbart
|
|
The First Mouse | 4/35 |
Arrays | 5/35 |
An array is
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
... Arrays | 6/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 Two | 7/35 |
Convert code on previous slide into a complete program:
#include
Iteration over Arrays | 8/35 |
Two approaches to array iteration:
int i; int v[10]; for (i = 0; i < 10; i++) { printf("%d\n", v[i]); }
int i; char s[10];// somehow s[] is set to hold a C-string for (i = 0; s[i] != '\0'; i++) { printf("%c", s[i]); }
Array Initialisation | 9/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 Initialisation | 10/35 |
Write a program to determine answers to the following:
v[8]
v[8]
s
w[]
sizeof()
unsigned
unsigned long
Array Storage | 11/35 |
What exactly does an array declaration produce in memory?
An array declaration T a[N];
T
N
N
sizeof(T)
a
char buf[100];
buf
... Array Storage | 12/35 |
Example array declarations:
Exercise: Array Storage | 13/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 Pointers | 14/35 |
An analogy of how arrays are represented:
The array name acts like a pointer to the first element (s == &s[0]
... Arrays and Pointers | 15/35 |
On the previous slide, we suggested s == &s[0]
Types: s
char[]
&s[0]
char *
There is a close relationship between pointers and arrays
Arrays and Functions | 16/35 |
When an array is passed as a parameter to a function
int total, vec[20]; ... total = sum(vec);
Within the function ...
... Arrays and Functions | 17/35 |
Since functions do not know how large an array is:
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 functions | 18/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 Arithmetic | 19/35 |
A pointer variable holds a value which is an address.
C knows what type of object is being pointed to
sizeof
int a[6];// address 0x1000 int *p; p = &a[0];// p contains 0x1000 p = p + 1;// p now contains 0x1004
... Aside: Pointer Arithmetic | 20/35 |
For a pointer declared as T *p;
T
A
p = p + k;
k
p
A + k*sizeof(T)
k
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 Pointers | 21/35 |
An alternative approach to iteration through an array:
int *p; int a[6]; for (p = &a[0]; p < &a[6]; p++) { printf("%2d ", *p); }
... Arrays and Pointers | 22/35 |
Pointer-based scan written in more typical style
Note: because of pointer/array connection a[i] == *(a+i)
Exercise: Pointer-based Array Scans | 23/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 Strings | 24/35 |
One common type of multi-d array is e.g. char *argv[]
Each element of argv[]
char *
\0
... Arrays of Strings | 25/35 |
More detail on how argv
argv[]
... Arrays of Strings | 26/35 |
Normally, we treat the strings as atomic values, e.g.
for (i = 0; i < argc; i++) { printf("%s ",argv[i]); }
However, because argv
for (i = 0; i < argc; i++) { for (j = 0; argv[i][j] != '\0'; i++) { putchar(argv[i][j]); } putchar(' '); }
argv[i]
argv[i][j]
Exercise: The ohce Command | 27/35 |
Write a program called ohce
$ ./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:
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:
... 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 Operations | 31/35 |
Consider a datatype for square matrices defined as:
#define N 10 typedef int Matrix[N][N];
Write an operation on this datatype to:
stdout
3d Arrays | 32/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
cube
int
cube
int
... 3d Arrays | 33/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 Operations | 34/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:
stdout
cube[i]
Tips for Next Week's Lab | 35/35 |
Chars, strings and other arrays
strstr(s,sp)
sp
s
ctype.h
isupper(c)
islower(c)
prompt$ ./asci -upper 65 A 66 B 67 C 68 D ... 73 I 74 J 75 K 76 L ... ...
Produced: 11 Aug 2016