Arrays in C++ - Reading Assignment

1 -> An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
2 -> arrayName[1]
3 -> the size or length of a array are the amount of variables inside a array

1 -> int arrayName[3]{ 1, 2, 3 };
2 -> with function sizeOf(arrayName)
3 -> sizeOf(arrayName[0])
4-> we may get a segmentation fault which at least tell there’s a problem. In the end, it is up to make sure that your program respects the bounds of its arrays .

1 Like

This will give you the size of the array element. To get the length you must divide the entire size of the array with the array element size. :slight_smile:

1 Like

Arrays in C++ - Reading Assignment.

A.

  1. An array is a collection of items stored in a contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

  2. Index used to access the 2nd variable in an array is:

  • std: : cout << array[1] << ‘\ n’;
  1. The size of an array - std: :size() - will cause a compiler error if you try to use it on a fixed array that has been passed to function.

B.

  1. You can initialize an array by including values in braces after the declaration. eg
    int nconst [ 3 ] = {1, 2, 3, } element.

  2. You can get the size of an array in byte by using a machine with 4 bytes integers and 8 bytes pointers

  3. You can use the function sizeOf by dividing the size of the entire array by the size of an array element.

  4. When you try to write outside the bounds of an array, you will get an undefined behaviour and could over write the value of another variable, or cause the program to crash

2 Likes

Are you sure about that? It should work just fine.

No matter what machine you use, sizeof should give you the size of the array in bytes. :slight_smile:

Think about the following questions while reading:

  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. e.g: array[1].

  3. What can you say about the size of an array?
    The size of an array may be fixed (fixed-length array) where the size is known at compile time or dynamic (dynamic array) where the size may be set at runtime and changed during runtime.

Think about the following questions while reading:

  1. How can you initialize an array?
    An array can be initializes individually for each element, or using an initializer list.

  2. How can you get the size of an array in bytes?
    Using 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 a
    I would get undefined behaviour and possibly overwrite the value of another variable.

1 Like

1.What is an array?
an array is a list of values or parameters or words.

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?
as a programmer you can make the length of an array as much as you want. As long it can fit into your memory of the computer you use.

second article
1.How can you initialize an array?
with this:

int array[]{2,4,5,7};

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

std::cout << std::size(array)<< "\n";

but if the array has been passed thru a function it is not possible to use this.

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

int array[]{1,5,6,4,50,75,78,80};
std::cout << "The array has: " << sizeof(array) / sizeof(array[0]) << "elements\n";

return 0;

4.What happens when you try to write outside the bounds of the array?
then you get undifined behaviour or the program crashes.

1 Like

This would give you the length of the array. To get the size in bytes you should use sizeof. :slight_smile:

1 Like

Arrays 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?
    = myArray[1];

  3. What can you say about the size of an array?
    = The size (or length) can be fixed or dynamic.

Arrays Part 2:

  1. How can you initialize an array?
    = int myArray[3] {1, 2, 3};
    int: type, myArray: name of the array, [3]: size/length, {1, 2, 3}: initializer list

  2. How can you get the size of an array in bytes?
    = By using 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 behaviour.

1 Like

ahh yes i see thanks a lot for keeping me sharp. :slight_smile:

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. Index 1.
  3. The size of an array cannot be changed.
    Part 2
  4. arrayType arrayName[arraySize];
  5. sizeof(array);
  6. sizeof (array) / sizeof (array [0])
  7. It will result in undefined behavior.
1 Like

First Reading

  1. An array is a collection of elements of the same type, placed in memory locations that can be called upon using an index to a unique identifier. For example, you can assign 5 values of type-int to the same array, instead of creating 5 separate variables.

  2. array[1]{};

  3. The size of an array is either fixed or dynamic. Regardless, it is calculated by multiplying element size by the length of the array.

Second Reading

  1. You can initialize an array by using an initializer list. (Ex: int prime[5]{ 1, 12, 4 , 5 , 10};

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

  3. You can use the function sizeof to calculate the number of elements inside an array by sizeof(array) / sizeof(array[0] … sizeof(array) is the array length, divided by the size of the first element in the array (if it’s an integer, it’ll be one).

  4. Writing outside the bounds of an array returns undesirable and undefined behavior. C++ won’t catch the error, and will find a random value (probably previously used somewhere in its library) and assign it to the “accidentally” newly added element.

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. To access the 2nd variable in an array, one would use the index array[1]. i.e. the second variable in a C++ array would be [1] because in C++ counting starts at 0 (not 1).
  3. The size of an array can be fixed or dynamic. If fixed, its length must be declared at compile time; this means its length cannot be changed and is known prior to run time. Whereas a dynamic array can have a a length which is based on user input or a value calculated at run time (these are more complex to instantiate, but possible using C++)
  • To initialize an array, one can use1. an “initiator list”.
  • To get the size of an array in bytes using the iterator header “std::size()”, then using the sizeof operator “sizeof(array)
  • To use the function “sizeof” in order to calculate the number of elements in an array, we state "sizeof(array)/sizeof(array[0]). i.e. dividing the size of the entire array by the size of an array element. We always use [0] because we know that there will always be at least 1 element in a properly instantiated array.
  • When you try to write outside the bounds of the array the result will be undefined behaviour.
1 Like

Size will technically give you the length of the array or the number of elements. :slight_smile:

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

  2. Second variable in an array we access with index [1].

  3. size of an array can be fixed or dynamic

  4. one way to iitialize an array via initializer list

  5. sizeOf(arr)

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

  7. undefined behaviour

1 Like

I should have said sizeof(array), then correct?

Correct :slight_smile:

Part 1

1. What is an array?
Array is an aggregate that let 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 length of a fixed array must be known at compile time.
The length of a dynamic array can be set at runtime, and their length can be changed.

  1. How can you initialize an array?
    By using the initializer list int

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

  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 make any checking if your indices are valid for the length of the array. This could overwrite the value of another variable, or cause your program a crash.

1 Like
  1. What is an array?

Fixed arrays provide an easy way to allocate and use multiple variables of the same type so long as the length of the array is known at compile time.

  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?

Has to be compile time constant

  1. What is an array?

Fixed arrays provide an easy way to allocate and use multiple variables of the same type so long as the length of the array is known at compile time.

  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?

Has to be compile time constant

  1. How can you initialize an array?

Using an array initializer, int array[ ]{0,1,4,7,9}

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

Sizeof(array)

  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?

Gives an error

1 Like

You can have a dynamic array that can determine the size in runtime.

More accurately its undefined behaviour that most often result in an error. :slight_smile: