Schedule‎ > ‎

Lab 14: Arrays


  • Practice using Arrays
  • Write a function to append data to an array
  • Write a function to insert data into an array

^ top

Academic Honesty

Read the Scholastic Honesty Policy and Assignment Integrity policies of the syllabus. Here are some clarifications for this particular assignment:

  • You are expected to work either by yourself.
  • You may NOT give a copy of your code to any other student nor post it online.
  • You must breadboard the circuits yourself.
  • You may NOT look at another student's code until you complete and submit this assignment.
  • You may get help from people if you get stuck, but only if they show you like examples and not show or tell you the exact code to type.

Part 1: Putting Data in C-Style Arrays (array_append.ino)

Make a function that prints out the contents of an array. You'll use it in this lab. It should look like this: 

void print_array(int numbers[], int size) {
  cout << "{ ";
  for (int i = 0; i < size; i++) {
    cout << numbers[i] << " ";   
  cout << "}" << endl;

Consider the following code:

int size = 6;
int numbers[] = {12, 14, 16, 2, 10, 200};
print_array(numbers, size);

The output of the code looks like this: 

{ 12 14 16 2 10 200 }

Simple enough. What if you wanted to add a number to the end of the numbers[] array? C and C++ have no way to resize it. The only way to do that is to create a new array and copy all of the numbers, then add the new number to the end. 

// Make a bigger array
int more_numbers[size+1];
// Copy the contents of the old array into the new one
for (int i=0; i<size; i++) {
  more_numbers[i] = numbers[i];

// Add my new number
more_numbers[size] = 32;

Add this code to your code and use print_array to verify that you've added 32 to the list. Once you're convinced that it works create a function that adds a number to the end of a list:

void append_number(int numbers[], int size, int newnum) {

    // Append a number using the code above. 
    print_array(more_numbers, size+1);

You now have a function that appends to an array.  Save it as array_append.ino.

Questions: (put answers in your Canvas comment area)

  1. Arrays can be resized [true or false]?
  2. After you call append_number is the array in the argument bigger, why or why not?

Part 2: Inserting a Number (array_insert.ino)

What would the code look like if you wanted to add a number into the middle of the array? The element to be added would displace the elements behind it in the array. 

Here's code that inserts an element into an array:

  // Make a bigger array
  int more_numbers[size + 1];

  // Copy the numbers up to the place to insert (array index 5)...
  for (int i=0; i<5; i++) {
    more_numbers[i] = numbers[i];

  // Place the new number into the array 
  more_numbers[5] = 32; 

  // Copy the rest of the numbers to their new homes...
  for (int i=5+1; i<size+1; i++) {
    more_numbers[i] = numbers[i-1];

Run this code and verify that it inserts the number "32" into the fifth index. The output should look like this:

{ 12 14 16 2 10 32 200 }

Now place the code into a subroutine: 

void insert_number(int numbers[], int size, int place, int newnum) {

  // insert the above code here... 
  // Hint: 5 becomes place and 32 becomes newnum

  print_array(more_numbers, size+1);

Check your work. If you run the following code: 

void loop() {
  int size = 6;
  int numbers[] = {12, 14, 16, 2, 10, 200};
  print_array(numbers, size);
  append_number(numbers, size, 99);
  insert_number(numbers, size, 3, 99);

The output should be:

{ 12 14 16 2 10 200 }
{ 12 14 16 2 10 200 99 }
{ 12 14 16 99 2 10 200 }

Questions (put answers in your Canvas comment area)

        3. What arguments to insert_number() would do the same thing as append_number()?
        4. What arguments to insert_number() put a number at the beginning of the array?
        5. If you have insert_number() do you need append_number()?

Save your file as array_insert.ino and submit it to Canvas.

Grading Criteria (20 pts)

Note that function headers must have the @param and @return comments filled in.

Each Part:  10 points  (Header 1pt, Function Headers 1pt, Compiles 1 pts, Proper Formatting 1 pt,  Works as Specified 4 pts, Answers to 5 Questions)

Part 1:  10 pts total

Part 2:  10 pts total

^ top