6. Sequencing w/ Arrays

6. Sequencing w/ Arrays

6. Sequencing w/ Arrays.

Did you know that The first ‘computer virus’ was born in 1983. It was written by a USC graduate student named Fred Cohen, and the virus could infect a computer, copy itself everywhere in the computer and even spread into other computers. He used this virus to hack into a mainframe computer and demonstrated its capabilities.

Did you know that The first ‘computer virus’ was born in 1983? It was written by a USC graduate student named Fred Cohen, and the virus could infect a computer, copy itself everywhere in the computer and even spread into other computers. He used this virus to hack into a mainframe computer and demonstrated its capabilities.

What is an Array in C?

An array is a collection of values that are of the same data type. Let’s see why we need arrays.

When we talked about variables and data structures, we discussed how we can use variables of different data types to store information. We learned about int, float, double and char. We also discussed how to use printf() to print the value stored in a variable. Let’s recall them quickly by going through the following example:

If we have a closer look at them, we can see a very important characteristic of them; they can store only one value in each. This means that if we need to store 10 integers, we will have to declare 10 integer variables and store our values in them. What if we want to store a thousand values? Will we have to declare a thousand variables?

By using arrays, we can easily store many values within a single variable. However, we should keep one thing in mind: arrays can consist of only one data type.

Creating an Array.

Just like other primitive data types, we can create and initialize arrays and change what’s inside of them.

array first picture.png

We have multiple ways to declare an array. The topmost one is the most complete way, where everything is fully defined and initialized. But it doesn’t always have to be. We can also use the other two ways to declare an array too.

Let’s see an example where we implement an actual array in our code: (we’ll be talking about the loop later).

The code above is completely editable, so give it a shot by changing the values in the editor and hit run to see your changed output.

int myArray[5] = {34, 29, 8, 49, 51};

In the above code, we first start by initializing our array, ‘myArray’. We specify that our array will hold at most 5 values. And then, just like when we are initializing a regular variable, we’re specifying what’s actually inside our array. We do that by opening curly brackets, and adding our elements and closing them at the end. Note how each element is separated by a comma. 

  • When we create an array, we need to give it a name. As usual, an array should also be named according to the laws we discussed in our variables tutorial.

  • Additionally, we have something new, the square brackets. This is what makes an array different from other variables. As an array can contain many values, we need to specify exactly how many values our array can hold. In this case, 5.

  • To initialize the array, we use curly brackets, and we can then put in our values inside the brackets as shown above.

We do not have to always initialize the array. But it is strongly suggested that the number of elements is specified inside the square brackets (i.e. myArray[10]).

for(int i=0; i<5; i++){
  printf("%d\n", myArray[i]);

This code is called for() loop. Check ‘loops’ tutorial to learn the breakdown of this code.

Let’s do a quick exercise before we continue our discussion below.

“,”url”:”https://ivlv.me/9zZlq”,”resolvedBy”:”embedly”,”floatDir”:null,”providerName”:”involve.me”}” data-block-type=”22″>

Accessing Array Elements.

Now we know how to declare an array, and how to initialize it. But how can we access the contents of our array? How can we change what’s inside of it? 

array table 2.png

To access an element inside an array, we need to use arrayName[index number].

For example, if we want to access the value 29 in our array, myArray, we need to use myArray[1]. 

But why ‘1’? It’s the second element from left, right? Yes, but in computer programs, we start with ‘0’. Therefore, if we try to observe what’s in our array. So, the indexes our array has are from 0 to 4. Not 1-5.

Note: Trying to access a non-existent element of an array will not give you a compile error. However, the program may crash when running or yield random values.

Have a look at the example below to understand how we can access what’s inside an array:

Now it’s time to test your knowledge. Let’s try to answer the questions below.

Quiz: A[6]= {4, 9, 3, 6, 4, 2}.

Changing Values of Elements in an Array.

When we define variables, we often need to change their values at certain places in our program. As an array contains many values, we can change them individually, without affecting the other elements.

In this example, let’s print the current value of the 0th element, change it and try to print the value in the same position.

We first define our array and initialize it. Then we declare a new variable, ‘number1’, and store the value in the myArray[0] position (0th element of our array), and print it. After that, we change myArray[0]’s value to 99, and repeat the same process to confirm that our array has been modified. In the end, we can see that the 0th element of our array has been changed from 34 to 99.

Displaying the Contents of an Array.

We recall using printf() statements whenever we wanted to display the value of a variable in previous tutorials. For arrays; however, things get a little bit difficult.

An array is a complex object where there are multiple values stored inside of it. So we cannot use statements like printf(“%d”, myArray); to print all the contents inside an array. Then how can we display its contents?

That’s when the Loops come in handy!

By now, we know that we can access an individual element of an array by following arrayName[index number] method. We can automate this by using a loop to do all the hard work for us by going through the array, one element at a time, printing them. 

We need to remember a few key points to consider when writing a loop to go through an array:

  • Arrays are indexed from ‘0’, not ‘1’.

  • The loop should NEVER try to access elements that do not exist in an array (i.e. If myArray has 5 elements, you should not write a statement such as myArray[5]. The indexing starts at myArray[0] and ends with myArray[4]). If not, the program will crash or produce bad results in your calculations.

Let’s make an array, and print the elements inside of it by using a for() loop:

The loop variable ‘i’ goes through values from 0 to 9 (including). Therefore, we can use the loop variable itself as an array index so we can pull the values out of the array; one by one.

When we look closely at our output, we can see the values in the array are printed until 51, and there are 5 more values which we don’t know where they came from. This is due to the loop trying to access elements that do not belong to the array. We should always want to prevent such things to happen by using the proper number in i<[number of elements].

Exercises fo you to try:

To better understand how arrays behave, try these tasks:

  • Make a char array where the elements are the letters of your name.

  • Write a for() loop to iterate through the array to print your name using the letters in the array.

  • Write a loop to add the numbers in an array and show the result.

Related Posts