Arrays in C++ - Reading Assignment

What is an array?
An array is a collection of variables (called elements) which can be accessed by the array name and the elements subscript (index)

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

What can you say about the size of an array?
It has to be stated as a constant at compilation time if it is a fixed array. Trying to access an out of bounds index will produce unforeseen results.

How can you initialize an array?
Individual elements can be initialized eg myArray[4] = 23;
Or they can be initialized on mass by using an initializer list eg int myArray[5] = { 12, 5, 56, 0, 23};

How can you get the size of an array in bytes?
Use the sizeof(anArray) function but not on an array passed into a function this just gives the size of the pointer to the array.

How can you use the function sizeof in order to calculate the number of elements in an array?
You divide the total size of the array by one single element eg sizeof(anArray) / sizeof(anArray[0]) = number of elements in array.

What happens when you try to write outside the bounds of the array?
You will get undefined behavior - which is usually very bad

1 Like

Part I

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

  2. array[1]

  3. The array::size member function returns the size of the array is terms of number of elements (unlike the sizeof language operator, which returns the size in bytes, see below under Part II).
    A fixed size array (or fixed length array or fixed array) is an array where the length is known at compile time.

Part II

  1. We can initialize it element by element (but it is cumbersome), or better, by using an initializer list, between curly brackets, for instance:
    int array[4] = { 1, 3, 5, 7 };
  2. By using sizeof(array)
  3. I need to divide the size of the array in bytes by the size of one element x: sizeof(array) / sizeof(array[x])
  4. Although the value calculated from the subscript out of the range will be inserted into memory, the program will generate an undefined behaviour and may possibly crash!

.

1 Like

Answers

Arrays Part 1

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

  2. We should use Index[1]

  3. There are two types of array: fixed and dynamic. Fixed arrays have a fixed length, dynamic can be changed during runtime.

Arrays Part 2

  1. We should declare the type of array, followed by the name. We can omit declaring the length of the array. For instance: int array[] = {0, 1, 2}.

  2. We can use the sizeof operator --> sizeof(array).

  3. Number of elements in an array = sizeof(array) / array[0]

  4. We will get undefined behavior, crash of the program or unexpected overwrite of values.

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. A fixed array is an array where the length is known at compile time. A dynamic array is different. It can have its length set at runtime, and its length can be changed.

  4. Several ways, but an initializer list is a convenient way to do it. eg:
    int prime[5] = { 2, 3, 5, 7, 11 }

  5. sizeof(array)

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

  7. You get undefined behavior. The value will be inserted into memory where the element would have been had it existed, which could overwrite the value of another variable, or cause your program to crash.

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.

2 Index 1

  1. Size of the array is defined at initialization (array length * data type size) and cannot be changed (for static arrays).

Part 2

  1. Entire arrays can be initialised with the help of an initializer list. For eg.
    int prime[5] = { 2, 3, 5, 7, 11 };

  2. sizeof(array)

  3. You can get the number of elements of an array like this:
    sizeof(arr) / sizeof(arr[0]);

  4. Undefined behaviour and unintended data manipulation

@ivga80: Please format your code correctly.

1 Like

Part 1:

  1. What is an array?
    “an aggregate data type that lets us access many variables of the same type through a single identifier.” Basically, a variable that contains many variables of the same kind.

  2. Which index would you use to access the 2nd variable in an array?
    We’d use [1], since [0] is the 1st variable in an array.

  3. What can you say about the size of an array?
    The size of an array, or the number of elements within an array, can be defined as fixed or variable in the sense that it can be truncated, split, added/substracted.

Part 2:

  1. How can you initialize an array?
    By using brackets: int array[3] = {1,2,3}

  2. How can you get the size of an array in bytes?
    By taking the size of a pointer (passing the array through a void function and returning sizeof(), then multiplying it by the number of elements in the array.

  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    By dividing the entire array by the size of an array element.

  4. What happens when you try to write outside the bounds of the array?
    It will compile, but will cause undesirable results afterwards when trying to access it, so don’t do it!

2 Likes

PART I

  1. What is an array?

An array is a data type which consist of group of many variables of the same type that can be accessed through a single identifier.

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

To access the 2nd variable in an array you would use index 1. exampleArray[1]

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

A size of the array is equal to the array’s length multiplied by the size of an element (array size = array length * element size). There are two types of arrays in C++. A fixed size array is an array where the length is known at compile time and dynamic array where array length can be set at runtime, and its length can be changed.

PART II

  1. How can you initialize an array?

One way to initialize an array is to do it element by element, which is not so practical so better and simpler way is to initialize entire array by using an initializer list.

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

When sizeof operator is used on arrays,it returns the total size of the array in bytes (array length multiplied by element size)

  1. 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 (array length = array size / element size).

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

Since C++ does not do any checking to make sure that your indices are valid for the length of your array, the value of the element outside bounds of array will be inserted into memory where the new element with defined index would have been had it existed. This will result in undefined behavior, such as overwriting the value of another variable, or causing program to crash.

2 Likes
  1. An array is used to storage variables in ordered numbers.

2.Like Javascript ( as we start counting from 0) we would use [1] to select the second element in the array.

3.The size of an array is defined initially and is the number of variables inserted in it. This could be modified later on in the code , if you decide so , with functions.
P.S. there shoudn’t be any Pop or Push commands.

  1. string arrayTest [2]={“Franco” , “Bollo” , “Pio”};

  2. with the sizeof() command : sizeof(arrayTest);

3.You can calculate it by the size of the array/ the size of one element .

  1. If you try to write outside the bounds of the array you d get an undefined error .
2 Likes

Questions: Arrays Part 1

  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? - would use array[1] to access the second variable.

  3. What can you say about the size of an array? - the array size is the array length multiplied by the element size.

Questions: Arrays Part 2:

  1. How can you initialize an array? - by using an initializer list which entails listing out the array in between the {} after determining the array length.

  2. How can you get the size of an array in bytes? - by using the ‘sizeof’ operator

  3. How can you use the function size of in order to calculate the number of elements in an array? - this can be determined by dividing the size of an entire array (sizeof(array)) by the size of an array element (sizeof(array[0])). The std: :size(array) function can be used in later versions.

  4. What happens when you try to write outside the bounds of the array? - this leads to an undefined behavior that can overwrite a value in memory or crash the program

2 Likes

1. What is an array?
A variable where you can store indexed objects

2. Which index would you use to access the 2nd variable in an array?
First index is 0. The second index is 1. You access the second variable with the index 1 : 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.

1. How can you initialize an array?
Many possibilities :

  • int array[3] = { 7 }; // 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

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>);
Example with array of int : sizeof(array)/sizeof(int);

4. What happens when you try to write outside the bounds of the array?
You get undefined behavior. You can overwrite a value in memory, the program can crash…

2 Likes

1.1. An array is a table of variables of the same type.
1.2. This first element in the array is at 0, thus the second is at 1.
1.3. Arrays can be static, i.e. of fixed length, or dynamic , i.e. length determine at runtime.

2.1 You declare the array type, name and length, then you fill it with values. int array[5] = { 0, 1, 2, 3, 4 };
2.2 sizeof(array)
2.3 # of byte of the array divided by the number of byte of 1 element. sizeof(array)/sizeof(array[0]);
2.4 No errors when compiled, but unpredictable behavior at runtime.

2 Likes

Part 1

  1. An array is a data type that lets programmer to store the same type of variables in an indexed order.
  2. It should be the index of number 1.
  3. The size of array is array length multiplied by element size.

Part 2

  1. One can initialize an array through declaring element by element or use initializer list.
  2. The sizeof operator can be used on arrays, and it will return the total size of the array.
  3. One can determine the number of elements by dividing the size of the entire array by the size of an array element:
  4. One will get undefined behavior – this could overwrite the value of another variable, or cause the program to crash.
2 Likes

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

Which index would you use to access the 2nd variable in an array?
index 1, we start at index 0

What can you say about the size of an array?
he size of my array is defined is how many variables are in it.*

How can you initialise an array?
Arrays can be initialised after the declaration by specifying the index and the assigned value.

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) divided with sizeof(array)

What happens when you try to write outside the bounds of the array?*
This could overwrite the value of another variable, and make the program crash in worst case.

1 Like

I.1. What is an array?
An array is a data type that lets us acces many variables of the same type through a single identifier.
I.2. Which index would you use to access the 2nd variable in an array?
Index 1.
I.3. What can you say about the size of an array?
The size of an array equals the product between the length of the array and the element size.
II.1. How can you initialize an array?
Using an initializer.
II.2. How can you get the size of an array in bytes?
sizeof(array);
II.3. How can you use the function sizeof in order to calculate the number of elements in an array?
sizeof(array)/sizeof(array[0]), it is recommended to choose the element 0 because it is the only guaranteed no matter what the array length is.
II.4. What happens when you try to write outside the bounds of the array?
You get unexpected behavior like program crash, overwrite the value of another value.

2 Likes

First Article

  1. An array is a data type which allows to store and access many similar (in terms of type) variables by only using the array name to refer to it.
  2. [1], because indexes start from 0.
  3. The size of an array can either be fixed or dynamic, basing on the type of array created. In the first case, the size is known at creation time and will not be modifiable, while in dynamic arrays size can vary but it makes it more difficult to create and manage them.

Second Article

  1. Either element by element or using an initializer list.
  2. Using the sizeof operator, which will compute the multiplication between array length and number of elements.
  3. It is possible through a division between the size of the array and the size of a single element.
  4. The program would return an undefined behavior alert and either cause a crash or an overwriting of other variables.
1 Like

First Section:

  1. With an array you can store multiple data/information in one variable (which is the array) but they have to be from the same data type (e. g. all from the type int or string wtc.). This array variable is one identifier but you have access to multiple data just with one variable.

  2. array[1]. The counter starts with 0. Therefore, the second variable is represented through the index 1 and not 2.

  3. You have to given the array a fixed length when you define them, because then the memory can be assign to the different elements of the array. If you want to use an array without a fixed length you can use dynamical arrays. But the construction of these arrays differ from the normal arrays.

Second section:

  1. You can initialize an array just by assign every value to every element. You have to access to the whole array via all indexes and assign a value to it. When you use large arrays, it is wasteful. Instead you should use an initializer list. Therefore you have to define your are with a length and on the right side of the expression you use the following brackets -> {} in order to assign values to the array. I have just read that you dont have to use the = symbol anymore in order to assign value when you use initializer lists.

  2. You have to write sizeof(array_name)

  3. You can calculate it by using the following statement:

sizeof(array_name) / sizeof(array_name[0])

You don´t have to use the index 0, you can also use 1, 2 etc., but you have to make sure that there is a element on this array position

  1. It leads to unexpected behaviour. You cannot be sure what will happen to your array. Maybe it changes the well defined content inside your array.
2 Likes

Answer:

    1. An array is an object that can store more than one value of the same type in a single identifier.
    1. [1]
    1. In C++ the size of an array has to be known at compile time. It cannot be changed after the array has been created. The size of the array is always N-1 because in C++, we start counting from “0”.
1 Like

Answer:

    1. You can initialize an array element by element. You can use the initializer list. You can use enums for better readability of the documentation. Instead of accessing elements of the array by index, you can use user-friendly names.
    1. You can use sizeof operator on the array.
    1. You can find the number of elements in the array by dividing size of the entire array by the size of the array element.
    1. You may get an undefined behaviour.
1 Like

What is an array?
An array is a list of values of the same type.

Which index would you use to access the 2nd variable in an array?
Array[1] - the index would be 1.

What can you say about the size of an array?
A fixed array has an unchangeable size, whereas a dynamic array can be changed in size.

How can you initialize an array?
This is done through an array initializer list - by using the square bracjkets after after a variable.

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

How can you use the function sizeof in order to calculate the number of elements in an array?
Dividing the sizeof(Array) by the size of the first element in the array sizeof([0]).

What happens when you try to write outside the bounds of the array?
This will not store the data in the array but will place it in a memory location unassociated with the array.

1 Like

Part I

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

  2. The index you would use to access the 2nd variable in an array is 1.

  3. Fixed arrays have a fixed length that can’t be changed because they have memory allocated at compile time. Dynamic arrays in the other hand, can be set at runtime, and their length can be changed.

Part II

  1. You can initialize an array using an initializer list.

  2. You can get the size of an array in bytes by using sizeof(array).

  3. You can divide sizeof(array) by sizeof(array[0]).

  4. When you try to write outside the bounds of the array what you input will be stored into the memory, and will cause an undefined behavior, it could even overwrite the value of another variable.

1 Like