10 - Arrays (simple)

Arrays are incredibly useful in programming. These were called "lists" in Snap! and Python. An array is a collection of elements that all have the same data type. We have already been using strings, which are collections of letters. The two examples below both create/declare a new string (array) and set it to the word "happy".

char string1[] = "happy";

char string2[] = { 'h', 'a', 'p', 'p', 'y', '\0'};        

        // Notice when creating an array of characters you need to include the null character at the end

        // This is not the case with any other primitive data type. To create an array of integers you simply write:        int collection[] = {1, 5, -22, 7};

        // When creating strings it is usually much easier to use the first method used to create string1

These both create an array of single characters. The first version is a short-hand way to initialize the array that only works for strings/words. The second method is the method by which arrays of other data types can be initialized (except you don't need to include the '\0' when dealing with other data types).

Declaring and Initializing an Array

The general format for creating an array is:

        // You can create/declare the array but not initialize it to any value

(data type)   (variable name) [size of array];

        // Or you can create/declare it and set its value at the start.

(data type)   (variable name) [size of array]   =   { element1, element2, element3 ... };        

Here are two examples of arrays being created and initialized:

int numbers[] = { 1, 5, 8, -11, 17};

double decimals[] = {1.0, 3.5, -2.2, 3.14, 0.0};

Since an array is not a primitive data type in C like integers, doubles, and characters, you cannot later set its value with the "=" operator. For example, the program below will cause an error:

int numbers[] = { 1, 5, 8, -11, 17};

numbers[] = { 1, 2, 3, 4, 5};                // ERROR!  You can't use the "=" operator with arrays.  You can only use "=" when looking at single elements of an array.

If you want to change the value(s) of the array, you have to do it element by element. FOR loops are incredibly useful when doing this. In the next section, we will look at how to access/set individual elements of arrays.

Accessing Elements of the Array

After creating an array, you use square brackets [ ] to access specific individual elements of the collection. Each element is able to be accessed, changed, and replace. You need to make sure that when you alter or replace it, that the result is of the same data type as before. Here is an example:

int numbers[] = { 1, 5, 8, -11, 17};            // Creating the array of integers called "numbers" that has 5 elements

printf( "%d", numbers[0] );                      // This prints the value of element zero, or the first element, of the array. This will print the integer "1"

numbers[2] = 5;                                        // This overwrites the value of element #2, from the previous value of 8 to a new value of 5.

Remember that just as in Python, the number of the elements starts at zero. Below is an an example program that creates an array of integers, then prints each element. It then prompts the user to change a value and reprints the values of the elements in the array.

Length / Size of an Array

One of the most useful functions when dealing with arrays is how to find the length of the array. In this way, you are able to use a loop to cycle from element #0 (the first one) until element #(length-1) (the last one). Cycling through an array is how you initialize, compare, search, sort, etc etc.

The "sizeof()" function actually tells you how many bytes of memory something takes up. You will combine two uses of this function to find the length of your array. First, you will find the sizeof(array) which will tell you how many integer number of bytes (memory) the array takes up. Second, you will find the sizeof(single element) to see how many integer number of bytes of memory a single element takes up. Since you know every element of the array is the same data type and size, you simply divide the total memory taken, by the amount taken by each element and you get the number of elements. The code is below:

int numbers[] = {2, 4, 6, 8, 10, 12};

int length_of_my_array = sizeof(numbers) / sizeof(numbers[0]); 

        // Notice that I could either find the size of a single element using "numbers[0]" or simply the sizeof the data type of the element

int numbers[] = {2, 4, 6, 8, 10, 12};

int length_of_my_array = sizeof(numbers) / sizeof(int); 

Below is an example program that has the user enter

Important Notes:

Common Mistakes

Coming soon ...

Practice Exercises

Coming soon ...