Arrays in C++ - Reading Assignment


#81
  1. An aggregate of data type that lets us access many variables of the same type through a single identifier.

  2. [1]

  3. The amount of variables in an array.


  1. Initialize an array element by element or use an initializer list.

  2. sizeof

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

  4. Undefined behavior.


#82
  1. Arrays are a better way to store into one variable, multiple values. Each value will be indexed starting with 0
  2. The second value in an array will be automatically indexed at 1
  3. In C++ the size of an array refers to the number of bytes it is saved of in memory. So it depends on the number and the types of values it holds
  4. To initialize an Array, we need to use the square brackets like so [] and optionally put in between the number of elements we want to put inside the array.
  5. To get the size of an array, the keyword “sizeof” is require. It will return the number of bytes the array holds in memory.
  6. Because array elements are made of the same type, knowing the nb of bytes of one element and dividing the total size of the array with it will return the number of elements in the array. ex: sizeof(array) / sizeof(array[0]
  7. If a value is passed to an index of the array that doesn’t exists, that value will be added to an address in memory outside the array. That value can’t be accessed later and may cause unwanted behaviors nor bugs.

#83
  1. How can you initialize an array?
    A fixed array can be initialized by setting the value on an element by element basis or by using an initializer list that sets the value of each element in one statement.

  2. How can you get the size of an array in bytes?
    Using the sizeof operator on an array multiplies the array length by the element size and returns the total size of the array in bytes.

  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    You can calculate the number of elements in a fixed length array by using the sizeof operator on the array to determine the size of the array in bytes then using the sizeof operator on a single element of the array (best to use array element 0) to determine the size of a single element and then divide the size of the entire array (in bytes) by the size of a single element (in bytes).

  4. What happens when you try to write outside the bounds of the array?
    You will get undefined behavior because the compiler will allow you to write the value to a memory location that is outside the array.

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 temperature[365] = { };

  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.
    Write a main function that prints the number of legs an elephant has, using the enumerator.
    #include

using namespace std;

namespace animals
{
enum animalList
{
chicken, //0
dog, //1
cat, //2
elephant, //3
duck, //4
snake, //5
maxAnimals //6
} ;
}

using namespace animals;

void printFromAnimalList(int legs)
{
cout << “The elephant has " << legs << " legs” << endl;
}

int main()
{
int numberOfLegs[animals::maxAnimals];
numberOfLegs[animals::chicken] = 2;
numberOfLegs[animals::dog] = 4;
numberOfLegs[animals::cat] = 4;
numberOfLegs[animals::elephant] = 4;
numberOfLegs[animals::duck] = 2;
numberOfLegs[animals::snake] = 0;

int elephantLegs;
elephantLegs = numberOfLegs[animals::elephant];

printFromAnimalList(elephantLegs);

//    cout << "The elephant has " << numberOfLegs[animals::elephant] << " legs" << endl;

return 0;

}


#84
  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.

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

Index 1

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

The size of an array is the number of variables that can be allocated to the array.

  1. How can you initialize an array?

By listing out each value, element by element, or list the values out separated by commas which is called an initializer list.

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

By tying “size of (array).

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

By using the size of function to divide the size of elements by the array size.

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

C++ puts the value in memory where that element would have been if it exists.


#85

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?

[1]

What can you say about the size of an array?

There are two ways to declare an array. One being the dynamic array , where the size is not specified when it is declared and can vary in size. The other being the fixed array , where the size is specified by a compile-time constant on declaration and cannot be changed.

How can you initialize an array?

Element by element, or via an initializer list.

arrayname[]={element 1, element 2,…,element x}

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

sizeof() function

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?

The results are unpredictable and you write in an unallocated memory space.


#86
  1. An array is a structure that can hold multiple things inside of it – like ints and strings. For example: int array[10] = { }.

  2. Indexing is zero based so for four places in an array it would start with 0, then 1, 2, 3. To get the second variable in an array it would be array[1].

  3. A fixed array will have a set number of variables. Something like int array[5]; would have five numbers inside of it.


  1. An array is initialized by something like int array[5]; then going through array[0] to array[4]. It can also be done in this way: int array[5] = {1, 2, 3, 4, 5}. It can also be left to zero by doing: int array[5] ] { }.

  2. The sizeof() operator gives the size of an array in bytes.

  3. To get the number of elements the following would need to be done: int array[3] = {1, 2, 3}; sizeof(array) / sizeof(array[0]).

  4. Writing outside of the bounds of the array will cause undefined behavior in the program. If something is assigned to the fourth spot of an array when it only has three spots the number will be put into the spot regardless. This can cause issues and random behavior within the program, cause it to crash, etc.


#87
  1. What is an array?
    is a named list of similar type variables

  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?
    is fixed and must be known at the time the program is compiled

  4. How can you initialize an array?
    int array[] {var1, var2, var3, var4, …};

  5. How can you get the size of an array in bytes?
    sizeOf(array);

  6. How can you use the function sizeof in order to calculate the number of elements in an array?
    dividing the size of the array by the size of the elements inside it

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


#88

Part 1

1. What is an array?

An array is a data structure that can hold multiple types of the same type of data. Each element of the arrays is like 1 variable of the type defined when the array was created. Each element is indexed from [0] upwards and can be accessed using its index position.

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

Index position [1]

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

In a fixed array, the size of the array has to be fixed at compile time. Either with a direct numerical size or by reference to a constant. A dynamic array can have a size that is calculated at run time.

Part 2

1. How can you initialize an array?

To initialise the array elements with 0: double yearTemperatures[365] = {}; Or we can use a list: int legs[animals::totalAnimals] = {2, 4, 4, 4, 2, 0};

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

The "sizeof(array)" function can get the size in bytes of the array. Note this is not the number of elements.

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

The number of elements can be found by: sizeof(array) / sizeof(array[0])

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

This will result in undefined behavior, the array will access a memory location that has not been reserved for it and may be in use by another part of the program, and in turn crash the program.

Quiz - Part 1 & 2

#include

using namespace std;

namespace animals {

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

}

void printLegs (int x){
cout << “The animal " << " has " << x << " legs.” << endl;
}

int main ()
{
//Part 1
cout << "Enter a day of the year by number: ";
int dayOfYear;
double yearTemperatures[365] = {};
cin >> dayOfYear;
cout << "The temperature on your chosen day was: " << yearTemperatures[dayOfYear] << endl;

//Part 2
int legs[animals::totalAnimals] = {2, 4, 4, 4, 2, 0};
printLegs(legs[animals::elephant]);

return 0;

}


#89

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- You use the index number [1]
3- Array lengths can be either static or dynamic, and it must be specified when declaring it.

Part 2
1- You an initialize an array by declaring the number of elements it contains, or by explicitly defining what those elements are. You can also initialize some elements while specifying the length, and the rest will be given a value of 0.
2- sizeof(array) gives the size of the array in bytes.
3- sizeof(array)/sizeof(first_array_element) - We use the array element of index 0 because that one is guaranteed to exist.
4- C++ lets you compile an array that is out of bounds. The result is undefined behavior.


#90
  1. An array is an indexed list of objects of a given type
  2. arrays start at 0, so to access the second element you would have to do arrya[1]
  3. The size of an array is the array length multiplied by the element size you can specifiy te length.

Part 2

  1. int array[4]= {4}
    int array[]={0,4,5,7,8,}
    2.sizeof(array)/ sizeof(array[0])
    3.sizeof(arr)/sizeof(arr[0]);
  2. Writing outside of the array gives you elements that you would not expect
    int Prime[4]{0,3,4,5,} it would have to be 4 total elements starting at 0-4, prime[0,1,2,3]
    trying to call prime 5 would result in something unexpected.

#91
  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?
    There are two ways to declare an array. One being the dynamic array , where the size is not specified when it is declared and can vary in size. The other being the fixed array , where the size is specified by a compile-time constant on declaration and cannot be changed.

  4. How can you initialize an array?
    Element by element or with initializer list

  5. How can you get the size of an array in bytes?
    With sizeof(array)

  6. 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])

  7. What happens when you try to write outside the bounds of the array?
    It will continue to write in a memory location of where the element would have been had it existed. When this happens, this could overwrite the value of another variable, which leads to unexpected behaviours and results.


#92
  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 (= length) of an array is either fixed or dynamic… for fixed - Size of the array is defined at initialization (array length * data type size)
  4. One way to initialize an array is to do it element by element/ can also initialize entire arrays via use of an initializer list
  5. The sizeof operator can be used on arrays, and it will return the total size of the array (array length multiplied by element size).
  6. sizeof/1 element provided all elements same type
  7. you will get undefined behavior – For example, this could overwrite the value of another variable, or cause your program to crash

#93

Arrays part 1

  1. An array is an aggregate data type that allows us to access many variables of the same type through the same identifier.
    2.([1])
  2. The size of an array is the length multiplied by the element size. You can specify the arraly length during the declaration between brackets or the compliler will do it for you.

Arrays part 2

  1. One way to initialize an array is to do it element by element. For a more convenient way to initialize entire arrays, in c++ we use an initializer list.
    2.sizeof(erray);
    3.int array[3]={7};//
    4.you get an undefned behavior or the program will crash.

#94

1. What is an array?
An array is a stucture that stores many variables of the same type inside a single data type

2. Which index would you use to access the 2nd variable in an array?
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.

4. How can you initialize an array?
by individually defining the values or create an initializer list which is recommended

5. How can you get the size of an array in bytes?
the number of total integers multiplied by 4 because on a 32bit program every integer represents 4 bytes and every char represents 2 bytes.

6. How can you use the function sizeof in order to calculate the number of elements in an array?
sizeof (array) / sizeof (array [0])

7. What happens when you try to write outside the bounds of the array?
it is assigned to the next byte in memory which runs as an undefined behavior that can cause errors or the entire program crashing.


#95
  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. e.g. TestScores [50% 63% 79% 54%]
    To access 63% score we extract it from the array as bellow
    TestScores [1]

  3. Size of an array is the number of variables or elements that can be placed in one array

Part II:

  1. How can you initialize an array?
    we can use an initializer list.
    e.g
    int prime[5] = { 8, 3, 9, 2, 5};
    or you can “not” specify the length of the ‘fixed’ array and let C++ figure it out. i.e.
    int prime [ ] = {0, 1, 2 ,3 ,4, 5}; // implies that you don’t have to update the array length.

  2. Size of an array in bytes.
    we can use sizeof(array);

  3. The function sizeof can be used such that the division between the size of the entire array and the size of the elements within an array gives the length of a fixed array. i.e.

sizeof(array) / sizeof(array[0])
NB
We typically use array element 0 for the array element, since it’s the only element guaranteed to exist no matter what the array length is.

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 the array is declared in.

  1. What happens when you try to write outside the bounds of the array?
    the program might crash or one of the elements could be replaced. it’s better to keep track of what’s happening and input using the right parameters.

#96

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

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 the array is the amount of elements in an array.

Part 2

How can you initialise an array?

  • int prime[5];
  • OR int prime[5] = {2,3,5,7,11};
  • OR int prime[5] = {};
  • OR int prime[] = {2,3,5,7,11};

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?

  • C++ does not do any checking to make sure that your indices are valid for the length of your array.
  • The new value will be inserted in the memory where the outside of bounds element would have been had it existed.
  • This could overwrite the value of another variable, or cause your program to crash

#97
  1. A way to store a group of one data type under one variable name .

  2. Use the index number 1

  3. It can be fixed or dynamic

  4. Since an array like any other variable is not automatically initiated, it must be initiated either by doing it manually or using an initializer list.

  5. Use sizeof operator

  6. Use sizeof of array divided by sizeof one element of array

  7. You get an undefined behaviour


#98

Part 1:

  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

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

    Fixed arrays have memory allocated, so the length of a fixed array must be known at compile time. This introduces two limitations:

    1. Fixed arrays cannot have a length based on either user input or some other value calculated at runtime.
    2. Fixed arrays have a fixed length that can not be changed.

Part 2:

  1. How can you initialize an array?

    Two ways to initialize an array is element by element like a normal variable or via the use of an initializer list

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

    use the sizeof operator

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

    Use sizeof the entire array divided by the sizeof an array element.

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


#99

1. What is an array?
An array is a type of data where we can store, in a single place, multiple values. Each of these values can be accessed – and manipulated – by their position in the array. This position is called the index or subscript. The first position is index 0.

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

3. What can you say about the size of an array?
In C++ the size (length) of an array is fixed: it has to be determined and set BEFORE compiling, and cannot be changed. Therefore, the length cannot be determined by eventual user input. C++ offers a solution to this with the use of “dynamic arrays,” which will be discussed later in the course.

PART 2

  1. How can you initialize an array?
    There are 2 ways:
  • Element by element…
    int exampleA[4];
    exampleA[0] = 10;
    exampleA[1] = 12;
    exampleA[2] = 14;
    exampleA[3] = 16;

  • Using an initializer list…
    exampleB[4] = { 10, 12, 14, 16 };

  1. How can you get the size of an array in bytes?
    By using sizeof(array)

  2. How can you use the function sizeof in order to calculate the number of elements in an array?
    By dividing he size of the entire array by the size of an array element. Namely:
    sizeof(array) / sizeof(array[0]

  3. What happens when you try to write outside the bounds of the array?
    The program will compile, but the value will be inserted into memory and the result will be an undefined behavior which could either provide an incorrect /unexpected result, or overwrite the value of another variable, or cause the program to crash.


#100

Part 1

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]

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

  • Fixed arrays cannot have a length based on either user input or some other value calculated at runtime.
  • Fixed arrays have a fixed length that can not be changed.

In many cases, these limitations are problematic. Fortunately, C++ supports a second kind of array known as a dynamic array . The length of a dynamic array can be set at runtime, and their length can be changed.

Part 2

  1. How can you initialize an array?

There are multiple ways to initialize an array with slight variations in syntax. But i usually involves using curly braces { }.

int array[5] = { 7, 4, 5 }; // only initializes first 3 elements

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

by using the sizeof(array);

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

sizeof(array)/sizeof(array elemnt)

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

it will produce undefined behavior and should be avoided.