Lesson 24 - Arrays

Arrays are essentially a list of a variable type. There are integer arrays, float arrays, boolean arrays, basically you can make an array for any data type. Array’s have a fixed size, that is to say, they can only hold a set user-decided number of values, but the values within the array can be changed.


One of many ways you can declare an array is:

data_type array_name[array_size];



For example, if I wanted to declare a string array with five “slots” called fruits, I’d write the following:

string fruit[5];


This array however, is empty. All of the array slots don’t have a value in them. We can add a value to an array slot by doing:

array_name[slot_number] = value



For our fruit array that could look like:

fruit[1] = "Apple";


If “null” represents no value, our array now looks like this:

{null, “Apple”, null, null, null}


VERY IMPORTANT NOTE: “Apple” is in the second slot because the first slot of an array has a slot number of 0. Below is an array where the value of each slot is it’s own slot number:

{0, 1, 2, 3, 4}

{null, “Apple”, null, null, null}






If I had an integer array called nums that had the following values

{73, 98, 86, 61, 96} 

This is how you would access each value:



In some cases you may need to define each slot value individually like we did with “Apple”. However, you can also preset the value of each slot in an array by defining it like such:

string fruit[5] = {"Banana", "Apple", "Potato", "Orange", "Lemon"};


If you want to change a value in the array afterwards, like to correct “Potato” you can just re-set the value like:

fruit[2] = "Lime";

{“Banana'', “Apple”, “Lime”, “Orange”, “Lemon”} is our final fruit array.



Arrays - An Exercise

Given a size 7 integer array called n, write a program that will find the largest and smallest value in the array and assign them to two separate variables maxNum and minNum respectively.

Note: Don’t worry about taking input, assume the array is defined within your main loop as

int n[7] = {values};

Answer:

#include <iostream>

using namespace std;

int main(){
    int n[7] = {2, 4, 6, 7, 1, 3, 4};
    int maxNum;
    int minNum;

    maxNum = n[0];
    minNum = n[0];
    for(int i = 0; i < 7; i++){
        maxNum = max(n[i], maxNum);
        minNum = min(n[i], minNum);
    }
    cout << "Max Number: " << maxNum << endl;
    cout << "Min Number: " << minNum << endl;
}


NOTE: The max function picks the bigger number and the min function picks the smaller number.


Arrays - VEX Applications

In VEX, arrays are commonly used to hold a collection of related preset values. For example, if the game required your lift to reach several different heights, you could make an array of encoder/potentiometer values that correspond to the needed lift heights. 


This will be written in pseudo-code (not fully fleshed out code, written just to demonstrate the concept) but will show how you could write code to cycle between these lift heights:

void moveLiftTo(int liftHeight){
    //Code that moves the lift to the "liftHeight" position
}

void opcontrol() {
    int liftHeightCount = 0;
    int liftPresets[4] = {0, 200, 400, 600};

    while (true) {
        if(controller.get_digital(DIGITAL_L1)){ //Raises lift to next position
            liftHeightCount += 1;
        }
        else if(controller.get_digital(DIGITAL_L2)){ //Lowers lift to prev pos
            liftHeightCount -= 1;
        }
        if(liftHeightCount > 3) liftHeightCount = 0; //Resets to 0 pos if fully cycled
        if(liftHeightCount < 0) liftHeightCount = 0; //Keeps liftHeightCount >= 0
       
        moveLiftTo(liftPresets[liftHeightCount]);
    }
}