-
Itâs a group of data of the same variables.
-
[1].
-
The length of an array is predetermined in a fixed array while it can be changed in a dynamic array.
-
type name[number] ={ variables}
-
find the sizeof(array)
-
sizeof(array)/sizeof(array[0]) since it will divide total number of bytes by the amount of bytes per each element in the array.
-
It will go into undefined behavior.
Part 1:
-
What is an array?
Like weâve learned in Javascript, 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?
The index used to access the second variable in an array would be [1]. -
What can you say about the size of an array?
The size of an array is the number of elements. The length (number of the elements) has to be determined before compilation (compile-time constant).
Part 2:
-
How can you initialize an array?
We can initialize it element by element or with an initializer list (faster option). -
How can you get the size of an array in bytes?
We can use the function sizeof(array); -
How can you use the function sizeof in order to calculate the number of elements in an array?
We divide it like: sizeof(array) / sizeof(array[0])
For earlier versions of C++ we use a standard function std::size by including #include -
What happens when you try to write outside the bounds of the array?
It will generate undefined behavior because the value will be inserted in memory.
Part 1:
- stored indexed objects
- 1
- array length * element size
Part 2:
- variable followed by curly braces: int array[5] {1,2,3,4,5};
- sizeof(array)
- sizeof(array)/sizeof(element)
- undefined behavior
An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
1
In common usage, the terms âarray sizeâ and âarray lengthâ are both most often used to refer to the arrayâs length. 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 )
You can use an initializer list
sizeof(array),
sizeof(array) / sizeof(array[0])
undefined behaviour
Part 1
- An array is a data type that letâs us access many variables of the same type through a single identifier.
- index[1]
- That it is fixed in size.
Part 2
- You can initialize an array element by element or use initializer list.
- By using sizeof() operator.
3.sizeof(array[0]) is the element size, so our equation becomes array length = sizeof(array) / sizeof(array[0]) - It will cause undefined behavior.
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 The size of my array is defined is how many variables are in it.
1 One way to initialize an array is to do it element by element or use of an nitializer list.
2 the number of total integers multiplied by 4 because on a 32bit program every integer represents 4 bytes and every char represents 2 bytes.
3 sizeof (array) / sizeof (array [0])
4 it is assigned to the next byte in memory which runs as an undefined behavior that can cause errors or the entire program crashing.
Hey @Ray
For the 2 question of the second part you can just use sizeof(array) as you did in question 3.
sizeof(array) is the size of your array, and sizeof(array[0]) the size of the first element.
Also a char will always be represented by 1 bytes.
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? A parameter called a subscript (or index) that tells the compiler which element we want. This process is called subscripting or indexing the array.
What can you say about the size of an array? A fixed array (also called a fixed length array or fixed size array) is an array where the length is known at compile time.
The following array definition is essentially equivalent:
1 int testScore[30]; // allocate 30 integer variables in a fixed 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).
In the above example, we declare a fixed array named testScore, with a length of 30. A fixed array (also called a fixed length array or fixed size array) is an array where the length is known at compile time. When testScore is instantiated, the compiler will allocate 30 integers.
How can you initialize an array? One way to initialize an array is to do it element by element or to initialize entire arrays via use of an initializer list. Such as: 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? The sizeof operator can be used on arrays, and it will return the total size of the array in bytes (array length multiplied by element size).
How can you use the function sizeof in order to calculate the number of elements in an array? Using algebra, this equation: array length = array size / element size. sizeof(array) is the array size, and sizeof(array[0]) is the element size, so the equation becomes array length = sizeof(array) / sizeof(array[0]). Typically uses an array element 0 for the array element, since itâs the only element guaranteed to exist no matter what the array length is.
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.
Quiz
- Declare an array to hold the high temperature (to the nearest tenth of a degree) for each day of a year (assume 365 days in a year). Initialize the array with a value of 0.0 for each day.
int main(){
double temperature[1] = {};
temperature[0] = 10;
cout << "The average is " << (temperature[0] / 365) << endl;
return 0;
}
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 The size of an array can be fixed or dynamic.
4 eg. myArray[4] = (1, 34, 56};
5 the length of the array * element size.
6 sizeof(array) sizeof(array[0]) = number of elements.
7 You get undefined behaviour.
Hi @cherrybluemoon
You are initializing an array of 1 element not 365 in your example
So if you try to access an other index of your array you will have (in addition of a lot of warning when you compile) random value for each index (regarding your computer memory).
Regarding âthe averageâ you are displaying, it is just the first value of your array divide by 365, it is not the average of your array.
Thanks for input, I will continue to work on this.
Ok, I am at a lost, been working on this for a couple of days now, but will continue the challenge. This stuff is fun!
-
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?
Because we start to count with 0, the second variable is [1] -
What can you say about the size of an array?
The length of an array depends on his type: fixed or dynamic array
- fixed array: fixed length, which can´t be changed, because of memory allocating at compile time.
- dynamic array: length can be set at runtime and length can be changed, but more complicated to instantiate.
----------------arrays-part-ii.----------------------------------------
-
How can you initialize an array?
- do it element by element;
- with an initializer list : int prime[5]={1,3,5,7,9};
-
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(array[0]); -
What happens when you try to write outside the bounds of the array?
Undefined behavior, because C++doesnât check if the length of an array is valid with your input elements. Could overwrite the value of another variable or cause your program to crash.
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 (one)
3. What can you say about the size of an array?
A fixed array (also called a fixed length array or fixed size array ) is an array where the length is known at compile time.
The length of a dynamic array can be set at runtime, and their length can be changed.
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 is via use of an initializer list .
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?
Divide the value returned by the function sizeof by the length of the array.
4. What happens when you try to write outside the bounds of the array?
Undefined behavior - the value of another variable could be overwritten or the program could crash.
Read this article (http://www.learncpp.com/cpp-tutorial/61-arrays-part-i/ 7).
Think about the following questions while reading:
-
What is an array? An object that holds many values at once within sequential a address.
-
Which index would you use to access the 2nd variable in an array? index 1, ie myArray[1].
-
What can you say about the size of an array? The max size is dependent on the system hardware and memory, and index length should reach the systems max integer value, dependent on wether more memory intensive data (ie. objects) are stored as appose to less memory intensive data (ie. integers).
Read this article (http://www.learncpp.com/cpp-tutorial/62-arrays-part-ii/ 11).
Think about the following questions while reading:
- How can you initialize an array? From my personal code snippets notes:
// use initializer list to initialize the fixed array
int prime[5] = { 2, 3, 5, 7, 11 };
// Initialize all elements to 0
int array[5] = { };
// let initializer list set length of the array
int array[] = { 0, 1, 2, 3, 4 };
-
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(array[0]).
-
What happens when you try to write outside the bounds of the array? It will cause undefined behavior, unpredictable results, and hard to trace errors.
-
What is an array?
itâs a data type that groups variables of the same type in an indexed ordered structure -
Which index would you use to access the 2nd variable in an array?
indexing of arrays starts with 0 so the second element of an array will have the index 1 and to access it the necessary code would look like this:
array_name[1] -
What can you say about the size of an array?
there are two kinds of array, fixed and dynamic, the first one does not allow changes in its lenght, the second one can be changed during runtime -
How can you initialize an array?
there are many ways of initializing arrays:
long array[4]={0,0,0,0}
-
How can you get the size of an array in bytes?
using the command sizeof(array) -
How can you use the function sizeof in order to calculate the number of elements in an array?
by dividing the size of the array byt the size of an element
sizeof(array) / sizeof(array[0]);
-
What happens when you try to write outside the bounds of the array?
the result is undefined behaviour due to the fact that itâs not possible to know what section of the memory will be allocated to the element
Part 1
- A collection of values of the same type that can be accessed by an index.
- [1]
- It is the length of the array multiplied by the size of each element.
Part 2
- Either one element at a time, or by using an initializer list.
- By using the sizeof() operator.
- sizeof(a) / sizeof(a[0])
- The code will try to write to a location in memory where the non-existent element is expected to be resulting in undefined behaviour. The program could crash (one would hope).
PART1:
- What is an array?
- An array is an aggregate data type that allows us to access many variables of the same through a single identifier
- Which index would you use to access the 2nd variable in an array?
- If the variable of the interger was âducks,â and you want to access the 2nd variable, then use ducks[1].
- What can you say about the size of an array?
- There are fixed arrays and dynamic arrays. Fixed arrays can have a fixed number where it will be accessible through 0 to fixed number-1. A dynamic array can be set at runtime
PART2:
- How can you initialize an array?
- You can initialize an array by inserting the desired objects, numbers, etc⌠in the curly brackets
- How can you get the size of an array in bytes?
- sizeof(array) - 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?
- You can determine the length of a fixed array by dividing the size of the entire array by the size of an array element
- What happens when you try to write outside the bounds of the array?
- Will get an undefined behaviour, possibly 0 as the output