Arrays in C++ - Reading Assignment

1. What is an array?
2. Which index would you use to access the 2nd variable in an array?
3. What can you say about the size of an array?

A1) An array is an aggregate data type which allows us to execute many variables of the same type in a single identifier.

A2) I would use index (subscript) [1] to access the second variable, because in C++ arrays start counting from (0)

A3) The size of an array is always a fixed number and known at time of compiling.

  1. How can you initialize an array?
  2. How can you get the size of an array in bytes?
  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?

A1) You can initialise an array through what is called an ‘Initialiser list’. This is done by inserting the values within curly brackets after the subscript (Index)

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

A3) We can do this by dividing the size of the entire array by the of the size of the array element.

A4) Writing outside the bounds of an array will result in undefined behaviour, possibly overwriting the value of other variables or causing the program to crash.

1 Like

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

array[1] because array[0] is the first variable.

The length is known at compile time during the declaration of the array.

You can do it one element by element, use an initializer list, or use zero initialization.

sizeof(array)

You have to divide the sizeof(array) by the element sizeof(array[0])

It is undefined behavior

1 Like

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. Index 1
  3. It is defined by the number of variables in it

Part two

  1. int prime[5]{ 2, 3, 5, 7, 11 };
  2. sizeof(array);
  3. sizeof(yourArray) / sizeof(datatype used for this array)
  4. undefined behavoir or even a crash of your program
1 Like

1.) An array is an aggregate data type that lets us access many variables of the same type through one identifier. [1]. The size of a fixed array is set when it is initialized; dynamic arrays can be altered later.

2.) Int myArray[9] {}; Declare the data type, a space, the name immediately followed by [] with a fixed number inside, then a space, and finally {};.

3.) Use the sizeOf() operator.

4.) { //Given array
int arr[] = {10 ,20 ,30};

int al = sizeof(arr)/sizeof(arr[0]); //length calculation
cout << "The length of the array is: " <<al;

return 0;
}

5.) You will get undefined behavior.

1 Like
  1. An array is quicker way of writing multiple variables through a single identifier.

  2. One, Because the first index is always given 0

  3. Size reffers to the Dynamics of the array. These are fixed arrays. The Length is defined with a element size number.

PART ii.

  1. [] with the numbers inside. it is called an initializer list.

  2. Size of Operator does this.

  3. The std::size() function from the header can be used to calculate the number of elements in an array,

  4. Undefined behaviour. The value will get stored none the less but could cause it to crash later down the build.

1 Like

Can anyone help with the meaning of this Syntax?

‘\n’

Thank you all.

An array is a variable where you can store multiple objects.

[1].

The size of an array is the number of variables that can be assigned in the array.


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

sizeof(array);.

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

Unexcpected behavior and can crash your program.

1 Like

How would you get the size without using the helper function? :wink:

1 Like

This is a newline, the \ is an escape character which means everything after it will be treated as a special character (in case of n this means a newline), you can also use it to print characters that like " ("\"") or \ ("\\"). :slight_smile:

You can check other possible special characters here:
https://en.cppreference.com/w/cpp/language/escape

2 Likes

This will come in very useful, thanks pal.

No idea :slight_smile:

You can use sizeof which gives you the size of the element/array in bytes. You get the size of the entire array and divide with the size of either the first element or the type of elements the array stores.

1 Like

Reading Assignment: Arrays in C++

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?

Test array [1], as it’s index start with 0

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

length is known at compile time and are fixed

When instantiated, the compiler will allocate [n]variable_types, where n is the array range or number of array elements

PART2

  1. *How can you initialize an array?
    Int string_array[30]{}

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

  3. How can you use the function sizeof in order to calculate the number of elements in an array? std::cout << “The array has: " << std::size(array) << " elements\n”;

  4. *What happens when you try to write outside the bounds of the array?
    Anything outside of that range causes undefined behavior. If the index was near the range, most probably you read your own program’s memory. If the index was largely out of range, most probably your program will be killed by the operating system. But you can’t know, anything can happen.

1 Like

This will give you the number of elements in an array, to get the length in bytes you use sizeof. Can you figure out how to use sizeof to then get the number of elements without using ``std::size? :slight_smile:

1 Like

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. array size = array length * element size.

Part 2:

  1. By using an initializer list.

  2. By using the sizeof operator.

  3. We can determine the length of a fixed array by dividing the size of the entire array by the size of an array element.

  4. It will still store the value as if the element existed, but it will not create a new element.

2 Likes

Part I:

1. What is an array?
It’s 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?
You can setup an array in two ways; fixed or dynamic. In fixed arrays the size is declared and allocated memory at compile time, and dynamic arrays are declared at runtime.

Part II:

  1. How can you initialize an array?
    You can initialize an array in a list or by each element;
    e.g.:
    int array[3]{}; //one way
    array[0] = 1;
    array[1] = 1;
    array[2] = 1;

    double darray[3]{ 1.1, 2.2, 3.3 }; //another way

  2. How can you get the size of an array in bytes?
    The sizeof() operator gives you the size in bytes by taking the array’s length and multiplying it by the size of the element.

  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    By taking the array size and dividing it by the element size:
    sizeof(array) / sizeof(array[0])

  4. What happens when you try to write outside the bounds of the array?
    You will get undefined behavior which could overwrite the value of another variable, or cause your program to 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?
    use index 1 : array[1];

  3. What can you say about the size of an array? It os determined by number of elements there are.

  4. How can you initialize an array?
    You to do it element by element.

  5. How can you get the size of an array in bytes?
    When the array is initialized via the sizeof(array).

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

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

1 Like

You can’t initialize the array using sizeof. You initialize it like any array and use sizeof to get its size. :slight_smile:

1 Like
  1. What is an array?

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?

[1]

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

Can be where we know the size at compile time (Fixed Array). Also, we can have arrays where size can be changed later on (Dynamic Arrays)

How can you initialize an array?

Using an initializer list

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

Using the sizeof function

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?

You get undefined behaviour ie. Overwrite the value of another variable or cause the program to crash.

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

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

  3. Arrays can be instantiated with fixed or dynamic size.

  4. An array can be initialized with an initializer list or with zero initialization.

  5. sizeof() returns the size of an array in bytes.

  6. sizeof(array) / sizeof(array[0]) - dividing size of entire array by size of one element gives the number of elements in an array.

  7. When you try to write outside the bounds of an array, you will get undefined behaviour(could overwrite the value of another variable, or cause your program to crash).

Solution to problem 2 in Quiz:

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

int main()
{
    int animalNumberOfLegs[animalGroup::max_animals]{2, 4, 4, 4, 2, 0};

    cout << "Elephant has " << animalNumberOfLegs[animalGroup::elephant] << " legs." << endl;
    return 0;
}
1 Like