# Arrays in C++ - Reading Assignment

#41

Don’t forget the quiz!!! (see mine below)

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?
The index, or subscript within the subscript operator, would be 1.

What can you say about the size of an array?
The number of elements in an array, also known as array size, can be fixed or dynamically changed.

How can you initialize an array?
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

int prime[5] = { 2, 3, 5, 7, 11 }; // use initializer list to initialize the fixed array

or

// Initialize all elements to 0
int array[5] = { };

or

int prime[5] { 2, 3, 5, 7, 11 }; // use uniform initialization to initialize the fixed array (c++11)

or

int array[] = { 0, 1, 2, 3, 4 }; // let initializer list set length of the array

How can you get the size of an array in bytes?
int main()
{
int array[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
std::cout << sizeof(array) << ‘\n’; // will print the size of the array in bytes

``````return 0;
``````

}

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, 5, 8, 13, 21 };
std::cout << “The array has: " << sizeof(array) / sizeof(array[0]) << " elements\n”;

``````return 0;
``````

}

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.
When writing outside the bounds of the array, value will be inserted into memory where the 6th element would have been had it existed.
When this happens, you will get undefined behavior… this could overwrite the value of another variable, or cause your program to crash.

Quiz

1) 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.

``````#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
double temperature[365] = { 0.0 };
cout << setprecision(1) << fixed; //set the precision of cout so that 0.0 gets printed
cout << temperature[0] << endl;

return 0;
}
``````

2) Set up an enum with the names of the following animals: chicken, dog, cat, elephant, duck, and snake. Put the enum in a namespace. Define an array with an element for each of these animals, and use an initializer list to initialize each element to hold the number of legs that animal has.
Write a main function that prints the number of legs an elephant has, using the enumerator.

``````#include <iostream>
#include <iomanip>

using namespace std;

namespace Animals
{
enum Animals
{
CHICKEN, // 0
DOG, // 1
CAT, // 2
ELEPHANT, // 3
DUCK, // 4
SNAKE, // 5
MAX_NUMBER_OF_ANIMALS // 6
};
}

int main()
{
int animalLegs[] = {Animals::CHICKEN, Animals::DOG, Animals::CAT, Animals::ELEPHANT, Animals::DUCK, Animals::SNAKE}; // initializer list
animalLegs[Animals::CHICKEN] = 2;
animalLegs[Animals::DOG] = 4;
animalLegs[Animals::CAT] = 4;
animalLegs[Animals::ELEPHANT] = 4;
animalLegs[Animals::DUCK] = 2;
animalLegs[Animals::SNAKE] = 0;
cout << animalLegs[Animals::ELEPHANT] << endl;

return 0;
}``````

#42
1. ``````What is an array?
``````

An array is a type of container that can hold variables of a defined type, variables inside an array will be designated an index which will always start at 0.

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

index 0 = position 1
index 1 = position 2

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

it will always be 1 digit lower than the amount of elements inside of it.

1. ``````How can you initialize an array?
``````

int array[] {var1, var2, var3, var4, …};
int is the data type, array is an empty array, vars in curly brackets are the elements inside the array.

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

you can use the sizeOf() method. sizeOf(array);

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

you can divide the total size of the array by a single element.
int totalElements = sizeOf(array) / array[0];

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

unexpected behavior / program crash

Quiz

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

double yearlyTemp[365] {0.0};

1. Set up an enum with the names of the following animals: chicken, dog, cat, elephant, duck, and snake. Put the enum in a namespace. Define an array with an element for each of these animals, and use an initializer list to initialize each element to hold the number of legs that animal has.

#include

using namespace std;

namespace Animals{
enum Animals{
chicken; //0
dog; // 1
cat; // 2
elephant; //3
duck; //4
snake; //5
TOTAL_ANIMALS; //6
};
}

int main()
{
int legs[Animals::TOTAL_ANIMALS]{2,4,4,4,2,0};

``````cout << "An Elephant has" << legs[Animals::elephant] << "legs" << endl;

return 0;
``````

}

#43

1. What is an array?
It’s a special variable type that can storage many values of same type.

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

3. What can you say about the size of an array?
To create a fixed array, it’s necessary to define the length at compile time, because the compiler will allocate memory to storage all elements of this array.

4. How can you initialize an array?
After the statement of creating, initialize every element in array, like any simple variable. Another way, is pass an initializer list when the array is create.

5. How can you get the size of an array in bytes?
Use the sizeof() function and pass the array as argument.

6. How can you use the function sizeof in order to calculate the number of elements in an array?
Dividing the array’s sizeof by sizeof the any element of array.

7. What happens when you try to write outside the bounds of the array?
Program will has undefined behavior.

#44

# part 1

What is an array? It can be used to store multiple different values without having to create multiple unique variables. How many values you want to store must be known at compile time
Which index would you use to access the 2nd variable in an array? index 3 because the first element in the array has index 0
What can you say about the size of an array? The size is fixed and must be known at compile time

# part 2

How can you initialize an array? You can initialize an array by assigning a value for each element or by assigning the array to { }
How can you get the size of an array in bytes? by using sizeof(arrray)
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 by the size of the elements inside it
What happens when you try to write outside the bounds of the array? it could override the last value inside the array or crash the program: undefined behaviour

#45

1. What is an array?
Is a set of allocated memory spaces to store specified number of values of certain 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?
The memory size is the multiplication of size of the specific value type stored in array (set in the initialization) with the length of the array set during the initialization. You can decide what is the length (how many objects can be stored in the array) of the array while initializing the array.

1. How can you initialize an array?
You can initialize array in multiple ways:

``````int array[5]; // no initialization (undefined behaviour)
int array[5] = { 1, 2, 3, 4, 5 }; // initialize using the initializer list
int array[5] = {}; // initialize all elements to 0
int array[5] = { 1, 2, 3 }; // only initialize first 3 elements
int array[] = { 1, 2, 3, 4, 5 }; // let initializer list set length of the array
int array[5] { 1, 2, 3, 4, 5 }; // use uniform initialization to initialize the fixed array
``````

2. How can you get the size of an array in bytes?
using the sizeof function
3. How can you use the function sizeof in order to calculate the number of elements in an array?
sizeof(array)/sizeof(array[0]); // dividing the size of the whole array with the size of the single array element.
4. What happens when you try to write outside the bounds of the array?
Exception is thrown - “Segmentation fault”

#46
1. An array is a collection of datapoints

2. to access 2nd variable [1]

3. The size of an array must be defined before using it

part ii

1. to initialise, int array[30] = { 1, 2, 3, …}

2. array size in bytes sizeof(array)

3. sizeof / int bytes of 4

4. overstepping the bounds of an array could cause things to be overwritten or crash

#47

1.1 Array is a data type that allows for storage of multiple values in it of the same type e.g. int, char, double,etc.

1.2. array[1] accesses 2nd.

1.3. The size of an array is the memory space it occupies which is the number of elements times the size of a single element.

2.1. arrray[]={<elements>}
array[]{<elements>}

2.2. By using sizeof() inside the same function where the array is initialized

2.3. length= sizeof(array)/sizeof(array[0])

2.4. Undefined behavious: The value gets written to the location where that index would have been had the array been of that size. This might overwrite some other important data present at that location.

#48

What is an array?
grouped sequence of data values
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?
length * elementSize;

How can you initialize an array?
int array[3] = {1, 2, 3};
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 (but probably not what you intended).

#49

An array allows you to store multiple values of the same type as one variable.

You would use [1] to access the 2nd value.

C++ doesn’t really give you a way to get the size of an array from the array itself. For statically sized arrays, you can use sizeof to get the size of the array in bytes (which you can then divide by the sizeof value of the type the array holds to get the number of elements the array holds).

C++ allows you to initialize an array using an initializer list (a list of initial values in curly braces). If you have an initializer list whose size doesn’t match that of the array, the remaining elements in the array will be initialized to zero.

If you attempt to write outside of an array’s bounds, bad things will likely happen. You may overwrite some other code or data, which may crash the program. Just imagine the havoc that this can cause if an adversary is able to do this.

#50

PART ONE

1. A list of a type of variable.
2. 1, since the listing begins at 0.
3. That it depends in the total size of the elements that conform it, but is still less that setting all its elements as separate variables.

PART TWO

1. Like this: `vartype nameofmyarray[amount of elements in my array] = {elements to initialize the array};`
2. With the command: `size(myarray)`.
3. By telling the command the number of the index of the element which size I want to know, like this: `sizeof(myarray[index])`.
4. You may get an unpredictable behavior which is never good.

#51

Part 1

1. An array is a collection of data values that are of the same type.
2. To access the 2nd value in an array, you would need index 1.
3. The size of an array is fixed, so you must not exceed this limit to prevent errors in your code.

Part 2

1. `int testScore[30]; // int array of 30 test scores`
2. Using the `sizeof(arrayName);`
3. `sizeof(array) / sizeof(array[0])`
4. If you are outside the array bounds, your program will behave irregularly. The index you tried using will be saved in memory, however, you cannot access it using the array because of its bounds.

#52

Arrays in C++

6.1 - Arrays (Part !)

1. What is an array?

An array is a data type that contained numerous variables identified by a single identifier/

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

To identify the second variable in an array by indexing is to use, the program should be called by the expression array[1].

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

The size of an array can be defined on the quantity of variables stored within the array.

**6.2 - Arrays (Part II)

1. How can you initialize an array?

One can initialize an array using a loop. An example of array initialized by a loop is:

int nScores[100]; // declare the array and then…
for (int i = 0; i < 100; i++) // …initialize it
{
nScores[i] = 0;
}

Another method to initialize an array is by including initial values in braces after declaring the array Example of this is:

int nCount[5] = {0, 1, 2, 3, 4};

The output of this array becomes:

nCount[0]=0
nCount[1]=1
nCount[2]=2
nCount[3]=3
nCount[4]=4

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

One can determine the size of an array in bytes by stating the sizeof() function.

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

To use the sizeof() function to calculate the number of elements in array requires the arithmetic of diving the sizeof(array)/sizeof(array[0])

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

By writing outside of the bounds of the arrays will result in unpredictable behavior in the program , leading to an increase in difficulty to debug the program.

#53

What is an array?
An array is a variable that can hold several values of the same type.

Which index would you use to access the 2nd variable in an array?
The indexes in an array start on index[0] so the second variable in an array is going to be index 1.

What can you say about the size of an array?
The size of an array is the amount of elements in the array multiplied by the size of each element.

How can you initialize an array?
To initialize an array first you write the data type of tha array like int, double , and so on , then the name of the array followed with square brackets like [] , ,inside of the square brackets it goes the number of elements inside the array(fixed array) , and you can define it by = {3 ,3,4,6,7} like that.

How can you get the size of an array in bytes?
you can use the function 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]) is going to give you the number of elements of the array.

What happens when you try to write outside the bounds of the array?
When you write outside of the array , like an int arr[3] a fixed array of 3 elements and you do like arr[3] = 4; like assigning the value 4 to the forth element of our fixed array of three elements we will have undefined behavior because c++ is going to assign the number 4 to a portion of the memory that we don t know what we have stored there. like the eos attack that insert code through that vulnerability.

#54

PART I

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?
Index 1
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). For an array of length N, the array elements are numbered 0 through N-1! This is called the array’s range.

#55

PART II

1. How can you initialize an array?
Array elements are treated just like normal variables, and as such, they are not initialized when created. One way to initialize an array is to do it element by element. However, this is not very efficient, especially as the array gets larger, so C++ provides a more convenient way to initialize entire arrays 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 in bytes (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?
We can determine the length of a fixed array by dividing the size of the entire array by the size of an array element, as the size of the entire array is equal to the array’s length multiplied by the size of an element.
4. 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. When writing outside the boundaries of an array happens, you will get undefined behaviour; this could overwrite the value of another variable, or cause your program to crash.

#56

What is an array?
It is a data type. It permits to bundle data of the same type under a common name.

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

What can you say about the size of an array?
It always should be a positive integer and should be known at compile time.

Second part.
How can you initialize an array?
(a) Element by element (not very efficient)
e.g.

int array[2]
array[0]=1;
array[1]=2;

(b) List.

int array[2]={1,2}

(no need to specify the length of the array)

int array[2] {1,2}

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

With the command sizeof.

How can you use the function sizeof in order to calculate the number of elements in an array?
With the formula:

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

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

The value you are inserting outside the bound will be assigned to a memory slot (which one … it is not clear to me). This will cause undefined behavior.

#57

PART ONE:

1. What is an array?

An array is an aggregate data type, which allows us to access a lot of variables of the same type via one identifier.

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

You would use index 1 to access the 2nd variable in an array.

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

Array size = array length * element size.

PART TWO:

1. How can you initialize an array?

You can initialize an array via use of an initializer list.

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

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

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

Array length = sizeof(array) / sizeof(array[0]).

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

When you try to write outside the bounds of the array, you’ll get undefined behavior.

#58

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?
array[1]

## 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?

## we 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?
C++ does not do any checking to make sure that your indices are valid for the length of your array. So a value can be inserted into memory where an element would have not 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.

QUIZ

## Part1#include using namespace std; int main() { double temp[365] = { 0 }; // init all days to zero return 0; }

Part 2
#include

namespace animals
{
enum animals
{
chicken,
dog,
cat,
elephant,
duck,
snake,
max_animals
};
}

int main()
{
int no_of_legs[animals::max_animals] = {2,4,4,4,0,0};
std::cout << “An elephant should have " << no_of_legs[animals::elephant] << " legs\n”;

``````return 0;
``````

}

#59
1. It is a collection of data of the same type, that can be accessed in one variable
2. I should type index 1, because the first index of an array is number 0
3. The size of the array stays forever the same, once initialised
4. By putting square brackets after the name of the variable int numbers[];
5. By writing sizeof(array)
6. By dividing the overall size of array - sizeof(array) - by the size of one element - sizeof(array[0])
7. The software will not protest, but it will not write it into the array, and will write that data into the random memory address, which can cause some unexpected behaviour

#60

## Part 1

1. An array is a block of sequential memory that contains like values such as ints or strings.

2. Since arrays have zero based indexing the second element would reside at index 1. array[1]

3. A fixed array has its size set when it is declared and can not be changed, but C++ also contains dynamic arrays that can have their size changed.

## Part 2

1. There are a couple of different methods for initializing arrays in C++. One way is with an initializer list someArray[4] = { 1, 2, 3, 4 };
Another method C++ 11 introduced is this someArray[] { 1, 2, 3, 4, 5 }; Which helps us not have to specify the size as the compiler will infer from the initializer list.

2. You can get the byte size of an array by passing it to the sizeof() function if the array has not been passed into a function, because that would only render the size of a pointer in bytes.

3. You can get the element count of an array by using the sizeof() function to retrieve the total bytes of the array and dividing that by the results of the sizeof() function applied to a single array element.

``````int numElems = sizeof(someArray) / sizeof(someArray[0]);
``````
1. The program will write into memory at the address that would have contained that element which will result in undefined behavior.