Arrays in C++ - Reading Assignment

  1. An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
  2. 1
  3. Fixed arrays have a fixed length that can not be changed but dynamic arrays can have a changeable length

1A Element by element and via the use of an initializer list
2A By using the std::size() function from the header
3A By dividing the size of the entire array by the size of an array element
4A Undefined or even crash

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?
    The size of an array is the array length multiplied by element size. You can specify the array length during the declaration between brackets, otherwise, the compiler can do it for you if you initialize the array.

  4. How can you initialize an array?
    int prime[5]{ 2, 3, 5, 7, 11 }; // use initializer list to initialize the fixed array

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

  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?
    When you write outside the bounds of the array, the value gets stored nevertheless, but this may and probably will result in undefined behaviour.

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? - The index 1

  3. What can you say about the size of an array? - The std::size() function from the iterator header can be used to determine the length of arrays.

  4. How can you initialize an array? - You can do that by using an initializing list.

  5. How can you get the size of an array in bytes? - sizeof(array) return the size of a pointer - 4 bytes on 32 bit systems, and 8 bytes on 64 bit systems

  6. How can you use the function sizeof in order to calculate the number of elements in an array? - sizeof(array) / sizeof(array[0]dividing the size of the entire array by the size of an array element

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

1 Like

PART 1:

  1. An array is an aggregate data type that lets us access many variables of the same type through a single identifier, using the [] brackets.

  2. To access the second variable, we use the index 1, as to call the 1st we use 0.

  3. The size of an array object is always equal to the second template parameter used to instantiate the array template class (N). Unlike ‘sizeof’, which returns the size in bytes, this function returns the size of an array in terms of number of elements.

PART 2:

  1. Entire arrays can be initialized with the use of an initializer list.

  2. To find the size of an array in bytes, we use the function ‘sizeof’.
    image

  3. To determine the number of elements in an array, we divide the total size of the array, by the size of the array element:
    int length = sizeof(arr)/sizeof(arr[0]); printf(" Number of elements present in given array : %d", length);

  4. The array index out of bounds error, is a special case of the buffer overflow error. It’s the area outside the array bounds which id being addressed, that’s why this situation is considered a case of undefined behavior.

1 Like

What is an array?
Array is aggregate collection of variables of the same type which lets the user access it using a single identifier

Which index would you use to access the 2nd variable in an array?
Since index starts at 0, the index “1” should be used for 2nd variable in the array

What can you say about the size of an array?
The size of an array is not a simple function to use since it does not return the number of elements but rather, element x values of the elements.

How can you initialize an array?
You initialize an array by name[size] {element, element,…}

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])
Does not work for array that doesn’t not have a definite size at initialization.

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

2 Likes
  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? from 0 to N-1

  4. How can you initialize an array? int array []= {};

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

  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?

2 Likes
  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?
    Stores a fixed-size sequential collection of elements of the same type.

  4. How can you initialize an array?
    Element by element or best way using a initalizer list.

  5. How can you get the size of an array in bytes?
    using the sizeof operator: sizeof (array);

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

  7. *What happens when you try to write outside the bounds of the array?
    error, undefined behaviour

1 Like

Arrays (Part 1):

  1. it is a data type that lets us access many variables of the same type through a single identifier.
  2. index 1.
  3. the size of the array can be fixed or dynamic.

Arrays (Part 2):

  1. you can either do it element by element or use an initializer list
  2. sizeof(array)
  3. devide the size of the entire array by the size of one array element
  4. undefined behaviour
1 Like

// using an enumerator
enum Weekday
{
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
sunday,

maxWeekday

};
int numberOfLessonsPerDay[maxWeekday]{}; // Ok

In the first part this code is used. How is the length of the array filled in? I can’t seem to get the logic.
Thanks in advance.

1 Like

Part 1

  1. A type of data that collects information in form of values that will be assigned to variable which are themselves assigned by the name of the array which is called a identifier.

  2. int array2[1]

  3. It is defined at the begining and can’t be changed for static arrays.

Part 2

  1. Defining each value or create a recommended list.

  2. Number of total integers multiplied by 4.

  3. Sizeof(array).

  4. It will be assigned to the next memory byte and it will cause errors and thus the program to crash.

2 Likes

Its because you’re using an enum to initialize the array. Enums by default are assigned consecutive numbers, starting from 0. So the maxWeekday will have a number 7 assigned to it. This fits in the number of days in a week therefore when you initialize an array with maxWeekday you will get a properly sized array where each element represents a day in a week.
You can then use the same enum to access the array elements using the Weekday enum :slight_smile:

2 Likes
  1. What is an array?

    It allows us to put data under a single identifier and retrieve that data.

  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?

    You usually have to define the size prior to creating the array, but you can also have a dynamic array
    that can be changed, depending on the data

  4. How can you initialize an array?

    int array[3] {1,2,3};

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

    sizeOf(array)

  6. How can you use the function sizeOf in order to calculate the number of elements in an array?

    array length = sizeOf(array)/sizeOf(array[0])

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

    Undefined behavior

2 Likes

PART 1

  1. What is an array?
    An aggregate data type that lets us access multiple values of the same type from 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?
    Size of an array is declared within brackets i.e. int arr[10]{};

PART 2

  1. How can you initialize an array?
    Put values in the initializer list. int arr[3]{1,2,3};
  2. How can you get the size of an array in bytes?
    sizeof()
  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    Divide the size of an array by the size of a single element.
  4. What happens when you try to write outside the bounds of the array?
    You get undefined behavior, a crash or the value assigns to another index
2 Likes
  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?
    The size is fixed and can’t be modified later on.

  4. How can you initialize an array?
    Example:
    int array[3] = {1, 2, 3};

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

  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?
    Undefined behavior.

2 Likes
  1. What is an array?

A data type where you can store elements of the same type.

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

[1]

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

The size is fixed, you can define it in the defintion of the array.

  1. How can you initialize an array?

int prime[5]{ 2, 3, 5, 7, 11 };

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

I’m really confused at this part

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

This one is confusing as well for me

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

An error is produced

2 Likes

You can use sizeof, which gives you the size in bytes.

Using the size of the array in bytes and dividing it by the size of an array element will give you the length of the array (sizeof(array) / sizeof(array[0])).

Technically it results in undefined behavior, which will end up in an error most of the time. :slight_smile:

1 Like

1.) An aggregate data type
2.) array[1];
3.) With a fixed array the length must be known at compile time.

1.) element by element or through an initializer list.
2.) sizeof();
3.) size of array / size of array element.
→ std::cout << sizeof (array) / sizeof (array[0];
4.) C++ does not check for validity. undefinied behaviour or a crash might occur.

2 Likes

Arrays Part 1

  1. An array is a single identifier that acts as an aggregate data type of many variables, all of the same type.
  2. variable[1] is used to access the 2nd variable in an array
  3. The array’s range is 0-(N-1); a fixed array is declared with a constant array size; a dynamic array has a range that can be changed as needed.

Arrays Part 2

  1. You can create an initializer list to initialize an array
  2. Multiply the sizeof(array) by the sizeof(variable_type) to get the size of an array in bytes
  3. sizeof(array)
  4. An unintended value could be inserted into the target element, overwrite the value of a different variable, or lead to a crash. Essentially, you’ll get undefined behavior.
2 Likes

Actually sizeof will give you the size in bytes. To get the length you divide by the element type. :slight_smile:

1 Like
  1. An array is a data type that allows us to access many variables with one identifier.

  2. [1]

  3. The size or the length of an array is determined by the number of elements in that array.

  1. We can initialize an array by setting the array size before adding values.
    For example:
    int arr [3] = {1,5,7};

  2. sizeof();

  3. We can use sizeof to find the length of the whole array , and then divide it by the length of each element.

  4. The program won’t function or it will have security problems.

2 Likes