article 1
1. what is an array?
A= is an aggregate data type that lets us access many variables of the same type through a single identifier.
2. which index would you use to acces the 2nd variable in an array?
A= myArray[1];
we start counting from [0], [1], [2]… etc.
3. what can you say about the size of an array?
A= we can declare how many variables we want in a fixed array like this:
int myArray[30]{};
Fixed arrays cannot have a length based on either user input or other value calculated at run time, and they cannot be changed. For that there is dynamic array which the length can be set and changed at runtime.
article 2
1. how can you initialize an array?
A= Array elements are treated just like normal variables, and as such, they are not initialized when created, one way to initialize an array is to do it element by element OR with an initializer list:
int myarray[3]{"Alpha", "Bravo", "Charlie"};
however if there are less initializers in the list than the array can hold, the remaining elements are initialized to 0 (zero initialization).
2. how can you get the size of an array in bytes?
A= by using sizeof operator, will return the total size of the array (array length multiplied by element size)
3. how can you use the function sizeof in order to calculate the number of elements in an array?
A= we can determine the length of a fixed array by dividing the size of the entire array by the size of an array element:
#include <iostream>
int main()
{
int myArray[]{1,2,3,4,5};
std::cout<<"the array has: "<<sizeof(myArray)/sizeof(array[0])
<<" elements"<<endl;
return 0;
}
4. what happens when you try to write outside the bounds of the array?
A= C++ does NOT checks to make sure that your indices are valid for the length of the array. It will get undefined behavior. For example, this could cause an overwrite on the value of another variable, or crashing the program.