Arrays in C++ - Reading Assignment

1 What is an array?* is an aggregate data type that lets us have 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? It starts at 0 and ends at N. The array length is the count of the number of variables in the array = N-1.

Part 2

  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? sizeoff(array)

  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? UB. Buffer overflow.

1 Like
  1. What is an array?a collection of variables of similar type under one identifier
  2. Which index would you use to access the 2nd variable in an array? index 1
  3. What can you say about the size of an array? it is the array length multiplied by the element size

Part 2:

  1. How can you initialize an array? element by element or an initializer list
  2. How can you get the size of an array in bytes? with the sizeof operator
  3. How can you use the function sizeof in order to calculate the number of elements in an array?by dividing the size of the the entire array by the size of an array element.
  4. What happens when you try to write outside the bounds of the array? an undefined behavior.
1 Like

1.1. A list holding objects, every object has an index starting from 0;
1.2. [1]
1.3. Size of the array is decided when declaring the variable. For example: int array[5];
2.1. int array[5]; initializes an array that is type integer and has 5 slots.
2.2. sizeOf(array)
2.3. sizeOf(array)/sizeOf() type = int/string/double…
2.4. You will get undefined behavior once again ^^

1 Like
  1. is a data set of values that allows you to access many variables of the same type through a single identifier.

  2. You would is the index of 1 because all array sets start with 0.

  3. The size of an array determines how many elements it can contain within its array length.

  4. You initialize an array by assigning a definition to an identifier and using square brackets then using curly brackets to write values separated by commas how ever many you write set the array length. If the curly brackets don’t hold any values the array length is set to 0.

  5. sizeOf(array)

  6. You take array size divided by the element size and you get the array length.

  7. When you write in the outside of an array it gets stored but can lead to potential bugs in your software later on. This can be the reason of undefined behavior.

1 Like
  1. An array is an aggregate data type that lets us access many variables through a single identifier.

  2. [1]

  3. The size of an array dictates how many elements will be contained in the array length.

  4. You can define each index line by line or contain the array elements in braces {} on the same line as the declaration of array name.

  5. sizeof()

  6. array length = array size / element size
    array length = sizeof(array) / sizeof(array[0])

  7. Undefined Behaviour

1 Like

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

2. Which index would you use to access the 2nd variable in an array?
To access the 2nd variable in an array , use index [1]
3. What can you say about the size of an array?
“The sizeof operator can be used on arrays, and it will return the total size of the array (array length multiplied by element size).” – from the second article (https://www.learncpp.com/cpp-tutorial/arrays-part-ii/)

1. How can you initialize an array?
You can initialize an array by using an initializer list, filling in the curly brackets with elements separated by commas, if not enough elements are given the remaining elements initialize to 0.

2. How can you get the size of an array in bytes?
sizeof(array);” – Ttoine I had a hard time finding where it specified this was how to find size in bytes, thankyou for the clarification.

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

4. What happens when you try to write outside the bounds of the array?
Trying to write out of range/bounds in an array will cause the data you are trying to pass to be saved where it would have gone had it existed. This will cause UB or undefined behaviour.

1 Like

What is an array?
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?
the subscript operator, [1] for the second variable

What can you say about the size of an array?
There are tow types of arrays, one is fixed and the other is dynamic. The difference that dynamic arrays can define the length of the array after compiling, and fixed arrays can not.

How can you initialize an array?
using an initializer list

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?
The length of a fixed array can be calculated by dividing the size of the entire array by the size of the array element. array size = sizeof(array)/sizeof(array[0])

What happens when you try to write outside the bounds of the array?
This causes undefined behavior and possible program crash

1 Like

I

  1. An array is a string of variables aggregated on a single identifier.
  2. Index [1]
  3. The size of an array is predefined and can’t be changed after is declared.

II

  1. Put curly brackets (with or without values) after the array’s declaration.
  2. std::size(array’s name) will output the size of the array.
  3. sizeof(array) / sizeof(array[0]) (i’l just use std::size()…)
  4. The program may crash or have an undefined behavior.
1 Like

It would output the length of the array or number of elements, not the size in bytes. :slight_smile:

  1. What is an array?
    An array is a collection of the same data type that allows access to many variables 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 size is determined by how many variables are in it and if it’s a fixed or dynamic array.

  4. How can you initialize an array?
    Several ways…

  • int array[3] = { 5 }; // only initialize the first element
  • int array[3] = { 0, 1, 2 }; // explicitly define length of the array
  • int array[] = { 0, 1, 2 }; // let initializer list set length of the array
  1. How can you get the size of an array in bytes?
    sizeof(array);

  2. How can you use the function sizeof in order to calculate the number of elements in an array?
    example - sizeof(array)/sizeof(array[0]);

  3. What happens when you try to write outside the bounds of the array?
    It is assigned to the next byte in memory but can result in undefined behavior that can cause errors or result in program crash. Important to ensure array indices are valid for the size of array.

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. fixed arrays = have a fixed length that cannot be changed
    dynamic arrays = length can be changed but coding is more complicated

  4. one way is by initializing each element in the array, individually with curly brackets.
    or
    initializing how many elements in the array with the use of square brackets with the number of element contained within e.g. followed by the curly brackets [x]{}

  5. multiply the number of elements in the array by the integer byte size (2 bytes on 32 bit, 4 bytes on 64 bit)

  6. std::sizeof(array)

  7. you can get undefined behavior resulting in crashes or bugs

1 Like

Sizeof gives you the site of the array in bytes. To get the number of elements you have to divide by the element type or just use the first element in the array. :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?*
    index [1] since the first variable has index [0]

  3. What can you say about the size of an array?*
    In a fixed array, the size/length is known at compile time. In dynamic arrays the length can be set at runtime.

  4. How can you initialize an array?
    With empty curly brackets, all values in an array are set to 0 or empty. Or you can put comma separated values between the curly brackets.

  5. How can you get the size of an array in bytes?
    sizeof(array) gives array length multiplied by element size

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

  7. What happens when you try to write outside the bounds of the array?
    This will cause undefined behavior

1 Like

PART 1

  1. An array is an aggregate data type that can store many variables of the same type through a
    unique identifier.

  2. Index 1.

  3. The size of an array is the number of variables that are stored inside of it.

PART 2

  1. typeArray nameArray[sizeArray]{};

  2. sizeof(nameArray);

  3. sizeof(nameArray)/sizeof(nameArray[0]);

  4. The result is undefinied behaviour(e.g. other variables overwriting, computer crashes, …).

1 Like
  1. An array is an collection of variables together under 1 identifier

  2. [1] because we start counting from 0

  3. That there are two types of arrays a fixed array (what we’ve just learned) and a dynamic array which is a little more complicated

1 by using { 2, 4, 7 } for example

2 sizeof(array);

3 sizeof(array) / sizeof(array[0]

4 undefined behaviour

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

  3. If we use the “sizeof” operator on an array, we will get the number of elements of the array multiplied by the size of the type of the element.

  4. The most convenient way to initiate an array is to use initiater list.

  5. By using the sizeof() operator. It will multiply the number of elements by the size of the type of array.

double arr [3]{1.2 , 3.5};
std : : cout << sizeof(arr)/sizeof (double) <<"/n

This will print 3.

  1. The excess input will be stored somewhere in memory or the complier will give a error in newe4r versions, in older versions, the result is undefined behaviour
1 Like

1. What is an array?

A collection of common items

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?

Can not be changed. It is always N-1 to access an element.

  1. How can you initialize an array?

int array[5]{}
int array[]{0,1,2,3,4}

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

sizeof(array)

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

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

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

Compiler Error

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 one 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 must be specified at compile time by either definition of array length or initializer list.

Second Part

  1. How can you initialize an array?
    With an initializer list , which is written in curly braces after type, array name and index.

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

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

  • sizeof(array)/sizeof(array[0])
    You divide the total bytes that are reserved for an array by the bytes each element of the array occupies. Result is number of elements (or array length).
  1. What happens when you try to write outside the bounds of the array?
  • This will result in undefined behaviour and could potentially create vulnerability in your code.
1 Like

Part 1:

  1. An array is a aggregate data type that lets us access many variables of the same type through a single identifier.
  2. Test[1]
  3. The size of an array is the length of the array times the size of the elements
    Part 2:
  4. You can initialize an array by using an initializer list
  5. using the function sizeof(array)
  6. sizeof(array) / sizeof(array[0])
  7. It could give you an undefined behavior
1 Like
  1. An array is a data type that allows one to access many variables of the same type through the same identifier.

  2. Index 1.

  3. The array length is declared at the same time the array variable is declared, using square brackets.

  4. Include an initializer list inside the curly braces, or just leave the curly braces empty to initialize with zeros.

  5. One way is to use sizeof(array) to produce the length of array times the bytes per element.

  6. Use sizeof(array) and divide by sizeof(element-type), and you’ll get the number of elements.

  7. The compiler will generate an error.

1 Like