Introduction: Creating and Using an Array in C++

Hello again! A number of you have used an array before or even for those of you that have not used one, you may have wished that there was a convenient way to store multiple values without having to manually declare each one of them individually. An array is a great way to solve that predicament.

Today, I am going to go over some of the basics of a single dimensional array; how to create one, how to access it, and some of the ways you can manipulate the array. If you want to know about two dimensional arrays, check out my Instructable here.

Step 1: What We Will Eventually Need

It has been my experience that it is rather difficult to figure out if code is working as it should unless we have a visual representation to show us whether or not the code is working. One of the more fun ways to use an array (at least in my opinion) is to use it as a recording device to store a series of values. What I will use to demonstrate this is:

  • 3 breadboard buttons (the record button, a button to have its presses recorded, and a play button)
  • 3 LEDs (to indicate when each button has been pressed)
  • 6 small value resistors (I will use 220 Ohms)
  • Digilent's chipKIT Uno32 (the microcontroller to run the code)
  • Jumper wires
  • Breadboard

Step 2: Declaring an Array

Before we get too far, lets get back to the likely original reason you clicked on this Instructable: arrays.

I mentioned that arrays are nice ways to have groups of a particular type of value without having to individually name them; that is true. In a loose sense, arrays can be thought of like a hotel; there are multiple rooms that are all the same type of room (at least in this hotel), but each of them are individually accessible by number.

To declare an array in C++ you will need three things: what type of array it is (an array of integers, floats, chars, etc), the name of the array that you are calling, and how big your array is. For example, creating an array of 5 integers could look like:

int myFirstArray [5] ;

The reason you need to say how big your array is going to be is because the compiler will need to know how much memory it should set aside for the array. What this means for a recording, since we don't know exactly how long the recording will be, is that we will pick a maximum size that the recording can be.

Step 3: Adding Values to the Array

You likely noticed that when we declared our first array we did not say what the 5 integers actually are. This is allowed in C++, much like you can declare a single integer with just it's name and no specific value; the compiler just needs to know that there is a chunk of memory taken up by the integer. It doesn't really care what the actual value is (presuming its not too big), so it will assign a "0" to each of the 5 integers and then we can give them new values later when we start recording.

However, we could have assigned initial values to the array by doing the following:

int myFirstArray [5] = {1, 2, 3, 2, 1} ;

We could have only defined the first integers if we wanted; the compiler would have then assigned 0's to the last two of them.

Step 4: Accessing the Stored Values

To assign values to the array we need to specify which part of the array we are changing, which makes sense since we don't want a value randomly thrown in the array so that we can't find it. Array's number themselves from 0 to one less the total amount of elements in the array, so if you want to access the third value of the array you would type:

myFirstArray [2] = 35 ;

Unfortunately, you cannot assign multiple new values to an array at the same time; you have to assign them individually like we did in the above sample code. It's a bummer, but that's life.

We can also access the values of an array in a similar fashion. For example, if we wanted to have a delay in our program by using some form of a delay() function, we could do the following:

delay (myFirstArray [1]) ;

This would delay the program by however long the second value in the myFirstArray array is.

Step 5: Setting Up the Circuit- Powering the Buttons

Let's set up our circuit so we can test out what we learned.

Connect the negative power rail on the breadboard to one of the ground pins labeled "GND" on the chipKIT Uno32. Then run a jumper wire from the 3.3V power supply pin on the Uno32 to the positive power rail on the breadboard.

Now, place each of the three buttons slightly spaced apart on the breadboard with each button straddling the valley on the middle of the breadboard. Connect one leg of each button to the negative power rail by using one of the resistors. Connect the leg on the same side of the valley as the grounded led to the positive power rail.

Step 6: Setting Up the Circuit- LEDs and Digital Signals

For our visual portion of the circuit (which involves the LEDs, believe it or not) place an LED next to each button. Attach a 220 Ohm resistor from the cathode side of each LED to the negative power rail.

But while this looks nice, it does us no good if we can't give or receive any signals from our circuit. To remedy this, run a jumper wire from the anode side of each LED to their own digital pin on the Uno32 as well as a jumper wire from the grounded leg of each button to their own digital pin on the Uno32. I used pins 5, 7, and 9 for the buttons and pins 31, 33, and 35 for the LEDs.

Step 7: The Code

By nature of a recording, neither we nor the microcontroller know for sure how long the recording will be until we actually stop recording. Because of this, we will have guess how big our array will have to be to store our longest recording. We won't have to worry about our size limitation for our integers since the Uno32 has a 32 bit processor, unless you were planning to hold (or not hold) the button during the recording for more than 24 days at a time.

Presuming this is the case, we, or at least I, will set the recording array size to be 50 elements since I don't plan on recording more than 25 button presses (one for the button depressed and the light off, one for the button pressed and the light on). There is also an additional single dimension array that keeps track if the button was on or off so that way you are not just recording the length of time of the button being pressed (or not) while guessing if the button started out pressed or not at the beginning of the recording. You can download the code that I used for the Uno32 in the text file below. If you don't have it already, you will need the free MPIDE software to program the Uno32.

Step 8: Final Thoughts

Check out the demonstration video of this recording device on youtube! If you would like to learn how to create and use multi-dimensional arrays, you can learn how from my Instructable here.

To find out what other cool things are happening at Digilent, check out the Digilent blog for new updates every day!

Please feel free to ask me any questions that you may have and I will get back to you as soon I as am able.