Arrays in C++ - Reading Assignment

1. What is an array?
A variable used to access multiple variables of the same type.

2. Which index would you use to access the 2nd variable in an array?
The first index is 0, second is 1. You access the indexed variable with the subscript operator [ ]: array[1];

3. What can you say about the size of an array?
For the purposes of this question, size refers to length. There are two types of array possible in C++;

  • Fixed - length is set at compile time, cannot be affected by user input, cannot change: int gradeArray[20]{ }; //fixed array of 20 elements.
  • Dynamic - length can be set at runtime and can be changed

More generally total array size - as in the bytes value - requires the array length multiplied by the size of the elements (data type bytes size)

Part Two

1. How can you initialize an array?
One option is to do so element by element, which is time consuming.

  • int prime[5] {};
    prime[0]{2};
    prime[1] = 3;
    prime[2] = 5;
    prime[3] = 7;
    prime[4] = 11;

A more efficient method is to initialize via an initialiezer list.

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

2. How can you get the size of an array in bytes?
std::size(array) will only return length
sizeof(array) will give the byte value

3. How can you use the function sizeof in order to calculate the number of elements in an array?
std::size(array) will not work on a fixed array that has been passed to a function, so at times other methods are needed. As it is possible to use sizeof(array) to find the byte value, and we know the total byte value is the array length * the element data type’s byte value, we can simple divide the total byte value of the array, but the byte value of the element data type, to find the length.

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

4. What happens when you try to write outside the bounds of the array?
It will result in undefined behavior as C++ will not check. It can lead to the value of other variables being overwritten or the program crashing.

1 Like

1. What is an array? - It is a set of parameters we can arrange and access
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? - They are set or have a limite, if we want to differ we can use a dynamic array.

  1. How can you initialize an array? - You can initialize them one by one or if you have more variables then use an initializer list.
  2. How can you get the size of an array in bytes? - sizeOf(arr)
  3. How can you use the function sizeof in order to calculate the number of elements in an array? - sizeof(arr)/sizeof(int) for other types sub type for int or in the article sizeof(arr)/sizeof(arr[0]) but I like my way better, it is more clea
  4. What happens when you try to write outside the bounds of the array? It shoots “undefined behavior”
1 Like

1. What is an array?
An aggregate of multiple variables of the same 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?
array size = array length * element size

  1. How can you initialize an array?
    Use an initializer list:
    int array[] = {11, 22, 33}

  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(array[0])

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

1 Like
  1. An array is a variable with many values.
  2. You would use index 1. For Example: testScores[5]{98,12,45,67,23}. if you want the second position you would say testScores[1]
  3. The size of arrays need to fixed at compile time unless you are using dynamic arrays

part 2

  1. You can initialize an array by using [] brackets and {}. Additionally, you can use the enumerator function to give a name to each value.
  2. You can get the size of array by using the function sizeof().
  3. You take the sizeof(array)/sizeof(element in array).
  4. when you try to operate outside of the array you will get a 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 a single identifier.

  2. Which index would you use to access the 2nd variable in an array? Arrays always count starting from 0! [0]= first [1]= second one

  3. What can you say about the size of an array? array size = array length * element size.


  1. How can you initialize an array? Int prime [5]{2,3,5,7,11 };

  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(array[0]

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

1 Like
  1. An aggregate data type that allows aggregated data to be accessed through a single identifier.
  2. arrayName[1]
  3. In C++ array size is instantiated at compile time for fixed length arrays and cannot be changed. However an array’s size is instantiated at runtime for dynamic arrays and can be altered.
  1. int prime[5]{ 2, 3, 5, 7, 11 };
  2. sizeof(array)
  3. (sizeof(array))/(sizeof(array[0]))
  4. The result will be undefined behaviour. i.e. unintended overwrites or program crashes.
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. There are 2 types of arrays: fixed length array (which have the same length i.e same number of elements the whole time) and variable length array (which could have different length at different times).
2. Which index would you use to access the 2nd variable in an array?
myArray[1];
like in many other languages, in C++ you start counting from 0, so the an array which has N elements, goes from 0 to N-1
3. What can you say about the size of an array?
There are 2 types of arrays: fixed length array (which have the same length i.e same number of elements the whole time) and dynamic array (which could have different length at different times).

Because fixed arrays have memory allocated at compile time, that introduces two limitations:

  • Fixed arrays cannot have a length based on either user input or some other value calculated at runtime.
  • Fixed arrays have a fixed length that can not be changed.

In many cases, these limitations are problematic. Fortunately, C++ supports a second kind of array known as a dynamic array . The length of a dynamic array can be set at runtime, and their length can be changed.

  1. How can you initialize an array?
    There are different ways to initialize an array, but there is one preferred way to do it, to avoid any undefined behavior, which is: Explicitly initialize arrays, preferably with an initializer list, even if they would be initialized without an initializer list.

some correct initializing examples:

int numbers[5]{}; //initialize all elements to 0
double doubleNumbers[5]{1.3, 2.5, -10.1}; // initialize to [1.3, 2.5, -10.1, 0.0, 0.0]
std::string array[3]{}; // initialize all elements to empty strings

some “not so correct” initializing examples:

int numbers[3]; //uninitialized
double numbers[3]; // uninitialized

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

I give up … I was trying to find it using my googling and bravesearching skills but I failed… :sleepy: … so I will scroll up and find it from the answers from my fellow companions

from an array int array[3] {1, 2, 3}, i think is by using sizeof(array), but Iam not so clear if my machine has X byte integers or has X byte pointers … as this is not covered in this course anyway so far
3. How can you use the function sizeof in order to calculate the number of elements in an array?
in an int array, you could do:

sizeof(array)/sizeof(int)

but I think you could also use std::size(array) from C++20 onwards

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

you meet with trinity … nah just joking

C++ does not do any checking to make sure that your indices are valid for the length of your array. So in the above example, the value of 13 will be inserted into memory where the 6th element would have been had it existed. When this happens, you will get undefined behavior – For example, this could overwrite the value of another variable, or cause your program to crash.

1 Like

From the matrix? :grin:

1 Like

yep :joy: :joy: you got me

1. What is an array?
An array is a struct that holds many vars with the same type and identified by an index
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 has to be stated when you declare the array and the size has to be known at compile time.

  1. How can you initialize an array?
   int arr[10] {}; // all zeros
   int arr[3] = { 10,20,30 };  
  1. How can you get the size of an array in bytes?
size(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])

  2. What happens when you try to write outside the bounds of the array?
    You write on stack (memory space). “stack smashing detected”. Dangerous

1 Like
  1. An array is a way to store multiple variables in one identifier.
  2. randomArray[1]
  3. You can declare the amount of elements you would like before you define them, and you can’t change that amount later
1 Like

i:1) An array stores multiple variables of the same type under one identifier

  1. 1

  2. you can have dynamic and fixed arrays. Dynamic arrays allow for the length of the array to be changed, for example over time when new data comes in it could be put in the array as the next entry. A fixed array does not allow this and the size of the array is defined at the start

ii:1) You can do it element by element, however this isnt practical especially for larger arrays. You can also use an initializer list, which you would put in square brackets before the array for example int array[] = {1,2,3}

  1. using the sizeof function

  2. by dividing the size of the array by the size of each element in the array.

  3. you will get undefined behaviour and the out of bounds value could potentially overwrite and existing value in your memory

1 Like

PART I
1. What is an array?
A data type which we can store and have access to multiple 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
eg.

int carPark[5]{};

If we want to see what is inside the 2nd variable, we use

carPark[1].

3. What can you say about the size of an array?
The size of a fixed array (fixed length array or fixed size array) is Determined at the beginning of the array variable declaration.
The size / length is known at compile time.

Part II
1. How can you initialize an array?
The best way to do it, is with an initializer list.
e.g

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

we use initializer list to initialize an fixed array, we
could also “initialize” an array element by element…but this not efficient and in reality it isn’t initialization but assignment. This would pose a problem if we use an array with const. The assignment wouldn’t work.

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

std::size(array)

for arrays which haven’t been passed to functions.

3. How can you use the function size of 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?
In C++, the value will be inserted into the memory. C++ doesn’t care if the indices are valid for the array’s length.
The results of trying to write outside the arrays bound could be undefined behavior, like overwriting the value of another variable, or cause a program crash.

2 Likes
  1. A variable where you can store indexed objects
  2. array[1]
  3. 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.

  1. int array[1,2,3,4,5,6,7,8,9]{};
  2. sizeof(array)
  3. You get undefined behavior. You can overwrite a value in memory, the program can crash…
1 Like

Reading #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?
    The length of an array must be know at compile time.

Reading #2

  1. How can you initialize an array?
    One way to initialize an array is to do it element by element, a more convenient way to initialize entire arrays via use of an initializer list, there is also zero initialization. the best practice is to explicitly initialize arrays, even if they would be initialized without an initializer list.

  2. How can you get the size of an array in bytes?
    The size of the entire array is equal to the array’s length multiplied by the size of an element.

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

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

What happened to last two questions? :wink:

My bad, I got distracted and sent without finishing.

  1. How can you use the function sizeof in order to calculate the number of elements in an array?
    “Using algebra, we can rearrange this equation: array length = array size / element size. sizeof(array) is the array size, and sizeof(array[0]) is the element size, so our equation becomes array length = sizeof(array) / sizeof(array[0]). We typically use array element 0 for the array element, since it’s the only element guaranteed to exist no matter what the array length is.t”

  2. What happens when you try to write outside the bounds of the array?
    “C++ does not do any checking to make sure that your indices are valid for the length of your array. This could overwrite the value of another variable, or cause your program to crash.”

1 Like
  1. a function (not a literal function, i’m using this word out of it’s context in the coding space) that allows you to store multiple values/variables(it’s complicated to explain please just try to follow along)
  2. whicheverArray[1];
  3. idk what this question really means but here it is: you can set a given value for an array (a fixed array) that’s about it.
1 Like

Think about the following questions while reading:

1. What is an array?

An array is a list of variables of the same type that can be accessed 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 length and size is known at compile time

  1. How can you initialize an array?

  2. int array[3]{};

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

Undefined behavior.

1 Like
  1. block of data of the same type bundled together and accessible through the array variable, an indexed list

  2. 1

  3. element size multiplied by the length of the array.

  4. type[length of array (optional)] nameOfArray = {elements to put into it};

  5. sizeOf function

  6. divide the array size by the element size

  7. memory location outside the array boundary is overwritten which may cause unexpected behaviour

1 Like