Lesson 15: Arrays in Action

[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)


[/spoiler]

[spoiler title=”Lesson Source Code”]

#include 
#include 
 
using namespace std;
int main(){
  //1. Showing how to iterate through a loop again
  //2. Show how to determine the size of an array
  //3. Pointer -- "Is the same as an array" when used with arrays
  //4. How to reverse an array.
 
  //Homework -- Make a second array, equal to the reverse of the first array
  //Bonus Create an array of 5 strings, allow the user to enter values for the strings, then output them in random order.
 
  const int size = 7;
  int myArray[size] = { 1,2,3,4,5,6,7 };
  // | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 
  // | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 
 
 
 
  for (int i = size-1; i >= 0; i--){
    cout << myArray[i] << endl;
  }
 
 
  cout << endl;
  system("PAUSE");
  return 0;
}

[/spoiler]

Homework: Using an array allow a user to enter 5 numbers, then using a loop find the total and average of the numbers and output them.



Arrays
Before I want to talk about arrays, I want to first talk a bit more about variables. One of the things that we’ve all wanted a user to be able to do at some point is: “Enter x variables, enter y number to stop” and then you do something with them. And the way to do this was probably with some crazy structure of a while loop, multiple ifs, tons of variables, tons of tests to make sure that stuff was used / input correctly. Arrays are the start of a solution to the issue. Arrays are a container that allow us to hold “like-type” variables (all the same type). So for instance, an array of ints can only hold ints. If we try to put in a double, it will actually work (in some IDE, others will trigger an error), but it will trim everything after your decimal place. With any of the types we’ve used thus far, we can make an array. This includes string, but that’s a little bit special since each individual string is a little bit like an array itself.


Declaring an Array
In C++ there are a few different ways to declare an array, in this lesson we looked at 2 of them. Size-known and initialized arrays. A size-known array is one where we use either a plain number (int) or a const int that is declared at compile-time to make the size of the array known. It bears mentioning that the following is INVALID

int size =0;
cin >> size; //user inputs 10
int myArray[size]; //Error, since size is not known at compile time

We have to be very careful that we adhere to using arrays in a way that doesn’t confuse this. There are ways to create array-like structures of unknown size that we’ll get into later, but these are not as simple or straight forward.

Some good examples of array declaration are:

int myArray[7];
int anotherArray[15] = { 0 }; // sets all 15 items (anotherArray[0] - anotherArray[14] equal to 0



What is an Array
All this talk of how to set them up, and how to make them, and so little talk of what they do. As I said earlier, an array is a sort of storage container that holds like type variables. But rather than just saying that, let me show you a case of when an array might be good (remember this example for later, we’ll touch it again in classes).

//THIS IS BAD CODE
double price1 = 2.09;
double price2 = 3.19;
double price3 = 5.99;
double price4 = 1.05;

int itemNum1 = 1;
int itemNum2 = 2;
int itemNum3 = 3;
int itemNum4 = 4;

string itemName1 = "Item 1";
string itemName2 = "Item 2";
string itemName3 = "Item 3";
string itemName4 = "Item 4";

The smarter way to do this (until we get to objects) is to use arrays to contain the similar data. The above example become very complex if we ever need to edit item numbers, change prices for a sale, or output them in some way that makes sense. A better example looks like this:

double prices[4]    = {2.09, 3.19, 5.99, 1.05}; //Prices
int itemNumbers[4]  = {1,2,3,4}; //Item Numbers
string itemNames[4] = {"Item 1", "Item 2", "Item 3", "Item 4"}; //Item Names

for (int i=0; i<4; i++){ 
    cout << "ItemNumber: " << itemNumbers[i] << "-"  << itemNames[i] << " :" << prices[i] << endl;
 }

output:

ItemNumber: 1 - Item 1 : 2.09
ItemNumber: 2 - Item 2 : 3.19
ItemNumber: 3 - Item 3 : 5.99
ItemNumber: 4 - Item 4 : 1.05


Off-By-One error
The off by 1 error is going to be your all-time worst enemy. These are common with loops and have been discussed there, but they are equally common for arrays. Arrays are 0-indexed. Meaning the FIRST location in an array is array[0] rather than array[1] like a lot of people seem to try. This is going to be the primary error you run into and it will present as an Array Bounds Error (or something along those lines)