bg287

csc110

Posted in csc110 by bnmng on 2010 04/15

Today:
One Dimensional Arrays (8.1 Bronson)
Declaration
Indexing
IO

Array Initialization

Homework: 8.3 Bronson.

April 22: Free Class.

One more program due & one more homework due.

What we can do with arrays.
You can use arrays as arguments to functions. But for the most part, you don’t do much with arrays as a whole. Instead, you work with individual elements in the array. What that means is, you don’t say “myarray=12”; Instead you say “myarray[0]=12” or “myarray[6]=27″ or whatever.

Array Initialization.
We can assign a series of values to the array at compile time.

You can initialize arrays as follows:
int grade[] = {98,87,38,50,97};
This will initialize and populate a 5 element array.

int grade[10] = {98,87,38,50,97};
This will initialize a ten element and populate the first 5 elements (grade[0] to grade[4]). It will also populate the remaining elements (grade[5] through grade[9]) with zero.

cstrings are arrays of characters. There are special features in c++ which allow us to do things with cstrings that we can’t do with other arrays. The last character has to be followed by a zero.

char name1[]=”mary”; //This works. It creates a 5 element array of ‘m’, ‘a’, ‘r’, ‘y’, and 0.
char name1[4]={‘m’,’a’,’r’,’y’}; //This creates an array BUT it’s not a cstring because it doesn’t terminate with a zero.
char name1[]={‘m’,’a’,’r’,’y’}; //Same as above. This will initialize a four variable array that is not terminated with a zero.
char name1[5]={‘m’,’a’,’r’,’y’}; //This works. Because it’s specified as a 5 element array, it will initialize the array with ‘m’, ‘a’, ‘r’, ‘y’, and 0.

Last program next Tuesday.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: