Arrays in C++ - Reading Assignment

  1. What is an array? an array is an aggregate data type that allows us to access many variables of a single data type.

  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? - you can fix the length of an array at compile time, or the length of a dynamic array can be set at runtime.

  4. How can you initialize an array? by creating an initialiser list in the {}

  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]) - by dividing the size of an array by the size of an array element

  7. What happens when you try to write outside the bounds of the array? could overwrite another value, crash the program - undefined behaviour.

1 Like
  1. An array is an aggregate data type that allows us access many variables of the same type via a single identifier.
  2. arrayName[1];
  3. Depends if the array is fixed or dynamic. A fixed array has to be a compile-time constant, this requires the length of the fixed array to be known at compile time, the length of the fixed array cannot be changed. Dynamic arrays can be set at runtime, and the length of the dynamic arrays can be changed.
  4. You could initialize an array element by element, or you could use an initializer list.
  5. size(array)
  6. sizeof(array)/sizeof(array[0])
  7. You will get an undefined error, leading to the program crashing or the value of another variable to be overwritten.
1 Like

1. What is an array?
A data structure for storing a sequence of values of the same type.

2. Which index would you use to access the 2nd variable in an array?
Since the 1st variable is present at index 0, so 2nd will be present at index 1.

3. What can you say about the size of an array?
For a static array, the length must be known at the compile time.
For a dynamic array, the length can be set at run time.

1. How can you initialize an array?
By using the initializer list, an array can be initialized.
For instance:
int arr[5]{}; // all the elements are initialized to 0
int arr[5] {2,3,4,5,8}; // elements are mapped to the corresponding values in the initializer list.
int arr[] {2,3,4,5,8}; //we don’t need to specify the size.
int arr[5] {2}; // First element is set to 2 while remaining are set to 0.

2. How can you get the size of an array in bytes?
int arr[5]{};
int sizeOfArray = sizeof(arr); //20 B

3. How can you use the function sizeof to calculate the number of elements in an array?
int arr[5]{};
int numberOfElements = sizeof(arr) / sizeof(int);

4. What happens when you try to write outside the bounds of the array?
It can result in undefined behavior. However, the compiler allows you to write outside the allocated memory with a warning.

1 Like

1 An array is an aggregate data type that let us access many variables of the same type through a single identifier.
2 array[1].
3 the size of an fixed array must be known.

2nd part
1 int nameofthearray [] {}
2 sizeof(array)
3 array length * element size
4 undefined behavior

1 Like

Article 1:

1.- An aggregate data type that lets us access many variables of the same type through a single identifier.

2.- 1, since number 0 is the first one.

3.- Its is length multiplied by the element size.

Article 2:

1.- You can do it element by element.

2.- sizeof(array)

3.- sizeof(array) / sizeof(array[N])

4.- An unidentified behaviour.

1 Like

You don’t get a warning. In fact here is an example from our student that made this mistake in his program that made it overwrite the next variable he declared after the array. The program compiled without any errors :slight_smile:

Thanks, @Alko89. :innocent:

  1. What is an array?

Basically a list that helps you store more data effectively.

  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 determined by multiplying the array length and element size.


  1. How can you initialize an array?

f.x. int arrayName[2] = {6, 97};

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

By using sizeof()

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

sizeof(arrayName) / sizeof(arrayName[0])

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

You will get undefined behaviour.

1 Like

Part 1

  1. Arrays are indexed lists of items of the same type.

  2. 1, as the indices start from 0.

  3. The size of an array is equal to its length multiplied by element size. Arrays can have a fixed length or a dynamic one.

Part 2

  1. type identifier[arrayLength] = {initializers_list};
  • if the array length is omitted, the compiler will initialize the length based on the number of listed initializers;
  • if the initializer list has fewer elements than the defined length, the listed initializers will be used (in order) for the first elements the array, while the remaining ones will contain zeroes (this is called zero initialization);
  • consequently, if the list is empty the whole array will have a zero initialization.
  1. By using the sizeof() operator.

  2. sizeof(array)/sizeof(array[0])
    (since, as stated earlier, the array size is equal to element size times number of elements)

  3. Undefined behavior.

1 Like

An array is the way to store multiple values in a single variable/object

  1. An array allows us to store multiple values in a variable / object.

  2. 1

  3. A fixed array is an array where the length is known at compile time

1… E.g., Int array [] {1, 2, 3, 5, 10)

  1. sizeof(arr)

  2. The sizeof operator can be used on arrays, and it will return the total size of the array.

std::cout << std::size(array) << ‘\n’; - will print the size of the array (I’m not too sure on this example, please let me know)

  1. You get undefined behaviour - for example, this could overwrite the value of another, or cause your program to crash
1 Like
  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. For fixed arrays the size has to be known and can not be changed later on because they have memory allocated at compile time. However, there are also dynamic arrays which can be set at compile time, and their length can be changed.

next questions:

  1. in order to initialize an array we use square brackets ([])
    2.array length = array size / element size. sizeof(array) is equal to the program - array length = sizeof(array) / sizeof(array[0])
    3.you will get undefined behavior – For example, this could overwrite the value of another variable, or cause your program to crash.
1 Like

It will return the size of array in bytes, to get the length of the array you have to divide the size with an array element or type. :slight_smile:

sizeof(array) / sizeof(array[0]) or sizeof(array) / sizeof(int)

Part 1

  1. Arrays allows the user to store multiple values in single variable.
  2. 1
  3. The size of an array is the length and you can have either fixed array or dynamic array.

Part 2

  1. You can initialize it element by element but it is so much torture, luckily c++ provides initializer list to initialize every element.
  2. sizeOf(array);
  3. sizeOf(array)/sizeof(“type of element in your array”);
  4. It will cause an undefined behavior/
1 Like

[1. An array is a data type that allows access to many variables of the same type through a single identifier.
2. Subscripting is the index to access the 2nd variable in an array.
3. An array has a fixed size in memory, but can have any amount of defined variables.

  1. You can initialize an array element by element, or through and initializer list.
  2. You can get the size in bytes by using the sizeof operator.
  3. You calculate the number of elements in an array by dividing the size of the array by the size of the array elements ----- sizeof(array) / sizeof(array[0]
  4. When you try to write outside the bounds of an array a value is inserted into the memory where the next element would have existed in an array, causing an undefined behavior causing your program to either crash or possibly overwrite other variables.
1 Like

What is an array?
An array is a variable in where we can store values in different indexes.

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

What can you say about the size of an array?
The size of one array is equal to it’s number of elements.

How can you initialize an array?
There are 2 ways of doing it:

  • By defining the length - a[5]{}
  • By defining the elements - a[]{1,2,3,4,5}

How can you get the size of an array in bytes?
Yes, by multiplying the number of elements / length of the array, with the size of this elements in bytes.
For example int array[ ]{1,2,3,4,5} it has 5 elements and each int has 4 bytes = 20 bytes

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

What happens when you try to write outside the bounds of the array?
Since C++ doesn’t recognise this as en error, we could start having errors and program crashes, but more important it could mess with our existing data, by overwriting values.

1 Like

sizeof will give you the size of the array in bytes. So you should divide the number by an array element type. :slight_smile:

1 Like
  1. What is an array? 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? we use the array name, along with the subscript operator 1

  3. What can you say about the size of an array? They are fixed or dynamic

  4. How can you initialize an array? an initializer list

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

  6. How can you use the function sizeof in order to calculate the number of elements in an array? use sizeof to get the total size of the array, and then divide by element size to get the number of elements in the array

  7. What happens when you try to write outside the bounds of the array? a memory location of where the element would have been had it existed. When this happens, this could overwrite the value of another variable, which leads to unexpected behaviours and results.

1 Like
// FIRST ANSWERS

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

2- array[1]

3- Determined by the multiplication of array length, and the element size.

// SECOND ANSWERS

1- Via use of an initializer list. Just like that;

int arrayName [arrayLength] = { 2, 3, 5, 7, 11 }; // Where the values between {} are the initializer list.

2- The sizeof operator can be used to return the total size of the array.

sizeof(array);

3-

sizeof(array)/sizeof(<type of element in your array>);

4- Undefined behavior. This could overwrite the value of another variable, or cause your program to crash.

1 Like

1 - An array is an ordered set of elements of the same data type
2 - To access the 2nd variable in an array - array_name[1]
3 - Size of the array is array length * element size, you can get this value using sizeof(array_name)

1 - Initialize an array as - array_name [size] {}
2 - To get the size of an array in bytes - use sizeOf(array_name)
3 - To find the number of elements in an array using sizeOf, sizeOf(array_name) / sizeOf(int) (for integers) or you can use sizeOf(array_name[0]) in the denominator for other types
4 - You will get unpredictable behavior (or a compiler error) if you try to write outside the bounds of the array

1 Like

Part 1

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

  2. You can use the index 0, the second index is 1.

  3. The size of an array is also known as length. Arrays can be fixed and dynamic.

Part 2

  1. You can initialize an array to do it element by element or initializer list

  2. Sizeof(array)

  3. Sizeof(array[0]) The only element guaranteed to exist no matter what the array length is.

  4. undefined behaviour occurs, therefore it overwrites the value of another variable, or cause your program to crash.

1 Like