# Arrays

All of the data type we’ve covered so far have been great for storing “single” pieces of information.
An integer holds a single integer.
A char holds a single character, an int pointer holds a single address to an integer.
But what about holding a phone number?
You can argue that a 10 digit number, ie. 416-069-6969 can be assigned to a single integer. But this has some limitations attached, say we want to change one of the digits, this would require reassigning it to a different number. How about accessing the area code specifically?
To overcome this problem, we can use something called a data structure.
In programming there are many different kinds of data structures, the first one we will look at is called the array.
Here is what an array looks like: An array has a specified size, and a type.
The individual boxes in the array are called its elements, and they are indexed from 0 to n-1. (n being the size)
Note that in C, array elements are by default not initialized.
Let’s declare an array of 4 integers and initialize them all to zero.

```int A = {0,0,0,0}; //you can also do ={0}; the rest are automatically also filled with 0s.
```

The array A, is indexed from 0 to 3.
Hence we can access individual elements like so:

```int A = {0,0,0,0};

A = 1;
A = 2;
A = 3;
A = A + A + A + 2;
```

When you access any element from the array, such as A these are like regular integers and you can apply all the same arithmetic operations on them as with integers from before.

Run Time Index Access
The most useful feature of the array is arguably the fact that element indices can be computed at run time.
What does this mean?
Run-time refers to the actual time when our program is running. This is what’s happening after the code compiles and is executed.

This means if we have the following array:

```int A = {1,2,3,4};
```

We can ask the user to enter an integer (must be less than the size) and access any element of that array based on the integer.

```int A = {1,2,3,4};

int i;
scanf("%d", &i);

printf("%dn",A[i]);
```

As you can see if the user enters 2, we print out the element corresponding to the index 2.

Determining the Size
It is crucial to note that you MUST specify the size of the array. You cannot have the following code:

```int iSize;
scanf("%d", &iSize);

int A[iSize]; //CAN'T DO THIS
```

The size of the array must be known at run-time. We will however later look at how we can create arrays of variable size.

If you recall, I previously mentioned how there are different libraries that you can use with a preprocessor directive.
There is a useful library called the standard library and can be used by writing:

```#include <stdlib.h>
```

There are many function here, but a specific on that can be useful when working with arrays is called:
sizeof()
This is a function that will return the size of whatever you put into the brackets.
For example if we write:

```int iSize;
iSize = sizeof(int);
```

The variable iSize will be set to the size that an integer takes up in memory. This will differ from computer to computer. However on a 32-bit operating system should evaluate to 4. More specifically, 4 bytes.
A clever way to make use of this is to determine the number of elements of any array.
By definition an array will have n-elements all consecutively addressed and evenly spaced.
Look at the diagram below:
For this 6 element array:

```int A;
``` Since each integer is 4 bytes, you have 4 spaces between each element.
Calling sizeof() on A will give you 26.
If you say sizeof() on A, that will give you 4, since A is a single element of an integer array, thus it’s just like any other integer.
Note that you may be wondering why not just divide by sizeof(int), this is because we might not know the type of A, so using A insures that this will work as expected on any array
Hence dividing the total size by the size of a single element gives us the number of elements!

```int A;
int numElem;
numElem = sizeof(A) / sizeof(A);
//numElem = 6
```

Working With Arrays
Now that we’ve covered some basics of arrays, let’s have some fun with them.

Write a piece of code to declare an array of 26 characters and fill it with the English alphabet.

```char Alph;

for(int i = 0; i < 26; i++)
Alph[i] = 'a' + i;
```

Note, when you have a character: ‘a’, if you add 1 to ‘a’, you got ‘b’, and so on.
Using this you fill the entire array with all the characters.

A powerful way to use arrays is use is for keeping track of certain things.
To make this more clear, let’s take a look at an example.
Write a program that asks the user to enter a digit. Print out how many 0s, 1s, 2s, 3s… 9s are in the digit.
For example if the input is:
1029221
Output will be something along the lines of
1 0
2 1
3 2
..
1 9
You have 1 zero digit, 2 ones, 3 twos and a nine.
Before, the only way to have done this would have been to have used 10 different counters, for digits 0-9. Then using % operator, obtain last digit, and depending on what it is, increment the corresponding counter.
Now with arrays, we have a much more elegant solution.
Note that in a 10 element array, the indexes go from 0, for the first element, up until 9 for the last element.
This coincides perfectly with what we need. Each index will represent the digit we are counting. What about the actual element? That’s where we store the count. We initialize a 10 element array to 0, and for the last digit from input, obtained by % operator, we increment the element AT THAT INDEX.
Let’s look at the code.

```int KEY = {0}; //initializes all elements to 0
int UserIn;
int LastDig;

scanf("%d", &UserIn);

while(UserIn) //while we haven't gotten to the last digit
{
LastDig = UserIn % 10; //take the last digit
KEY[LastDig]+=1; //increment the element stored at the index

UserIn /= 10; //chop off last digit
}

for(int i = 0; i < 10 ; i++) //printf off how many of each digit
{
printf("%d ", i); //this line just prints the index
printf("%dn", KEY[i]); //this line prints how many of that number at the index
}
```

This same approach can be used to solve many different problems. For example, when we look at strings later, we’ll see how we can do this same thing to count how many times each letter of the alphabet appears in a given input.

Arrays & Pointers
I’m sure you’ll be glad to hear that we’re not done with pointers, not by a long shot.
Arrays and pointers go hand in hand. The first element of an array, A is A.
When you want to access the 4th or 7th or any element in the array, you write A or A, etc.
What is happening under the hood in C is that there is actually a pointer to the start of the array, and when you write A that pointer is being advanced until it reaches the desired element.
To make this more clear, let’s take a look at an example of how we can do this manually.

```int A = {0, 1, 2, 3, 4, 5};

A; //this value is 2
//let's use pointers now!
int *q = myAr; //q is an integer pointer, pointing to the beginning of the array

int i = 0;

while(i++ < 4) //here we can combine incrementing i
q++;       // and checking if its less than how much we want to increment
```

The Table below should help clear things up After we execute this line:

```q++;
```

Where q is pointing to the beginning of the array A, let’s see what happens. As you can see, q advances and points to the NEXT element.
So now if you dereference q:

```int temp = *q;
```

temp will be assigned the integer value 1.
This is identical to writing:

```int temp = A;
```