1a. An array is a data type that can aggregate variables of the same type.
2a. To access the second variable in an array we use array[1]
because arrays start with count 0.
3a. The size of an array can be fixed or dynamic. There are instances where one would be a better choice than the other. We can declare the size of an array in brackets when we initialize the array, or we can leave the brackets empty and the compiler will set the length of the array upon initialization.
1b. We can initialize array elements one at a time
int array[2]{};
array[0] = 2;
array[1] = 4;
or by using an initializer list:
int array[5]{2,4,6,8,10};
If we do not define a value for an array element and there is space allocated for the element, it will automatically be given a 0 value.
2b. sizeof(array) will return the number of elements * the size in bits, which for integers is typically 4.
3b. We can simply divide the sizeof(array) value (in bits) by the array’s first element. Since every element is of the same type, this will return the bit size of the elements being used.
sizeof(array)/sizeof(array[0])
4b. Writing values to places outside the array bounds will result in undefined behavior. This can replace other values in the array, or cause the program to crash.
-
An array is a variable that can store multiple values of the same type.
-
Arrays are indexed starting at 0, so the second variable is indexed with 1.
-
An array size is the number of indexes it has or how many variables it can store.
-
You can initialize an array element by element or using an initializer list { }
-
Use sizeof() operator.
-
The length of a fixed array can be calculated by dividing the size of the entire array by the size of the array element.
-
You will receive undefined behavior.
- list of given values
-
- size of an array is determined by how many values are in it
1)by int array
2)sizeof(arr)
3)sizeof(arr) divided by 0
4)will result in undefined behavior.
Part 1
- An array is an aggregrate data type that allows us to access and manipulate multiple variable of the same type through a single identifier.
- [1]
- The size of an array is the length of the array multiplied by the element size.
Part 2 - With an initializer list: type identifier[]{};
- sizeof()
- sizeof(array) / sizeof(array[0])
- Undefined behavior
- An array is and aggregate data type that allows you to access many variables of the same type through a single identifier.
- For the second element,
array[1]
- The size of an array is fixed at compile time.
-
You can initialize one element at a time or through an initializer list.
-
To get the size of an array in bytes,
sizeof(array)
-
To get the number of elements from sizeof, use
sizeof(array)/sizeof(variable type)
-
When you write outside of the bounds of an array, you get an undefined behavior. You may overwrite another value in memory or the program may crash.
Oh tank, i didn’t see that one!
How would dividing by 0 help?
Got that wrong … just finished that section of the course
-
a set of variables under one identifier
-
the index 1
-
we can declare a fixed length for the array for each respective function and the operation needed.
-
with an initializer list
-
sizeof(array)
3.you can divide the size of the array by the size of an array element
4.the behavior is undefined. It could potentially try to overwrite the valuable of another variable.
First part of the assignment
-
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. -
What can you say about the size of an array?
The size of an array (number of variables) is written in [square brackets] and is known as the array length.
-
How can you initialize an array?
With the use of an initializer list. -
How can you get the size of an array in bytes?
With sizeof(array). -
How can you use the function sizeof in order to calculate the number of elements in an array?
Sizeof(array)/ Sizeof(array element). -
What happens when you try to write outside the bounds of the array?
It will result in an undefined behavior.
-
An array is a data type aggregating many variables of the same type through a single identifier.
-
1
-
Array size can be fixed when the length is known at compile time or can be defined when the program run (dynamic array).
-
Array elements are not initialized when created, you can do it using an initializer list.
-
Use the sizeof operator to return the total size of the array (array length multiplied by element size).
-
By dividing the size of the entire array by the size of an array element: sizeof(array) / sizeof(array[0])
-
Undefined behavior as the value will be inserted into memory where the next element would have been had it existed.
Part 1
Is a data type that allow us to access many variables of the same type through a single identifier.
index[1];
That is equal to how many elements an array has. Also called the array length.
Part 2
there are many possibilities:
- int array[4]; // Do it element by element but it could be tedious.
array[0] = 2;
array[1] = 5;
array[2] = 8;
etc… - int array[4] { 1, 5, 4, 9, 10 }; // with a initializer list. or
- int array [ ] {1, 5, 4, 9, 10 }; // let the initializer list set length of the array.
sizeof(Array)
sizeof(array) / sizeof(array element).
Could output a result that you don’t expect it.
- 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
- What can you say about the size of an array?
An array size can be set before a program is ran through a fixed array length value. However, a dynamic array can change in size after the program begins executing, perhaps based off of user input.
- How can you initialize an array?
Through an initializer list, you can initiate an array’s elements all one line.
- How can you get the size of an array in bytes?
sizeof(array) will give you 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?
You can divide the returned value from the sizeof function by 4 since 4 bytes make up an integer value.
- What happens when you try to write outside the bounds of the array?
This will result in an undefined behavior because the data is stored somewhere outside of the array. Since the variable is stored in a random location C++ would write over memory stored in RAM.
-
What is an array?
An array is a collection of elements of the same type placed in contiguous memory locations that can be individually referenced by using an index to a unique identifier. Five values of type int can be declared as an array without having to declare five different variables (each with its own identifier). -
Which index would you use to access the 2nd variable in an array?*
-
The first index in an array is 0, so if we want to access the first element in an array, we’d use index 0. The second element is at index 1, the third is at index 2, and the fourth is at index 3…
-
What can you say about the size of an array?*
In an array variable declaration, we use square brackets ([]) to tell the compiler both that this is an array variable (instead of a normal variable), as well as how many variables to allocate (called the array length ). The size of an array can depend on the data type stored in it and the number of variables allocated to it.
SECOND SET:
-
How can you initialize an array?
You can initialize the elements of an array with a loop as follows:
int nScores[100]; // declare the array and then…
for (int i = 0; i < 100; i++) // …initialize it
{
nScores[i] = 0;
}
You can also initialize an array when you declare it by including the initial values in braces after the declaration. For a small array, this is easy:
int nCount[5] = {0, 1, 2, 3, 4};
Here the value of nCount[0] is initialized to 0, nCount[1] to 1, nCount[2] to 2, and so on. If there are more elements than numbers in the list, C++ pads the list with zeros. Thus, in the following case:
int nCount[5] = {1};
the first element (nCount[0]) is set to 1. Every other element gets initialized to zero. You can use this approach to initialize a large array to zero as well:
int nScores[100] = {0};
This not only declares the array but initializes every element in the array to zero.
-
How can you get the size of an array in bytes?
To determine the size of your array in bytes , you can use the sizeof operator: int a[17]; size_t n = sizeof (a); On my computer, ints are 4 bytes long, so n is 68. -
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:
9 #include
int main()
{
int array[]{ 1, 1, 2, 3, 5, 8, 13, 21 };
std::cout << “The array has: " << sizeof(array) / sizeof(array[0]) << " elements\n”;
return 0;
}
This printed:
The array has: 8 elements
Note that this will only work if the array is a fixed-length array, and you’re doing this trick in the same function that array is declared in
-
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. So if you insert into an array where there is not an element existing or allocated, you will get undefined behavior – For example, this could overwrite the value of another variable, or cause your program to crash.
Although it happens less often, C++ will also let you use a negative index, with similarly undesirable results.
- An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
- 1
- The size of an array is declared ahead of time using a variable inside square brackets Array[5]
- int array[5]{1, 2, 3, 4, 5};
- SizeOf(Array)
- sizeof(arr)/sizeof(int)
- Undefined behavior
- What is an array?
- Data structure of many variables for that 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?
- They all have their own lengths depending on how many variables are in the array.
1.How can you initialize an array?
- Char [x] {1, ,3, 6, 12, 23};
2.How can you get the size of an array in bytes?
- use “sizeof” divide total size of array by the array element.
3.How can you use the function sizeof in order to calculate the number of elements in an array?
-By getting the predeclared array size.
4.What happens when you try to write outside the bounds of the array?
-unexpected behavior
1.What is an array? its an aggregate data type that lets you 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] would be used, as 0 occupies the 1st variable.
3.What can you say about the size of an array? Elements inside [ ] bracket multiplied by the arrays length by elements size which is called the range.
Part 2.
- How can you initialize an array? It can be done painfully by elements by element or we can use an element list.
- How can you get the size of an array in bytes? The sizeof operator can be used on arrays length multiplied by element size.
- How can you use the function sizeof in order to calculate the number of elements in an array? sizeof(array)/sizeof(array[0]) = elements.
1.What happens when you try to write outside the bounds of the array? When writing outside the bounds you get an undefined behavior and could crash program.
*1. **What is an array? An array is an aggregate data type that lets you access many variables of the same type with one identifier.
2. Which index would you use to access the 2nd variable in an array? The second variable has index of 1.
3. What can you say about the size of an array? Size of a fixed array is defined by a number in brackets [ ] and can be any length.
- How can you initialize an array? You can intialize and array by putting variables in curly braces separated by commas { 1, 3, 5 …} or you can intialize as 0 for int, 0.0 for double, or empty string for strings by leaving the braces empty.
- How can you get the size of an array in bytes? Use std::sizeof.
- How can you use the function sizeof in order to calculate the number of elements in an array? The sizeof (array) divided by sizeof element [0].
- What happens when you try to write outside the bounds of the array? You will get undefined behavior … program results are wrong or cannot be trusted.