Arrays in C++ - Reading Assignment

  1. An array is a data type to access many different elements of the same type in one identifier.
  2. index 1
  3. An array will have a fixed size.

1 . You can initialize an array element by element or use an array initializer list.
2. The sizeof() function can get the size of the array in bytes
3. Divide the sizeof(array) by sizeof(bytes)
4. can cause problems in app

1 Like

FIRST PART
1. What is an array?
An Array is a indexed variable that aggregates various variables of the same data type.

2. Which index would you use to access the 2nd variable in an array?
To index the second variable os an arrays we use the index 1 -> array[1]

3. What can you say about the size of an array?
The size of an arrays are the number os elements it has. In a fixed sized array it needs to be declared at compile time, and cannot be changed, otherwise it will throw an error.

SECOND PART

  1. How can you initialize an array?
    a) Define it’s data type (int, enum, bool, etc);
    b) Give a name to the array;
    c) Declare de length of the array between “[]” (for fixed size);
    d) Give it values (using a comma to seperate them).

  2. How can you get the size of an array in bytes?
    sizeof(array);

  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    sizeof(array) divided by sizeof(array[0]) = number of elements

  4. What happens when you try to write outside the bounds of the array?
    It will write in the next byte of the memory and will result in an undefined behavior that can cause serious crashes

2 Likes

Part I

  1. 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
  1. 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.

  2. 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

  3. 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
  1. How can you get the size of an array in bytes?
    By using
sizeof(array);
  1. 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;
  1. What happens when you try to write outside the bounds of the array?
    Writing outside the bounds of the array will cause unidentified behavior.
1 Like

1. What is an array?
An aggregate data type that lets us access many variables of the same type through an identifier

2. Which index would you use to access the 2nd variable in an array?
subscript operator[1]

3. What can you say about the size of an array?
Fixed arrays have a fixed size that’s compiled into the program while dynamic arrays aren’t fixed, can be changed, and are set at runtime

  1. How can you initialize an array?
    element by element or by using an initializer list

  2. How can you get the size of an array in bytes?
    by using the sizeof operator which multiplies the array length by element size

  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    by dividing array size by element size

  4. What happens when you try to write outside the bounds of the array?
    undefined behavior