Meeting #7 (C++: Arrays & Vectors)

Arrays (by example):

Arrays allow us to store variables in lists. This is useful for storing sets of data that are similar (ex. getting the user input 10 times).

Creation General syntax:

type name[size];
type name[size] = {data1, data2...};

Example:

int array[10]; // empty integer array of size 10
int array2[5] = {10, 20, 10, 5, 69};

The first line creates an empty array of integers (go back to previous post if you forgot) called “array”. By default, all of the values are set to zero. You can see a graphical representation below:

Image result for array

You’ll also notice that arrays start at zero, meaning that when refer to the first element in the array, we are talking about the value at the index zero. On the second line, we set some values when we create the array. This results in our array already having values when created, so we don’t need to set them later.

Now, we need to be able to use our array, and get elements from it! The syntax is similar to using a variable.

Syntax:

name[index]; // access array element
name[index] = value; // set value of array at specific index

Example:

int array[10]; // create array for 10 integers
array[0] = 5; // set value at 0th index to 5
cout << array[0] << endl; // print value at 0th index
array[1] = -1; // set value at index 1 to -1
int var = array[1]; // create variable "var" and set its value

array[10] = 1; // BAD! This is outside of the array (the indexes are // between 0 and 9)

As you can see above, using arrays are very simple. However, in C++, arrays can be the source of many headaches. C++ does not prevent you from accessing indexes outside of the array bounds. This means that once you access or try to set values outside of the array, you run into undefined behaviour.

The computer may either give garbage data, or simply crash with a segmentation fault (error that occurs when programs access memory that is not theirs). If you want to read more about this, visit: https://www.geeksforgeeks.org/accessing-array-bounds-ccpp/

Combining Arrays with Loops

Arrays go hand in hand with loops, since loops are able to easily set and access the values of an array.

Here is an example involving setting the values of an array to their indexes.


#include <bits/stdc++.h>
using namespace std;

int main () {
int array[10];
for (int i = 0; i < 10; i++) { // loop from 0 - 9 for i
array[i] = i; // set value of array to i at index i
cout << "We set array[" << i << "] to " << array[i] << endl;
}
}

As you can see, we can put variables in our indexes as well. This example can be very useful for some of the challenge problems below. Let’s go over another example. If you forgot about loops, you can visit our previous post that contains more information on them (Meeting #6 (C++)).

A commonly used function of arrays and loops together is getting sets of user data. This is important for getting input in competitive programming problems.

#include <bits/stdc++.h>
using namespace std;

int main () {
int size; // size of array variable
cout << "input the size of the array" << endl;
cin >> size; // get input and put into size

int array[size]; // Create an array of size "size"

// get input from user "size" times and store in array
for (int i = 0; i < size; i++) { // loop from 0 - size-1
cin >> array[i]; // get input and put it into array[i]
cout << "We set array[" << i << "] to " << array[i] << endl;
}

// print user's input (by looping through stored values)
for (int i = 0; i < size; i++) { // loop through array
cout << array[i] << endl; // print contents of array[i]
}
}

The Mighty Vector (easier arrays):

Sometimes, we want to create arrays that have variable sizes (the size can change after being created). This can be done using something called a vector (similar to ArrayList in Java).

Creation Syntax:

vector<type> name;

// example
vector<int> array;

// create with a set sized
vector<int> array(10);

Don’t worry about the angled brackets just yet, they are used to specify the type of variable that you are storing in the vector. The usage of vectors is quite similar to arrays, however, it has the added feature of being able to add new elements to an array. This means that the array can grow larger after being created.

vector<int> array;

array.push_back(10); // add new element to array
cout << array[0] << endl; // use element in array
array[0] = 20; // set value in array
array[1] = 20; // WRONG! While vectors can grow in size, this does
// not mean they have more indexes than elements!
O

One benefit of vectors is that you have access to sort(), which is a utility that allows you to sort the values in an array (increasing order)!

vector<int> array; // create array and add values to it
array.push_back(3);
array.push_back(5);
array.push_back(4);
array.push_back(1);

sort(array.begin(), array.end()); // this sorts the array in
// ascending order

Functions (challenge)

We will be covering functions next week. If you want to get a head start, here is a link: https://www.tutorialspoint.com/cplusplus/cpp_functions.htm

Challenge!

Try out these problems on DMOJ! Be sure to register first. 

https://dmoj.ca/problem/ccc18j1

https://dmoj.ca/problem/ccc14j2

Challenge: https://dmoj.ca/problem/ccc16j2

Challenge: https://dmoj.ca/problem/ccc16j3

Published by

Computer Club Executives

The account of the Computer Club Executives