Computer Science E50b . Tuesday March 2, 1999 . 7pm . Science Center 111

ARRAYS

What is an array?

How to create arrays (declaration)

object-type name-of-array [size-of-array];

Ex:
int ps1-grades[36]; // creates an array called ps1-grades with 36 elements of type int
char name[5]; // creates an array called name with 5 elements of type char

How to access the value of an object stored in an array

Suppose you want to access the 1st element of the array name, and store this character in a variable c:
char c = name[0];

How to assign values to different objects stored in the array

There are different things you can do at different times to achieve this:

Multi-dimensional arrays

Multi-dimensional arrays are just like 1-d arrays, except that there are more than one [].
Ex: declaration of a 2-d array:
object-type name-of-array [size1] [size2]; // you could think of this as a table, size1 being the number of columns and size2 being the number of lines, this becomes tricky to visualize with more than 3-d
Would this be useful for a tic-tac-toe game for example?

Passing arrays to functions (Deitel p.241)


Ex:
void modifyArray (int a [], int size) { definition of modify array}
void cantModifyArray (const int a[], size) {}
int main() {
const int MYSIZE = 5;
int myarray[MYSIZE];
modifyArray (myarray, MYSIZE);
cantModifyArray (myarray, MYSIZE);
}

Some things to watch for

Some things to think about

When you declare an array, the name of the array is a pointer to the first element of the array...