Arrays in C++ - Reading Assignment

Part1

1. What is an array?
Array is a collection of variable of same type. It allows to store same data type values at its indexes.

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?
Array size is the number of indexes it has or how many varibles it can store. For fix size array it is placed between ‘[’ and ‘]’.

Part2

1. How can you initialize an array?
By assigning element value or via initializer list

2. How can you get the size of an array in bytes?
With sizeof(array). This gives us
array size = array length * element size

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

4. What happens when you try to write outside the bounds of the array?
We won’t get IndexOutOfBound Exception like Java. We will get undefined behavior as the out of bounds value may overwrite the existing value in that memory location.

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?
The size of the array represents the number of variables that can be allocated to the array.

1. How can you initialize an array?
You can initialize an array element by element, by assigning a value to each array index on by one, but this can be difficult when you have a longer list. A more convenient way is to initialize the whole array at once using an initializer list. For example:
int prime[5] = { 2, 3, 5, 7, 11 };

2. How can you get the size of an array in bytes?
We can get the total size of the array in bytes using the sizeof operator.

3. How can you use the function sizeof in order to calculate the number of elements in an array?
We can first use sizeof to get the total size of the array, and then divide by element size to get the number of elements in the array.

4. What happens when you try to write outside the bounds of the array?
If you try to write outside the bounds of an array it will result in undefined behavior. You could, for example, overwrite a value in a memory address assigned to another variable, and this can cause your program to crash or behave incorrectly.

PART ONE

  1. An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
  2. example[1],arrays always count starting from 0
    3.size = length

PART TWO

  1. a)element by element
    b)initializer list
    2.turn the total size of the array (array length multiplied by element size, .sizeof(array)
    3.array length = sizeof(array) / sizeof(array[0])
    4.this could overwrite the value of another variable, or cause your program to crash.
  1. An array is a collection of many variables accessible from a single identifier.

  2. You would use the index [1].

  3. The size of an array must be determined at compile time. They cannot be changed either. However, if you would like to changed the size of an array and have it be determined at runtime, you need to use a dynamic array.

  4. There are a few ways to initialize an array. You could list out each value, element by element, or list the values out separated by commas which is called an initializer list.

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

  6. You use the sizeof function to divide the size of elements by the array size.

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

Yes, I am not sure about enum too

PART 1

1. What is an array? An array is a data structure 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? Use index 1 to access the second variable in an array.

3. What can you say about the size of an array? Each of the variables in an array is called an element and the size of an array is determined by how many elements are in that array - this can be fixed or dynamic.

PART 2

1. How can you initialize an array? The more common way of initialising arrays is to use an initialiser list, where you specify the values held by each element of an array surrounded by curly braces. Elements that are left blank are then initialised to some default value.

2. How can you get the size of an array in bytes? By using the sizeof operator on an array.

3. How can you use the function sizeof in order to calculate the number of elements in an array? The number of elements in an array, also known as array length can be calculating using the following: array length = sizeof(array) / sizeof(array[0])

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

1 Like

What is an array?
- An array is a way to store multiple variables of the sdame type in a single object.
Which index would you use to access the 2nd variable in an array?
- As in javaScript we start counting from [0] so the 2nd variable would be at index [1].
What can you say about the size of an array?
- An array length needs to be defined when the array comes into use, it cannot be created from user input.

How can you initialize an array?
- You can set the intial values in the array putting “= {intial value, intial value,…}”
How can you get the size of an array in bytes?
- you type sizeof(array) and it will tell you the size of the array in bytes.
How can you use the function sizeof in order to calculate the number of elements in an array?
- we divide the sizeof(array) by the size an an array element. sizeof(array) / sizeof(array[0]). It only works on fixed size arrays.
What happens when you try to write outside the bounds of the array?
- C++ will not check the length of your array and will place the value into a random memory spot that it thinks is in your array.

Part I
1. What is an array?
It 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?
array[1]
3. What can you say about the size of an array?
The size is fixed since its creation and for a length of N array, it goes from 0 to N-1, this is the array’s range.

Part II
1. How can you initialize an array?
With a full or empty initialization list.
2. How can you get the size of an array in bytes?
With the sizeof(array) operator.
3. How can you use the function sizeof in order to calculate the number of elements in an array?
With sizeof(array)/sizeof(array[3])
4. What happens when you try to write outside the bounds of the array?
We will get undefined behaviour as known this could overwrite the value of another variable or cause your program to crash.
Quiz

  1. double highTemp [365] = {0.0};

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?
If we know the number of items we need, we can use a fixed size array, otherwise we can leave it empty [] to indicate that it is a variable length array.

I. How can you initialize an array?

When we know the size of the array before compile time we use:

int prime[2]; // hold the first 5 prime numbers
prime[0] = 2;
prime[1] = 3;

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

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

// note the lack of the equals sign in this syntax
int prime[5] { 2, 3, 5, 7, 11 };

Or we can allow the compiler to calculate the length for us so that we don’t have to update the size when adding new elements:
int array[] = { 0, 1, 2, 3, 4 };

II. How can you get the size of an array in bytes?
sizeof(array)
for the pointer we use sizeof(array[i]);
III. How can you use the function sizeof in order to calculate the number of elements in an array?
sizeof(array) / sizeof(array[0])
IV. What happens when you try to write outside the bounds of the array?
You will get undefined behaviour as C++ does not check if the indices are valid for the length of the array.

What is an array?
An array is an aggregate data type that allows access to its elements through a single identifier.

Which index would you use to access the 2nd variable in an array?
Because first element is 0, the 2nd element would be and index of 1.

What can you say about the size of an array?
Size of array needs to be a constant at compile time.


How can you initialize an array?
You can do it element by element or using an initializer list { }

How can you get the size of an array in bytes?
You can apply the sizeof function on an array to get the size in bytes.

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?
The results are unpredictable and you write in an unallocated memory space.

Arrays in C++



    1. Arrays are treathed like variables, storing multiple variables in one variable.

    2. Second element of an array is array[1]

    3. You can determine the size by the funtion sizeof(nameArray);




    1. int array[3] // set number of elements in brackets.
      int array[] = {0,1,2,3}; //lenght depends on declaration in elements

    2. by the sizeof operator, you can calculate te total size in bytes.

    3. By dividing the total sizeof an array by the size of 1 element.

    4. it produces undefined behaviour. It can overwrite a value of another variable or even cause a crash.
  1. An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
  2. We use index [1] to access 2nd variable in an array.
  3. Fixed size array is an array where the length is known at compile time.
  1. An array could be initialized as an initializer list.
  2. The sizeof operator can be used on arrays to return the total size of the array in bytes.
  3. Calculate the number of elements in an array:
    array length = sizeof(array) / sizeof(array[0])
  4. When you write outside the bounds of the array you get undefined behavior.

1. What is an array?
Array is a collection of values (or objects) with same data type that can be accesed with index.
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 is static if it is initialized at compile time.

1.How can you initialize an array?
You set it with a list of values.
2.How can you get the size of an array in bytes?
You use sizeof() operator.
3.How can you use the function sizeof in order to calculate the number of elements in an array?
You can use sizeof() an array and divide it by the sizeof() an element.
4.What happens when you try to write outside the bounds of the array?
You can however the program can start behaveing in undefined bahavior i.e. “buggy”.

Part 1:

  1. What is an array?

An array is an object that stores a series of values of the same type.

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

The 2nd variable of an array has index 1.

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

The size of an array is fixed and must be known at the time the program is compiled.

Part 2:

  1. How can you initialize an array?

We can initialize an array using curly brackets. E.g. int myArray[2] = {1, 2}; or int myArray[2] = {}; where in the second case the elements of the array are initialized to zero.

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

The sizeof operator multiplies the size of an element of the array by its length to give the size of the array in bytes.

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

As a result of the definition of sizeof the number of elements in an array in sizeof(array)/sizeof(array[0]).

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

If we write outside the bounds of the array we will write something in place of what is in the memory addressed where the non-existing index would be. This will cause undefined behaviour.

  1. An array is an data type with we can access many variables of the single through a single identifier.

  2. Index 1 should be used to access the 2nd variable in an array.

  3. The size of an array is fixed and should be known when the program compiles.

  4. You can initialize an array through an initializer list { }

  5. You can get the size of an array in bytes with sizeof() operator.

  6. You can use the function sizeof in order to calculate the number of elements in an array by dividing the size of the entire array by the size of an array element.

  7. Writing outside the bounds of an array will cause undefined behaviour.

Part 1

  1. An array is an aggregate data structure that lets us store data of the same type in one identifier.
  2. Index 1 is used to access the second varibale in an array.
  3. An array has a fixed size, though it may be dynamic.

Part 2

  1. An array may be initialized via an initializer list, using {} after the array.
  2. The total size (in bytes), in term of the sizeOf function, is the array length multiplied by element size.
  3. To find the length of an array, in terms of the number of elements in the array, is obtained via sizeOf(array)/array[0]
  4. Writing outsize the bounds of an array will result in undefined behaviour.

Quiz

  1. double temperature[365] {0.0} // Initialize the array with 0.0's
  2. namespace Animals
    {
        enum Animals
        {
            CHICKEN, // 0
            DOG, //1
            CAT, // 2
            ELEPHANT, // 3
            DUCK, // 4
            SNAKE, // 5
            POPULATION // 6
        };
    }
    
    int main()
    {
        int legs[Animals::POPULATION] { 2, 4, 4, 4, 2, 0 };
        std::cout << "The Elephant has " << animals[Animals::ELEPHANT] << " legs" << std::endl;
    
        return 0;
    }
    
    
1 Like

Part 1

  1. An array is an aggregate data type that allows us to access a collection of values of the same data type through a single identifier.
  2. The 2nd variable in an array is accessed with index 1.
  3. For a fixed array, its length is known at the compile-time and hence can’t be changed. For the second type of supported arrays in C++, namely dynamic arrays, its length can be set and changed at runtime.

Part 2

  1. (Fixed) arrays can be initialized after the declaration by simply using assignments one by one:
int years[3];
years[0] = 2009;
years[1] = 2011;
years[2] = 2015;

However, a more elegant way is using initializer lists:

int years[3] = {2009, 2011, 2015};
  1. The size of an array in bytes can be determined with the sizeof operator.
  2. The number of elements in an array can be determined by dividing the total length of an array in bytes by the size of a single element in bytes, i.e.: sizeof(array) / sizeof(array[0])
  3. If you write outside the bounds of an array, you get undefined behaviour – the values could probably overwrite other variable values or even return addresses for functions.

1. What is an array?
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?
C++ has 2 types of arrays. Fixed length were the array is declared with a fixed length that cannot be changed, and dynamic were the length of the array can be set at run time.

1. How can you initialize an array?
Element by element, or via an initializer list.

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

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

4. What happens when you try to write outside the bounds of the array?
undefined behavior – Note: C++ won’t stop you from doing this…

  1. An array consist of a number of values of same type

  2. 3

  3. The size of an array should be defined at the time of the array declaration

  4. An array can be initialized using element by element or by initializer list

  5. The size of an array can be found out by using sizeof() operator

  6. By dividing the sizeof(array) by sizeof(array[0]) which is the size of an initial element in array

  7. The program may crash or it may run but it can overwrite some random address value therefore causing an undesired behavior

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?
A fixed array (also called a fixed length array or fixed size array) is an array where the length is known at compile time. he length of a dynamic array can be set at runtime, and their length can be changed.

How can you initialize an array?
arrayname[]={element 1, element 2,…,element x}
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(arrayname) / sizeof(arrayname[0])
What happens when you try to write outside the bounds of the array?
get undefined behavior