Arrays in C++ - Reading Assignment

Technically undefined behavior, but most modern compilers should throw an error. :slight_smile:

  1. Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. So you can say an array is an placeholder

  2. with the sizeof operator =
    int a[17];
    size_t n = sizeof(a);

  3. array * elementsize - 1

1 Like
  1. How can you initialize an array?
    you can initialze it element by element, but its a hard way, faster is to initialize like this example:
    int prime[5]{ 2, 3, 5, 7, 11 }; using the initialize list.

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

int a[17];
size_t n = sizeof(a);

  1. Wenn wir einen Array-Index verwenden, der außerhalb der Grenzen liegt, dann wird der Compiler wahrscheinlich kompilieren und sogar ausgeführt. Aber es gibt keine Garantie für ein korrektes Ergebnis. Das Ergebnis kann unvorhersehbar sein und es wird viele Probleme verursachen, die schwer zu finden sind. Daher müssen du bei der Verwendung der Array-Indizierung vorsichtig sein.
  1. An array is a data structure that allows multiple instances of the same data type to be stored together in memory.

  2. 1

  3. The size (and type) of the array is determined when declaring the array.

==================

  1. Arrays are initialized with [] when declaring and empty array by size, or with {} when declaring the array with the actual variables it will hold.

  2. Using sizeof() will return the length of the array multiplied by the size of the elements.

  3. You can use sizeof(array) divided by the element size (sizeof(array[0])) to determine the number of elements.

  4. Unintended behavior. The value will be written to the space in memory for the element, but this space could already be in use by something else.

1 Like

Its the array size divided by the element size (in bytes). :slight_smile:

I don’t understand :sweat_smile:

1 Like

1. What is an array? its a dataset in which similar elements of data are arranged in a table.
2. Which index would you use to access the 2nd variable in an array? int myarray[1]
3. What can you say about the size of an array? that its size is determined by data type and bytes

  1. How can you initialize an array? int prime[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? 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?
    creates unpredictable behavior and may cause 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.O,1
    3.is the array length multiplied by element size

  2. C++ provides a more convenient way to initialize entire arrays via use of an initializer list .
    2.sizeof(array);

1 Like

What happened with the rest of the homework? :stuck_out_tongue:

Thanks :slight_smile:
sh… haha, i switched to german and didnt regnozied it :sweat_smile:

Thanks for pointing that out. Best not to be lazy and count on the compiler catching these sorts of things!

  1. An array is a type of data that allows for you to store multiple values under one identifier.
  2. you would use index[1].
  3. The size of an array is described the arrays length times the element size
  4. You can initialize an array by doing it element by element. An easier way however would be through the use of an initializer list used within the curly brackets of an array.
  5. You can get the size of an array in bytes by using the sizeof function.
  6. You can calculate the number of elements by doing sizeof(array)/sizeof(array[0]).
  7. Undefined behavior will occur within the 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. To access the 2nd variable in an array we should use the index[1].
  3. The size of an array can be fixed or dynamic.

Part II

  1. An array can be initialized element by element or by using an initialization list.
  2. To get the size of an array in bytes we can use sizeof operator.
  3. Yes, sizeof can be used to calculate the number of elements in an array: length = sizeof(array) / sizeof(array[0]).
  4. It could overwrite the value of another variable, or cause your program to crash.
1 Like

1. What is an array?
a data structure consisting of a collection of similar elements which can be accessed by their indices

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 how many elements it contains

  1. How can you initialize an array?
    int arrayname[size] = {e,l,e,m,e,n,t,s}

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

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

  4. What happens when you try to write outside the bounds of the array?
    don’t do it, you don’t know what can happen

1 Like

You should also divide by the sizeof the first element. :slight_smile:

  1. An array is an aggregated data type that lets us access many variables of the same type through a single identifier.
  2. You index an array by stating the name of the array and indexing it like test[1].
  3. The size of an array is fixed if you know the length. Length is how many variables to allocate.
1 Like

1.1. An array is data type where you can store multiple values.
1.2. array[1]
1.3. The size of an arry is fixed.

2.1. int points[5]; this is the array points that can contain 5 integer values
2.2. cout << (sizeof(points);
2.3. cout << (sizeof(points)/sizeof(*points));
2.4. the program will store the value in the RAM, outside the reserved space of the array, its very dangerous! You will have undefined behavior and is vulnerable for hacking.

1 Like
  1. It is a datatype that allows us to access several memory areas using one identifier.
  2. Index 1
  3. It is generally fixed in C++ but can also be dynamically assigned using dynamic arrays.
1 Like
  1. What is an array?
    A variable where you can store indexed objects

  2. Which index would you use to access the 2nd variable in an array?
    First index is 0. The second index is 1. You access the second variable with the index 1 : 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.

How can you initialize an array?
by individually defining the values or create an initializer list which is recommended
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.
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?
it is assigned to the next byte in memory which runs as an undefined behavior that can cause errors or the entire program crashing.

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. Because in C++ start to count by 0 your number index for accessing the 2nd variable would be 1

  3. That it is define by the number that we put in the square bracket ([]) and when we talk about the size it is the number of time

  4. Via the use of an initializer list.

  5. By using the function sizeof when you create a array or by multiplying the array length by the element size.

  6. sizeof(array)/sizeof(array[0]);
    4.You will get undefined behavior.

1 Like

dividing by sizeof what? :wink: