Arrays in C++ - Reading Assignment

This is because the string object stores pointer to a string, not the string itself so they are the same size. To get the actual size of the string in C++, you can use length:

int main()
{
    string array[3]{"cat", "čšž", "this is a wery long string"};

    cout << array[0].length() << endl;
    cout << array[1].length() << endl;
    cout << array[2].length() << endl;
    return 0;
}
3
6
26
2 Likes

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?
index 1

What can you say about the size of an array?
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).

How can you initialize an array?
To initialize a C++ Array, use square brackets after variable name to denote that it is an array, and assign the variable with list of elements.
{
int arr1[ ] = {7, 3, 8, 7, 2};
}

How can you get the size of an array in bytes?
by using the sizeof operator,

How can you use the function sizeof in order to calculate the number of elements in an array?
divide the total size of the array by the size of the array element.
int arr[5];
int len = sizeof(arr) / sizeof(arr[0]);
return 5;

What happens when you try to write outside the bounds of the array?
there is no guarantee to get the correct result. Result may unpredictable and it will start causing many problems that will be hard to find.

1 Like

You are just returning 5 here instead of len :stuck_out_tongue: but the answer is correct! :slight_smile:

1 Like
  1. an aggregate data type that lets us access many variables of the same type through a single identifier
  2. 1
  3. a fixed array can not change its size after runtime, a dynamic array is able to change size

  1. using an initializer list
  2. std::cout << sizeof(array)
  3. sizeof(array) / sizeof(array[0])
  4. a bunch of errors
1 Like

Section 1

  1. An array let’s us access many variables of the same type through a single identifier.
  2. The second variable will be [1]
  3. There are two types of arrays, where
    a. Fixed array – An array where the length is known at compile time and
    b. Dynamic array – Length can be set at runtime, but the length can be changed later.

Section 2

  1. Make use of an initializer list
  2. sizeof(array)
  3. Divide the value of (sizeof(array) by the length of the array
  4. Undefined behaviour – could overwrite the value of another variable and cause the program to crash
1 Like
  1. What is an array?
    It’s a data type that can aggregate multiple values of the same type.

  2. Which index would you use to access the 2nd variable in an array?
    Indexes start from zero, so to access 2nd value you use index 1. array[1];

  3. What can you say about the size of an array?
    It´s the length multiplied by the elements size. The length of the array can be defined by the user using square brackets after the array name or the compiler can set it once the array is initialized.

  4. How can you initialize an array?
    By defining the type of the values, array name, length and initial values

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

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

  7. What happens when you try to write outside the bounds of the array?
    Undefined behavior. The value can overwrite another value in memory or the program can crash.

1 Like

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?
array[1]
3. What can you say about the size of an array?
There are two types of array, fixed array and dynamic array. The differences are fixed arrays that cannot have a length based on either user input or some other value calculated at runtime and Fixed arrays have a fixed length that can not be changed.
4) How can you initialize an array?
int array [8];
//8 elements in the array
5) How can you get the size of an array in bytes?
size of array[]
6) How can you use the function sizeof in order to calculate the number of elements in an array?
sizeof(yourArray) / sizeof(datatype used for this array)
7) What happens when you try to write outside the bounds of the array?
undefined behavoir or even a crash of your program

1 Like

Part I

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

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

  3. In an array variable declaration the compiler is told the size of the array to allocate. There are fixed size arrays where the length is known at compile time, and dynamic size arrays where their size can be set at runtime, and their length can be changed.

Part II

  1. An array can be initialized via use of an initializer list.

  2. The sizeof operator can be used to return the total size of the array (array length multiplied by element size).

  3. You can use the function sizeof to calculate the number of elements in an array by dividing the total size of an array by the array length.

  4. If you try to write outside the bounds of the array you will get undefined behavior like overwriting the value of another variable, or causing your program to crash.

1 Like

If you would divide the size of the array by length, you would get the element size (if you mean length as the number of elements) :stuck_out_tongue:

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?

index [1]

What can you say about the size of an array?

The size of an array is equivalent to the last index position plus one (to account for position zero)

How can you initialize an array?

You can initialise an array by assigning values to each element or by using the initialiser list {} after declaring the array. If left empty it will initialise every element to zero or empty.

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?

Writing outside the bounds of the array will cause undefined behaviour such as overwriting another variable or crashing the program.

1 Like

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:
    -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.
    Dynamic:
    -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?
    int exampleArray[3];
    exampleArray[0] = 6;
    exampleArray[1] = 4;
    exampleArray[2] = 18;
    or
    int exampleArray[3] { 6, 4, 18 };
    If there are more initializers in the list than the array can hold, the compiler will generate an error.
    However, if there are less initializers in the list than the array can hold, the remaining elements are initialized to 0 (or whatever value 0 converts to for a non-integral fundamental type – e.g. 0.0 for double). This is called zero initialization .
  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?
    C++ does not do any checking to make sure that your indices are valid for the length of your array. You will get undefined behavior. This could overwrite the value of another variable, or cause your program to crash.
1 Like

Think about the following questions while reading:

1. What is 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? = length * elements size

  1. How can you initialize an array? int array[5]{ 2, 3, 5, 7, 11 };
  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]) = number of array elements
  4. What happens when you try to write outside the bounds of the array? undefined behaviours
1 Like

First part:

1. What is an array?

An array is a collection of multiple variables of the same 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 size of an array can be the number of elements it has, or the total byte size of these elements combined.

Second part:

First part:

1. What is an array?

An array is a collection of multiple variables of the same 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 size of an array can be the number of elements it has, or the total byte size of these elements combined.

Second part:

1. How can you initialze an array?

By giving the elements value like this: int array [4] {1,2,3,4}

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

size(array) - will give you the length of the array
sizeof(array) - will give you the total data size of the array (array length*size of element)

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

number of elements = sizeof(array)/sizeof(array[0])

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

You get undifined behaviour.

1 Like

First Part
1. What is an array?
An array is an aggregate data type that lets is access many variables of the same type through a single identifier. Uses the [Square brackets].

2. Which index would you use to access the 2nd variable in an array?
1 as Arrays elements start from 0 in there “array range”.

3. What can you say about the size of an array?
You are able to fix a size of an array be declaring it in the function. The maximum size of an array is determined by the amount of memory that a program can access.

Second Part

  1. How can you initialize an array?
    You can always go element by element but this would be very annoying quickly. With C++ it allows the use of initializer list which can simplify’s code and can be used more dynamically. Or, you can initialize to 0 using empty brackets.

  2. How can you get the size of an array in bytes?
    sizeof( myArray ) for the bytes used. With fixed length arrays you can use 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( myArray[0] ) will calculate the number of elements.

  4. What happens when you try to write outside the bounds of the array?
    Indexing outside the array will cause the program to inserted into the missing value with many will lead to undefined behavior which can lead to, overwriting other values, negative index’s an program crashes.

1 Like

This will only give you the size of one element, you can use this number and divide the entire size of the array to get its length :slight_smile:

1 Like

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]

What can you say about the size of an array?
The std::size() function from the header can be used to determine the length of arrays.
You initialize the size of a fixed array when you declare it by the number you write inside the “[]”. 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.

How can you initialize an array?
One way to initialize an array is to do it element by element or you can use of an initializer list.

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

What happens when you try to write outside the bounds of the array?
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 Like
  1. An array is a data type that lets us access many variables of the same type through a single identifier.

  2. Index number 1

  3. The size of an array can be determined by the number of variables that can be allocated in it.

Second Part

  1. Initializing an array, by using an Initializer list and defining individual values.

  2. You can get the size of array in bytes by using the size(array);

  3. sizeOf(array)// sizeOf(array[ ]);

  4. Error. or Undefined.

1 Like

Thanks mate :slight_smile: Appreciate it

You must divide by an element in the array, for example the first one (array[0]) :slight_smile:

1 Like

1. What is an array?
it’s a list of objects in a variable

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?
You can set an array of size n and the largest number in the array would be n-1

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

  2. How can you get the size of an array in bytes?
    sizeof(array) // will print the size of the array multiplied by the size of an int

  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    sizeof(array) / sizeof(array[0]) // gives the ‘total byte size’ divided by the array ‘per item size’

  4. What happens when you try to write outside the bounds of the array?
    It writes the index item in memory at a location where something else could possibly be.

1 Like