Its a trick you can use because enums also start with 0, so defining the last element as maxWeekday
will give you the number of weeks that you can use to define arrays with the correct number of elements for example.
-
What is an array?
An array is a collection of data points that can be accessed using a single identifier. These must have the same data type. -
Which index would you use to access the 2nd variable in an array?
[1]. -
What can you say about the size of an array?
It can be declared upon the array being made or can be assembled by the compiler when the code is run. -
How can you initialize an array?
You can use an initializer list where you populate the array when it is defined. -
How can you get the size of an array in bytes?
You can use the sizeof() function. -
How can you use the function sizeof in order to calculate the number of elements in an array?
sizeof() / sizeof(array[0]) -
What happens when you try to write outside the bounds of the array
It will save the value into memory and can cause undefined behavior to occur in the code.
-
A data structure that allows the storing of a single data type.
-
array[1]
-
The length can be defined or undefined but if it is defined than the length is fixed and values added over the predefined length it will result in undefined behavior.
-
int array[5]{};
-
size(array)
-
sizeof(array)/sizeof(array[1]) . To get the number of elements just divide the array by one element.
-
It will create undefined behavior as the out of bounds element will overwrite other data.
Its better to use the first array element (index 0) than the second one because you can be sure that at least one element in array will exist, if one creates an array with the size of one this could fail. Or use the array type instead.
1. What is an array?
An aggregate datatype where the elements or values are of the same type.
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’s the number of elements * the size of the type.
-
How can you initialize an array?
Using a list likeint arr[3] = {4,5,6};
-
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) / sizeof([type]);
-
What happens when you try to write outside the bounds of the array?
Undefined behavior, the program could crash if some other data is overwritten.
-
How can you initialize an array?
One example: int prime [30]{}; -
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). -
How can you use the function sizeof in order to calculate the number of elements in an array?
int main(){
int array[]{1, 1, 2, 3, 4, 5, 8, 13, 21};
std::cout << The array has: " sizeof(array) / sizeof(int) << " elements\n";
//or
std::cout << sizeof(array) / sizeof(array[0]) << ‘\n’;
}
-
What happens when you try to write outside the bounds of the array?
When this happens, you will get undefined behavior – For example, this could overwrite the value of another variable, or cause your program to crash.
-
Arrays are an aggregate data type that lets us access many variables of the same type through a single identifier.
-
index 1
-
The size of an array is array length * element size. The array length can be declared by the programmer e.g. array [5], or can be counted by the compiler based on how many elements there are in the array.
-
Easiest way to initialize an array is to create an initializer list. Initializing the below array with an initializer list would look as follows:
int numberOfDogs[4]{3,4,5,6,};
-
By using the sizeof operator.
-
sizeof(array) / sizeof(array[0]) = element size
-
If you write outside the bounds of an array you will get undefined behavior.
Part 1:
1 - Arrays are data-types that allow us to access multiple variables from a single identifier.
2 - [1].
3 - Arrays can be either dynamic or fixed in size. Dynamic arrays allow for the length of the array to be modified when it is time for them to be compiled. Fixed arrays have a set length and cannot be modified when they are being compiled.
Part 2:
1 - Arrays can be intitialized element by element, this, however, is a time consuming method to initialize arrays.An easier way to intitalize arrays in C++ is through the use of an intitializer list. The example below shows how an array can be intitalized in C++. If like in the example below there are less initializers than the array can hold, the remaining elements get initialized to 0, this is known as zero initialization . If there are more initializers than the array can hold then the compiler will return an error.
int main()
{
int array[5] {7, 4, 5};
cout<< array[0]<< endl;
cout<< array[1]<< endl;
cout<< array[2]<< endl;
cout<< array[3]<< endl;
cout<< array[4]<< endl;
cout<< array[5]<< endl;
return 0;
}
// 7, 4, 5, 0, 0
2 - To determine the size of the array above you would do the following:
int main ()
{
int array[] {7, 4, 5};
cout<< sizeof(array)<<endl;
cout<<sizeof(int)<<endl;
return 0;
}.
This function uses the sizeof ( array ) operator. This operator will count the number of elements in the array and will multiply by the size of the integers in bytes - this value is typically 4 bytes. This will return an array size of 12. The sizeof ( type of element ) operator can be used to determine the size of each individual integer.
3 - To determine the number of elements in an array you would call the function below. Type of element tells C++ what kind of element is in the array, i.e. an integer(written as int) or a string (written as string)
cout << sizeof(array) / sizeof(type of element) << endl;
4 - You will get undefined behaviour as it could potentially overwrite the value of another variable or cause the program to crash.
-
What is an array?
Type of data where we can store many variables. -
Which index would you use to access the 2nd variable in an array?
[1] -
What can you say about the size of an array?
The size of an array is the length meaning a number of variables stored in the array. -
How can you initialize an array?
By adding values in the curly braces. -
How can you get the size of an array in bytes?
Using sizeof operator. -
How can you use the function sizeof in order to calculate the number of elements in an array?
You apply sizeof array divided by sizeof array element to get the number of elements. -
What happens when you try to write outside the bounds of the array?
undefined behaviour.
1.What is an array?
It is an aggregated data type that let 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
- What can you say about the size of an array?
Size of the array is equal to the array’s length multiplied by the size of an element: array size = array length * element size.
…
- How can you initialize an array?
I) element by element:
int prime[5];
prime[0]= 2;
prime[1]= 3;
prime[2]= 5;
prime[3]= 7;
prime[4]= 11;
II) via initializer list: int prime [5]{2, 3, 5, 7, 11}; // use initializer list to initialize the fixed array
II a) int array[5]{ 0, 1, 2, 3, 4 }; // explicitly define the length of the array
II b) int array[]{ 0, 1, 2, 3, 4 }; // let the initializer list set length of the array
-
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?
We can determine the length of a fixed array by dividing the size of the entire array by the size of an array element:
sizeof(array) / sizeof(array[0]) -
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.
- An aggregate data type that allows for groups of data of a specific type to be stored and accessed.
- index 1
- The size of the array or length of the array is the number of items stored in the array itself, arrays are fixed in size
prt-2
- By setting the size of the array and individually setting each index of the array equal to a value
- You call the sizeOf() function and pass the array in the parameter, sizeof(arr)
- sizeOf(arr)/sizeOf(arr[0])
- You get undefined behavior and run into an index out of range error
P1
- " An array is an aggregate data type that lets us access many variables of the same type through a single identifier."
- 1
- Arrays may have fixed or dynamic size and are defined in different ways depending on this.
P2
- The most convenient way is with an initializer list. Example: int prime[5]{ 2, 3, 5, 7, 11 };
The length of the array can be omitted and in this case the complier will figure it out based on the elements in the list. - sizeof(array);
- You can get the number of elements by dividing the size in bytes of the whole array by the size of a single element: sizeof(array)/sizeof(array[0]);
- When writing outside the array bounds you will get undefined behaviour, for example overwriting values in another variable.
What is an array?
An array is a variable that can store multiple values of the same type.
Which index would you use to access the 2nd variable in an array?
Index number would be 1, because Index of an array is always arrayLength - 1, because the index of an array starts with 0.
What can you say about the size of an array?
An array can be given a fixed size when declaring it by filling in the size like this [8]. The array can also figure out it’s size by itself when initializing values inside { }. So int numbers[]{1, 2, 3, 4} will set the array to size of 4 entries.
How can you initialize an array?
directly likes this: string names[]{sandra, albert, freddo, janine};
or step by step like this: string names[2]; names[0] = “sandra”; names[1] = “albert”;
How can you get the size of an array in bytes?
string strings[8];
cout << "Array strings size: " << sizeof(strings); << endl;
How can you use the function sizeof in order to calculate the number of elements in an array?
sizeof(strings) / sizeof(strings[0];
Since C++ 17 size() was introduced which will work for arrays and vectors as well. sizeof() is little more complicated.
What happens when you try to write outside the bounds of the array?
Error.
ARRAYS PT1
-
An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
-
Index 1, by the syntax arrayName[1].
-
The size of an array is the length of an array. In C++ there is a fixed size array whose length is known at compile time and there is a dynamic array whose length is known at runtime.
ARRAYS PT2
-
An array is initialized by use a list initializer like so; typeOfArray arrayName[5]{1,2,3,4}
-
By the use of sizeof operator which will return the array length multiplied by the element size.
-
sizeof(array) / sizeof(array[0]).
-
It results in an undefined behavior.
Technically its undefined behavior, but most modern compilers should throw an error.
Part 1
- What is an array?
Array is a data type that let us put many variables inside single identifier - Which index would you use to access the 2nd variable in an array?
[1] - What can you say about the size of an array?
The size of array in C++ cant have a lenght based on user input or any other value calculated at runtime
Part 2
- How can you initialize an array?
You can do it manualy:
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 by using initalizer list:
int prime[5]{ 2, 3, 5, 7, 11 }; // use initializer list to initialize the fixed array
- How can you get the size of an array in bytes?
sizeof() - How can you use the function sizeof in order to calculate the number of elements in an array?
sizeof()/sizeof(array[0]) - What happens when you try to write outside the bounds of the array?
C++ will set that value into an element that does not exist and we will get undefined behavior that can even crash entire program
A)
1. What is an array?
An array is an aggregate data type or a structure that lets us access many variables of the same type through a single identifier, and a subscription to specify a single element/operator in the array.
2. Which index would you use to access the 2nd variable in an array?
One would use the subscript operator [1]
3. What can you say about the size of an array?
Independently of the array size, you can access the last array element through the Length N-1. It can have a fixed or dynamic size, also can be referred to as the length of the array.
B)
-
How can you initialize an array?
You can initialize one element at a time as well as, and better option with, initializer list, as in: int prime[5]{2,3,5,7,11}… -
How can you get the size of an array in bytes?
Using a “std::cout << std::size(array)” in a version older than 17 and;
with “sizeof[]” in earlier versions. -
How can you use the function sizeof in order to calculate the number of elements in an array?
Dividing the array size by the size of the element in the array.
sizeof(array)/sizeof(array[0]);
- *What happens when you try to write outside the bounds of the array?
It can occur an overwrite of another variable, print 0, or crash the program.
-
What is an array? — aggregate data structure storing many variables of same type but with single identifier
-
Which index would you use to access the 2nd variable in an array? — 1
-
What can you say about the size of an array? — can be a fixed size (fixed array) where size is defined at declaration in compile time: arr[5]; size can also be dynamic in dynamic arrays, size of array is always length or range -1
-
How can you initialize an array? — [] signifies an array, so there are multiple ways:
- int arr[]; //uninitialized
- int arr[5] {}; //initializes all to zero
- int arr[5] = {}; //initializes all to zero
- int arr[5] = {0}; //initialize all to zero
- int arr[5] = {1, 2, 3, 4, 5};
- int arr[5] = {1, 2, 3}; //remaining elements initialized to zero, called zero initialization
- int arr[] = {1, 2, 3, 4, 5}; automatically sets size to 5 (number of initialized elements), useful if changing number of elements upon initialization
-
How can you get the size of an array in bytes? — using sizeof(array)
-
How can you use the function sizeof in order to calculate the number of elements in an array? — sizeof(array) / sizeof(array[0]; this divides array size in bytes by size of an array element, will only work for fixed array
-
What happens when you try to write outside the bounds of the array? — compiler error, undefined behaviour, program cash