Arrays in C++ - Reading Assignment

Part I

  1. An array is a list of values with the same type
  2. Index 1
  3. The size of an array is fixed at array length multiplied by the element size

Part II

  • int prime[5] = { 2, 3, 5, 7, 11 };
  • int array[5] = { };
  1. sizeof(array);
  2. sizeof(array) / sizeof(array[0])
  3. undefined behavior

1- An array is a way to aggregate several variable of the same type in the same identifier.
2- index [1]
3- The size of an array is the amount of disk it occupies in bytes.

1- It is possible to initialize an array element by element, or via a list
2- It can be calculated through multiplying the element size times the length of the array
3- You can use sizeof divided by the individual size of one element
4- By doing this, you get an undefined behavior, because the value will be inserted into the memory where the element should have existed.

  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? 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 declaring a fixed array, the length of the array (between the square brackets) must be a compile-time constant. This is because the length of a fixed array must be known at compile time.
    Because fixed arrays have memory allocated at compile time, that introduces two limitations:

  • 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. However, dynamic arrays are a little more complicated to instantiate, so we’ll cover them later in the chapter.


  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 a pain, especially as the array gets larger. Fortunately, 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 (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?

  4. What happens when you try to write outside the bounds of the array?
    Determining the length of a fixed array prior to C++17. The sizeof operator can be used on arrays, and it will return the total size of the array (array length multiplied by element size).
    Determining the length of a fixed array as of C++17 Starting with C++17, a better option is to use the std::size() function, which is defined in the header. std::size() has the advantage of being easier to remember, it will work with other kinds of objects (such as std::array and std::vector), and it will cause a compiler error if you try to use it on a fixed array that has been passed to a function!

  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- array [1];
  3. What can you say about the size of an array?
    Size of an array = array length (number of elements) * element size.

Part 2

  1. How can you initialize an array?
    -by assigning element by element :
    ;int example [3]
    example [0]=1;
    example [1]= 2;
    example [2]=3;
    or by using an initializer list:
    int example[3]= {1,2,3};
  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?
    by dividing the size of the entire array by the size of the array element.
    sizeof (array)/sizeof (array[0])
  4. What happens when you try to write outside the bounds of the array?
    UndefinedBehavior.

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. The 2nd variable is 1.
  3. The length of an array is dynamic or fixed.

Part 2

  1. There are many ways… you can initialize an array by declaring what type of array, name of array, using [] to define it’s variable(s) length(scope) and by using the initializer list to define it’s variables values.
  2. You can get the size of an array in bytes, by doing sizeof(array).
  3. array size = sizeof(array) / sizeof(array[0]); array size = array length * element size.
  4. int prime[5];

// 5 Elements in array.

prime[5] = 13;

// Asking for 6th element because we start at 0.

Will cause undefined behavior because 13 will be put into memory where 6th element would have been.

#letsgetthiscrypto

What is an array?

A data type wich lets us access many values under one identifier

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

prime[1]

What can you say about the size of an array?

Fixed arrays length cant be changed

Dynamic can be changed in runtime

How can you initialize an array?

You do it element by element , or by making a list

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

Sizeof command

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

sizeof(array) divided by sizeof(array[0]) = number of elements

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

Undefined behaviour

Arrays in C++ - Reading Assignment

1. What is an array?

  • An array is an aggregation of many variables in a single identifier

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

  • array [3];

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

  • fixed arrays have a fixed array length must be declared at compile time (not runtime)
  • there are also dynamic array which allows to declare its length at runtime (more flexible arrays)
  • the last Index of an array is N (for length) - 1

4. How can you initialize an array?

  • initializing with lists
int array [] = {1, 1, 2, 3, 5, 8};
  • or element by element

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

sizeof(array[i]);

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

int array[] = {1, 1, 2, ,3, 5, 8};
elementsNums = sizeof(array)/sizeof(array[0]);

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

  • error

Q: What is an array?

A: An array is an aggregate data type that lets us access many variables of the same type through a single identifier.

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

A: The second element has index 1. Ie…“array[1]”.

Q: What can you say about the size of an array?

A: The size of an array can be worked with in many ways in C++.
A fixed array (also called a fixed length array or fixed size array) is an array where the length is known at compile time.
Each of the variables in an array is called an element. Elements do not have their own unique names. Instead, to access individual elements of an array, we use the array name, along with the subscript operator ([]), and a parameter called a subscript (or index) that tells the compiler which element we want. This process is called subscripting or indexing the array. Array subscripts must always be an integral type. This includes char, short, int, long, long long, etc… and strangely enough, bool (where false gives an index of 0 and true gives an index of 1). An array subscript can be a literal value, a variable (constant or non-constant), or an expression that evaluates to an integral type.Fixed array declarations the length of the array (between the square brackets) must be a compile-time constant. This is because the length of a fixed array must be known at compile time.Because fixed arrays have memory allocated at compile time, that introduces two limitations:
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. However, dynamic arrays are a little more complicated to instantiate.

/////////////////////////////////////////////////////

Q: How can you initialize an array?

A: C++ provides a more convenient way to initialize entire arrays via use of an initializer list.

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

A: when we’re talking about the number of elements in the array, and “size” when we’re referring to how large something is in bytes.

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

A: The sizeof operator can be used on arrays, and it will return the total size of the array

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

A: you will get undefined behavior – For example, this could overwrite the value of another variable, or cause your program to crash.

  1. What is an array?
    A sort of container which is able to keep an indexed group of varaibles.

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

  3. What can you say about the size of an array?
    There are arrays with a fixed length and dynamic.

Think about the following questions while reading:

  1. How can you initialize an array?
    int myarray[5] ={}

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

  3. 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. sizeof(array) / sizeof(array[0]) ;

  4. What happens when you try to write outside the bounds of the array?
    Your program could crash, undefined error.

  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?

array[1]

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

It can be fixed or dynamic.

Part II

  1. How can you initialize an array?
int prime[5] {2, 3, 5, 7, 11];

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

int prime[5] { };

const int arrayLength = 5;
int array[arrayLength];

enum ArrayElements
{
    MAX_ARRAY_LENGTH = 5
};
int array[MAX_ARRAY_LENGTH];
  1. How can you get the size of an array in bytes?
std::cout << sizeof(prime);
  1. How can you use the function sizeof in order to calculate the number of elements in an array?
sizeof(array) / sizeof(array[0])
  1. What happens when you try to write outside the bounds of the array?

The value outside the bounds will be inserted into memory where this element would have been had it 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.

1. What is an array?

An ordered collection of objects/data of a similar type.

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

Arrays are “zero based” so [1].

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

Size of array is the number of elements in the array. It is calculated by Element Size multiplied by number of elements.

  1. How can you initialize an array?

You can either assign a value to each element of the array or you can use an initializer list.

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

Size of each element in the array multiplied by the number of elements in the array

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

Array size divided by element size

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

You get unpredictable results as you try to write to memory that has not been allocated.

*1. What is an array?
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 an array is determined by the number of elements. For an array of length N, the elements from element 0 to element N -1 is termed the arrays range.

  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 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?
    we can determine the length of a fixed array by dividing the size of the entire array by the size of an array element, Put more compactly: array size = array length * element size.

  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, We will get undefined behavior and results.

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

  1. Which index would you use to access the 2nd variable in an array?
    array[1]
  2. What can you say about the size of an array?
    Size of the array is defined at initialization (array length * data type size) and can not be changed (for static arrays).
  3. How can you initialize an array?
    Via initializer list when the array is defined, or element whise
  4. How can you get the size of an array in bytes?
    sizeof(array)
  5. How can you use the function sizeof in order to calculate the number of elements in an array?
    sizeof(array)/sizeof(array elemnt)
  6. What happens when you try to write outside the bounds of the array?
    the memory location outside of the array scope will be written resulting in a unpredictable result.

part 1
1. What is an array?

An ordered list of elements of a given type.

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

In Pascal it depends upon the defined range. In C, it would be [N - 1] which would be 1. Off by one errors are common because ancient mathematicians didn’t conceive of zero as a number.

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

Static array sizes are defined at design time, dynamic arrays are defined at runtime. In Pascal, you can use the SetLength() procedure after declaring the array type.

Part 2
How can you initialize an array?

// int array[] { elementone, elementtwo, etc }

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

Use sizeof() function. Length of array is the size of array divided by the size of the elements. Later versions of C++ allow you to use size() which will do this for you.

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?

In Delphi Pascal you get a range check error. In C you might end up with undefined behavior that might grab random garbage.

Part 1…

  1. What is an array?
    An array is a collection of variables of the same type that can be accessed via a single identifier.

  2. Which index would you use to access the 2nd variable in an array?
    Arrays are 0 based so you would access the 2nd variable by using index[1].

  3. What can you say about the size of an array?
    The size, or length, of an array represents the number of elements it has.

Part 2…

  1. How can you initialize an array?
    Arrays are created in a similar manner to normal variables in that their data type must be specified during the declaration. Where they differ is that you must declare an array by appending square brackets with the array size specified within for a fixed length array. The array is then initialised by providing an initialiser list So…
    int array[5] = { 2, 4, 6, 8, 10 };
    …will be a fixed length array of 5 integer elements with indexes from 0-4. If less values are provided than the number of array elements, the remaining elements will be zero initialised.

  2. How can you get the size of an array in bytes?
    You must use the sizeof operator directly on the array to return its size in bytes, which is the array length multiplied by the element size.

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

  4. What happens when you try to write outside the bounds of the array?
    C++ does not validate the array index provided so the value will be written into the memory location that would have been allocated if the out-of-bound index existed. This would overwrite whatever value resides at that address and could have undefined behaviours or even crash the program.

Part1:

  1. An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
  2. array[1], index is 1
  3. The size of an array is either fixed ( called the array length and declared between square brackets []) or dynamic. You can declare it explicit or let initializer list set length of the array.

Part 2:

  1. int prime[5] = { 2, 3, 5, 7, 11 };
  2. int array[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
    std::cout << sizeof(array) << ‘\n’;
  3. int array[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
    std::cout << sizeof(array)/sizeof(array[0]) << ‘\n’;
  4. If there are more initializers in the list than the array can hold, the compiler will generate an error.
  1. An array is a place to store many variables at the same time so you don`t have to write out every single one.

  2. I would use [1] since the 1st is variable [0].

  3. It is fixed at array length multiplied by the element size

2.1) With a variable name and brackets [] to call the array.

2.2) If the array is initialized then sizeof(array).

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

2.4) Undefined behaviour or even crash of the program…

1. What is an array?
An array is an aggregate datatype that lets us access many variables of the same type through a single identifier.

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

3. What can you say about the size of an array?
In the case of a fixed array, it cannot be changed. The size of an array is:
array length * data type size

4. How can you initialize an array?
You can initialize an array element by element.
Or, you can use an initializer list.
Or, you can initialize to zero using empty brackets.

5. How can you get the size of an array in bytes?
Use the sizeof operator to get the total size of the array (array length * element size).

6. How can you use the function sizeof 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 of the bounds of an array?
This will cause undefined behavior possibly resulting in the program crashing or code vulnerability.

PART 1

  1. What is an array?
    An array is a list that can store different values that can be accessed when needed. Think of it as a bookshelf with different books.

  2. Which index would you use to access the 2nd variable in an array?
    Arrays always start with index 0., so array[1] will access the 2nd variable of the array.

  3. What can you say about the size of an array?
    The size of the array is defined by the length of the array. the size can be dynamic or fixed

PART 2

  1. How can you initialize an array?
    int array[3];
    array[0] = 13; // example 1

    int array[3] = {10, 5, 16}; // example 2

int array[] = {10, 5, 16}; // example 3

  1. How can you get the size of an array in bytes?
    you can get the array size in bytes by multiplying the array length with the element size.

  2. How can you use the function sizeof to calculate the number of elements in an array?
    By using sizeof(array) divided by sizeof(‘the type of the element’)
    Syntax: sizeof(array)/sizeof(int);

  3. What happens when you try to write outside the bounds of the array?
    You get undefined behavior. it is not recommended to write outside the bounds of the array. the program may crash.