Part I
- What is an array?
An array is a way to store multiple same type variables with a single identifier. They are declared using square brackets []. E.g.
int x[5]; //generates array with 5 elements
-
Which index would you use to access the 2nd variable in an array?
The index of elements starts with 0. So, the 2nd variable would be at index 1.
-
What can you say about the size of an array?
For fixed arrays, the size must be known and cannot be adjusted later on. Dynamic arrays can determine its length after compiling.
In a fixed array, the size is determined by the number of elements.
Part II
-
How can you initialize an array?
Using a initializer list
int prime[5]{1,3,5,7,11}; // uses initializer list to initialize the fixed array
- How can you get the size of an array in bytes?
By using
sizeof(array);
- How can you use the function size of in order to calculate the number of elements in an array?
The number of elements in an array can be calculated by dividing the size of the entire array by the size of the array element.
int array[] = {1,2,3,4,5};
cout << ((sizeof(array)/sizeof(array[0])) << endl;
- What happens when you try to write outside the bounds of the array?
Writing outside the bounds of the array will cause unidentified behavior.