Arrays in C++ - Reading Assignment

1)What is an array?
An aggregate data type that allows many varialbes of the same data type to be accessed.

2)Which index would you use to access the 2nd variable in an array?
1

3)What can you say about the size of an array?
It is determined upon initialization. It is array length multiplied by element size. The sizeof operator will return this value.

4)How can you initialize an array?
type arrayName[array length] {initializer list of array values}

5)How can you get the size of an array in bytes?
use the sizeof, size, or ssize operators

6)How can you use the function sizeof in order to
calculate the number of elements in an array?
sizeof(array) / sizeof(array[0])

7)What happens when you try to write outside the bounds of the array?

It causes a buffer overflow whereby random memory is overwritten. This can be exploited.

1 Like

*1. Allows us to allocate/assign multiple variables of the same type
*2. array[1]
*3. Is defined beforehand


  1. With initializer list
  2. sizeof(array)
  3. sizeof(array) / sizeof(array[0])
  4. undefined behaviour / programme may crash
1 Like
  1. An array is an aggregate data type that lets us access many variable of the same types through a single identifier.

  2. index[1]

  3. There are mainly two different types a static array length that never changes and is set as soon as the array is instantiated a name for these are Fix arrays or fixed length arrays. Dynamic arrays does not have an number of indices specified.

  4. The use of initializing lists after an identifier.

  5. the use of the sizeof pointer.

  6. By dividing sizeof(array) by the sizeof(arr[0])

  7. It defaults to the lastof index and uses that variable and can cause unpredictable problems.

1 Like

1. What is an array? An array 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 access the 2nd variable in an array? 1

3. What can you say about the size of an array? When declaring a fixed array, the length of the array must be a compile-time constant. This is because the length of a fixed array must be known at compile time. The length of a dynamic array can be set at runtime, and their length can be changed.

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

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? We can determine the length of a fixed array by dividing the size of the entire array by the size of an array element.

4. What happens when you try to write outside the bounds of the array? You will get undefined behavior. For example, this could overwrite the value of another variable, or cause your program to crash.

1 Like
  1. What is an array

An array is a a data type that allows us to access data of the same type from a single identifier.

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

In C++ the index is 1

  1. What can you say about the size of an array

The size of an array is the number of elements multiplied by the size of a single element since all elements are of the same type.

  1. How can you initialize an array?

An array is initialized by declaring the type i.e. double, int, etc… and the maximum number of elements in the array. A square bracket is used to tell the compiler that the data type is an array.
eg int myArray[4]
is an array that stores 4 integer values.

  1. How can you get the size of an array in bytes?

sizeof(myArray)

3 How can you use the function sizeof in order to calculate the number of elements in an array?

sizeof(myArray)/sizeof(myArray[1])
since all elements in the myArray are of same size we can choose any element in the array.

  1. What happens when you try to write outside the bounds of the array?

You get an undefined behavior which may lead to instabilities in the code.

1 Like

What is an array?
An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
Which index would you use to access the 2nd variable in an array?
[1], because the first element has index [0].
What can you say about the size of an array?
The size of an array is the sum of the elements multiplied by the total bytes the type is.

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

How can you initialize an array?
There are different ways of doing it. For example: int prime[3] ={2, 5, 3};
How can you get the size of an array in bytes?
sizeof(array);
How can you use the function sizeof in order to calculate the number of elements in an array?
sizeof(array) / sizeof(array[0]);
What happens when you try to write outside the bounds of the array?
it is assigned to the next byte in memory which runs as an undefined behavior that can cause errors or the entire program crashing.

1 Like
  1. An array is an indexed list of elements.

  2. The 2nd variable in an array is indexed as 1, the 1st variable always begins with 0.

  3. Array size is defined once initialized and sets the number of elements in the array.

  4. Initialize an array by utilizing [] brackets to set the length and {} for the elements, empty sets zeros.

  5. sizeof(array) provides the array size in bytes.

  6. using sizeof function to calculate number of elements in an array = sizeof(array) / sizeof(array[0])

  7. Undefined behavior.

1 Like

Part 1

1. What is an array?
  • A sequential sequence of variables of the same type. These variables are accessed via an numbered index starting at zero.
  • Declared as <type> <name>[<size>];
2. Which index would you use to access the 2nd variable in an array?

The 2nd element is at index 1.

3. What can you say about the size of an array?
  • The length must be known at compile time, so it cannot be determined by user input or calculated at runtime.
  • There are dynamic arrays which can have variable lengths but it is not covered in the article (using pointers?)

Part 2

1. How can you initialize an array?
  • By manually assigning values using the assignment operator (inefficient)
  • Using a curly bracket initializer list - i.e int numbers[3] = {1, 2, 3}.
    • When using this notation the size can be omitted and will equal the length of the initializer list
    • If an explicit size is defined and length of the initializer list is shorter than the size, all remaining elements are set to zero
    • If the initializer list is empty all elements are set to zero (or the equivalent for the type)
2. How can you get the size of an array in bytes?
  • By using the sizeof function on the array variable
  • size and ssize are better for C++ versions 17 and 20 onwards
3. How can you use the function sizeof in order to calculate the number of elements in an array?
int elementSize = sizeof(myArray) / sizeof(myArray[0]);
4. What happens when you try to write outside the bounds of the array?

(Starcraft Terran voice) “Nuclear launch detected!”

The runtime does not check to make sure the code is writing to a valid index, it just does it. This means it will be overwriting some random data in memory, resulting in unpredictable and random “undefined behaviour”.

I can attest to how frustrating and random this can be. The “segmentation fault” error was the bane of my existence when learning C++ during my undergrad back in the day! For the sake of performance the runtime does very little checking, it just trusts the code knows what it’s doing! This problem becomes even more ugly when you start getting into pointers.

I haven’t used C++ in ages though, hence why I’m doing this course.

1 Like

part 1

  1. a single data type that store several indexed objects
  2. 1
  3. a fixed array must have length set prior to runtime and cannot be changed, dynamic arrays can be set at runtime and also can be changed
    part 2
  4. Using square brackets you would set the array length equal to and in curly bracket you would list you array that is equal to or less than the array length
  5. sizeof(array);
  6. sizeof(array) divided by sizeof(array[0]) = number of elements
  7. by writing out of bounds of the array can lead to unintended results
1 Like

*1. **What is an array?
An aggregate data type that allows for many variables of the same data type.

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

3. What can you say about the size of an array?
Array lengths can be fixed (pre-defined) or variable (number of values not pre-set).

  1. How can you initialize an array?
    Each element can be declared, by structs, and setting a variable for the desired max length.

  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?
    Create a function with an array as a parameter, within the function cout the function call with the inputted array.

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

1 Like

Array Part I

  1. An array is a data type that enables us to group elements in an effective way. Strings or
    numbers for example. A collection of items

  2. To access the second variable you put in the value “1” into the named array:
    favoriteThings[1]

  3. The size of an array is either fixed or dynamic

Array Part II

  1. By creating an initializer list. For example:
    myArray[5] = [2, 4, 6,]; // only initialize first three elements

if you want to initialize to 0.0 you can use the variable double:
double array[3] = { ];

  1. Every character is 2 bytes and every integer is 4 bytes. So therefore
    multiplying will get you the correct result.
    int main() {
    int array[] = {2, 3, 4, 7, 7};
    cout << sizeof(array) << ‘\n’;
    return 0;
    } // This will print 20

  2. sizeof(array) / sizeof(array[0];

  3. The program can crash or it will say “undefined behavior”. It can also overwrite another variable in the program

1 Like

Article 1

  1. “An array is an aggregate data type that lets us access many variables of the same type through a single identifier.”

  2. The second variable in an array would use the index 1, since the first variable in an array is always indexed as 0.

  3. The size of an array can be determined by its length. Fixed size arrays have a defined length, meaning that if we array[30], we have 30 different integers (elements) in that array.

Article 2

  1. To initialize an array you can go element by element, but it is tedious and not very efficient. Most programmers use an initializer list. The list gives off the value corresponding with each element in the array. Then those elements can be called through the cout command by using the assigned index. It will then return the value assigned to that index.

  2. To get the size of any array in bytes you can simply type sizeOf(). The name of the array them gets placed inside of the parameters.

  3. You can divide the size of the array by the number of integers (or elements) inside fo it. You do this like so: sizeOf(array) / sizeOf(int)

  4. When you write outside the bounds of an array C++ will not know how to handle the commands you are giving it, and it will therefore be classified as undefined behavior.

1 Like

Hello sir, you probably refer to a Dynamic Array, that can be defined but does not have a fixed length.

Great answers, keep it like that please.

If you have any doubt, please let us know so we can help you! :slight_smile:

Carlos Z.

  1. What is an array?
    A data type that can hold a number of other variables of the same type.

  2. Which index would you use to access the 2nd variable in an array?
    Index 1. As arrays start at index 0.

  3. What can you say about the size of an array?
    The size of an array tells us how many positions are available (allocated) to store values.


  1. How can you initialize an array?
    We can initialize an array by enclosing the initial values in braces {}. Example:
int numbers[2] = {1, 2};
  1. How can you get the size of an array in bytes?
    Multiplying the number of elements by it’s size in bytes.

  2. How can you use the function sizeof in order to calculate the number of elements in an array?
    By dividing the total size of the array by the size of the elements it holds.

int intArray[20];
cout << sizeof(intArray)/sizeof(int) << endl;
  1. What happens when you try to write outside the bounds of the array?
    That will result in an undefined behavior. Most probably your program will crash, due to the fact that you are accessing an unknown memory position.
1 Like

Part 1

  1. What is an array?
    A data structure that allows for values to be stored and accessed with an index

  2. Which index would you use to access the 2nd variable in an array?
    index(1) since arrays are zero based

  3. What can you say about the size of an array?
    an array can be declared at a size or freely grow by appending records to the end.

Part 2

  1. How can you initialize an array?
    with the type, name and size in brackets. = ex. int myNums[10] = {1,4,2,6,7,2,3,5,8,9,0};

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

  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    int len = sizeof(arr)/sizeof(arr[0]); // so dividing by the array[0] from the array size

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

1 Like

Part 1

1 - What is an array?
An array is a collection of data, all of the same type (int, double etc.) which are stored under one variable name and referenced through index locations.

2 - Which index would you use to access the 2nd variable in an array?
Index 1 because C++ (like Javascript) counts from 0.

3 - What can you say about the size of an array?
Array sizes are fixed in C++ and need to be set when they are initialised.

Part 2

1 - How can you initialise an array?

int prime[5] = { 2, 3, 5, 7, 11 }; // NO = sign used after C++11

int array[5] = { }; // initialises all elements to 0

int array[] = { 0, 1, 2, 3, 4 }; // the compiler will set the length of the array automatically.

2 - How can you get the size of an array in bytes?
The sizeof function: sizeof(arrayName)

3 - How can you use the function sizeof in order to calculate the number of elements in an array?

sizeof(array)/sizeof(array[0]

4 - What happens when you try to write outside the bounds of the array.
You will get undefined behaviour.

Quiz 1:

double temperatureArray[365] = { };

Quiz 2:

#include <iostream>
using namespace std;

int main() {
    enum Animals {chicken, dog, cat, elephant, duck, snake, MAX_ANIMALS};
    int legs[Animals::MAX_ANIMALS] = {2,4,4,4,2,0};
    cout << "Ducks have " << legs[Animals::duck] << " legs";
    return 0;
}
1 Like

Excellent answer sir! really well documented! keep it like that please! :muscle:

Carlos Z.

1 Like

Part I

  1. What is an array?
    An array is a data type of objects of the same type data accessible through indexing.

  2. Which index would you use to access the 2nd variable in an array?
    I would offset by 1 to access the second object like this: myArray[1].

  3. What can you say about the size of an array?
    The length of a fixed array must be a compile-time constant because memory is allocated at compile-time and cannot be changed at run-time.
    The length of a dynamic array can be set at run-time, and their length can be changed.

Part II

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

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

  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    The size of an array divided by the size of one of the elements equals the number of elements in that array.
    sizeof(myArray) / sizeof(myArray[0])

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

1 Like

1. What is an array?
An array 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 access the 2nd variable in an array?
array[1]
3. What can you say about the size of an array?
Array sizes are fixed and need to be set when they are initialised

  1. How can you initialize an array?
    int prime[5] = { 2, 3, 5, 7, 11 }; // “=” not used since C++11
    int array[5] = { }; // initialises all elements to 0
    int array[] = { 0, 1, 2, 3, 4 }; // the compiler will set the length of the array automatically.
  2. How can you get the size of an array in bytes?
    The sizeof function: sizeof(arrayName)
  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    sizeof(array)/sizeof(array[0]
  4. What happens when you try to write outside the bounds of the array?
    That will result in an undefined behaviour. It could overwrite the value of another variable, or cause your program to crash.
1 Like

Part 1

  1. An array is a list of values in a single object.
  2. array[1]
  3. The size of an array is either fixed or dynamic. Fixed arrays have a size which is determined at compile-time and their lengths cannot be changed. Dynamic arrays can determine their size during run-time and their lengths can be changed.
    Part 2
  4. An array can be initialized (1) element by element, line by line, (2) all at once using a list, or (3) left blank using zero initialization. It can also be initialized without the “=” sign, when using uniform initialization.
  5. sizeof(array)
  6. sizeof(array)/sizeof(array[0])
  7. undefined behavior
1 Like