Arrays in C++ - Reading Assignment


#101
  1. An array is a list of a specific variable type.

  2. Index 1

  3. The size of an array is fixed but if it’s a dynamic array the size can be adjustable.

  4. You can initialize an array by using square brackets after a type variable.

  5. By using “sizeof()”.

  6. Sifeof()array/sizeof(element), and its recommended to use the first element (0).

  7. You will get undefined behavior.


#102
  1. What is an array?

    • a data structure that contains a set of same type variables
  2. Which index would you use to access the 2nd variable in an array?

    • 1 (starts with 0)
  3. What can you say about the size of an array?

    • the size is determined by the length of the array (the number of variables stored)
  4. How can you initialize an array?

    • int count[5] = { 1, 2, 3, 4, 5 }
  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(yourArray) / sizeof(datatype used for this array)
  7. What happens when you try to write outside the bounds of the array?

    • dangerous undefined behaviour

#103
  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?’
    Use index 1 to access the second variable in an array.

  3. What can you say about the size of an array?
    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

  4. How can you initialize an array?
    You can initialize an array element by element or use an initializer list.

  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) divided by sizeof(array[0]) = number of elements

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


#104

Part 1:

  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?
    The size of an array is the array length multiplied by the element size.

Part 2:

  1. How can you initialize an array?
    One way to initialize an array is to do it element by element
    int prime[5]; // hold the first 5 prime numbers
    prime[0] = 2;
    prime[1] = 3;
    prime[2] = 5;
    prime[3] = 7;
    prime[4] = 11;
    OR
    int prime[5] = { 2, 3, 5, 7, 11 }; // use initializer list to initialize the fixed array
    An array can be initialized by
  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)/sizeof(<type of element in your array>);
  4. What happens when you try yo write outside the bounds of the array?
    You get undefined behavior. You can overwrite a value in memory, the program can crash.

#105

1. What is an array?
An array is a structure 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?
An arrays count always starts from 0, so the 2nd variable index is [1].
3. What can you say about the size of an array?
a dynamic array doesn’t have a specified length so it’s size will vary.

A fixed array is an array where the length is known at compile time.
(int testScore[30]; // allocate 30 integer variables in a fixed array)

  1. How can you initialize an array?
    One way to initialize an array is to do it element by element, but can be difficult when dealing with large arrays. A more convenient way to initialize entire arrays via use of an initializer list.
    int prime[5] = { 2, 3, 5, 7, 11 }; // use initializer list to initialize the fixed array
  2. How can you get the size of an array in bytes?
    The sizeof operator can be used on arrays, and it will return the total size of the array (array length multiplied by element size)
  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.

#106
  1. Array is aatype of variable in which we can store multiple values with a single name.
  2. index 1
  3. No of elements in an array.

Part 2

  1. int arr[3] = {1, 2, 3};
  2. By using sizeof function.
  3. int arr[17];
    int size = sizeof(arr) / sizeof(arr[0]);
  4. C++ do not check if the index is correct it write the value at a memory location where the index we storing value into would have existed and it may cause unexpected behaviour.

#107

Hello @ivan and dear Community,


  1. What is an array?

An array is a “aggregated” variable that can store multiple values within itself.

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

int index = 1; array[index];

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

It’s fixed and need compile-time constants when created.

  1. How can you initialize an array?

int iArray[5]; int iArray[] {1, 2, 3, 4};

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

int iArray[5]; std::cout << sizeof(iArray) << endl;

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

int iArray[]{1, 2, 3}; std::cout << sizeof(iArray) / sizeof(iArray[0]) << endl;

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

Unpredictable behaviour will result.


Kind regards
Kevin


#108
  1. What is an array?
    A index list of object for a given 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?
    It the array length * by element size.

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

  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(int)

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


#109

Pt 1

  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? An array can be sized as one pleases based on a program’s limit. And the size of an array can be declared in different ways.

Pt2

  1. How can you initialize an array? You can initialize the size and fill each element (per index) by declaring them one by one. But a better way is to use an initializer list holding the set of elements inside a brace.
  2. How can you get the size of an array in bytes? Use the sizeof operator (after inititializing your array properly)
  3. How can you use the function sizeof in order to calculate the number of elements in an array? You can divide the size of the entire array by the size of an array element, i.e. referring to the 1st element (index 0). In both dividend and divisor, the operator sizeof is used. But in C++17 or later, one can use std::size() once the <iterator> header is included.
  4. What happens when you try to write outside the bounds of the array? It may seemingly accept it without harmful effects, but it leads to undefined behavior. This is because C++ does not do any checking to ensure indices are valid for the length of an array.

#110
  1. What is an array?
    Ans: An array is a sequence of objects of the same data type.
  2. Which index would you use to access the 2nd variable in an array?
    Ans: Index-1 as arrays always start at 0 in C++
  3. What can you say about the size of an array?
    Ans: Common arrays have fixed size except “dynamic arrays”

Part-2

  1. How can you initialize an array?
    Ans: Arrays can be initialized by specifying type of array, name of array followed by square brackets containing the size of an array e.g. int testScore[30]; will initialize an array called “testScore” having size of 30 elements.
  2. How can you get the size of an array in bytes?
    Ans: Arrays are simply pointers to an arbitrary amount of memory. If you do sizeof(array) it will return the size of a pointer - 4 bytes on 32 bit systems, and 8 bytes on 64 bit systems (if the program is compiled as 64 bit).
  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    Ans: First, note that the size of the entire array is equal to the array’s length multiplied by the size of an element. Put more compactly: array size = array length * element size. Therefore
    sizeof(array) / sizeof(array[0]) can give us size of entire array.
  4. What happens when you try to write outside the bounds of the array?
    Ans: It may cause undesired behavior and may cause the program to crash.

#111
  1. What is an array?
  • A variable that can store a list of variables.
  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 of an array is the array’s length multiplied by size of its elements
  1. How can you initialize an array?
  • int nameOfArray[size];
  • int nameOfArray2[size] = {0, 0, 0…};
  1. How can you get the size of an array in bytes?
  • sizeof(arrayName);
  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?
  • You basically overwrite other parts of memory outside the array, which can result in unpredictable behaviour that is difficult to debug

#112
  1. What is an array?
    An array is a 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?
    For static(fixed) arrays the size of the array is predefined at initialization

  4. How can you initialize an array?
    By using the initializer list or assigning element by element.

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

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

  7. What happens when you try to write outside the bounds of the array?
    It will cause undefined and potentially risky behavior. It’s the developers’ responsibility to ensure that the indices are valid for the range of the array.