2330 : Introduction to Programming in C

June 27th - Arrays

Today we'll be covering arrays. The course web site could not be updated the night before class due to your instructor's internet connection problem, and thus lecture notes cannot be included now. Anything needed for class use will be updated here soon.
Lecture notes
Below are some of the syntax related to arrays for your reference, from the lecture notes.
Declaring arrays
Declaring an array is simple, just like declaring variables, but with square brackets:

int arrayname[5]; // any positive number can go in place of 5
char someword[5]; // same, character array also forms a string
Accessing each individual element of an array
Accessing individual elements is easy with square brackets. Just remember the first element starts at 0. So the first element of "arrayname" above is arrayname[0], and the last element of it is arrayname[4]. You use these elements just like variables. You can use them wherever you can use a variable of that same type.
Initializing arrays
First way to initialize arrays:

int arrayname[5] = {1, 4, 3, 7, 8};

Second way to initialize arrays:

int arrayname[5];
for (i = 0; i < 5; i++)
    arrayname[i] = 0; // sets them all equal to zero
Character array to form string
You can use a character array to form a string easily.

char word[5] = {"Hello"};

Homework assignment
Textbook page 418, problem #5. The problem is long, but it does explain completely how the entire algorithm work. Your job is to translate this into C code to make it work. Use a for loop to set up an array that has values from 2 to 1000, don't be silly and try to set it up manually one by one! =)
And read the chapter on pointers, Ch. 13.