Arrays in C++ - Reading Assignment

#101
  1. An array is a list of a specific variable type.

  2. Index 1

  3. The size of an array is fixed but if it’s a dynamic array the size can be adjustable.

  4. You can initialize an array by using square brackets after a type variable.

  5. By using “sizeof()”.

  6. Sifeof()array/sizeof(element), and its recommended to use the first element (0).

  7. You will get undefined behavior.

1 Like

#102
  1. What is an array?

    • a data structure that contains a set of same type variables
  2. Which index would you use to access the 2nd variable in an array?

    • 1 (starts with 0)
  3. What can you say about the size of an array?

    • the size is determined by the length of the array (the number of variables stored)
  4. How can you initialize an array?

    • int count[5] = { 1, 2, 3, 4, 5 }
  5. How can you get the size of an array in bytes?

    • sizeof(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?

    • dangerous undefined behaviour
1 Like

#103
  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 to access the second variable in an array.

  3. What can you say about the size of an array?
    For fixed arrays the size has to be known and can not be changed later on because they have memory allocated at compile time. However, there are also dynamic arrays which can be set at compile time, and their length can be changed

  4. How can you initialize an array?
    You can initialize an array element by element or use an initializer list.

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

  6. 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

  7. 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

#104

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?
    array[1];
  3. What can you say about the size of an array?
    The size of an array is the array length multiplied by the element size.

Part 2:

  1. How can you initialize an array?
    One way to initialize an array is to do it element by element
    int prime[5]; // hold the first 5 prime numbers
    prime[0] = 2;
    prime[1] = 3;
    prime[2] = 5;
    prime[3] = 7;
    prime[4] = 11;
    OR
    int prime[5] = { 2, 3, 5, 7, 11 }; // use initializer list to initialize the fixed array
    An array can be initialized by
  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(<type of element in your array>);
  4. What happens when you try yo write outside the bounds of the array?
    You get undefined behavior. You can overwrite a value in memory, the program can crash.
1 Like

#105

1. What is an array?
An array is a structure 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?
An arrays count always starts from 0, so the 2nd variable index is [1].
3. What can you say about the size of an array?
a dynamic array doesn’t have a specified length so it’s size will vary.

A fixed array is an array where the length is known at compile time.
(int testScore[30]; // allocate 30 integer variables in a fixed array)

  1. How can you initialize an array?
    One way to initialize an array is to do it element by element, but can be difficult when dealing with large arrays. A more convenient way to initialize entire arrays via use of an initializer list.
    int prime[5] = { 2, 3, 5, 7, 11 }; // use initializer list to initialize the fixed array
  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.
  4. What happens when you try to write outside the bounds of the array?
    You will get undefined behavior – For example, this could overwrite the value of another variable, or cause your program to crash.
1 Like

#106
  1. Array is aatype of variable in which we can store multiple values with a single name.
  2. index 1
  3. No of elements in an array.

Part 2

  1. int arr[3] = {1, 2, 3};
  2. By using sizeof function.
  3. int arr[17];
    int size = sizeof(arr) / sizeof(arr[0]);
  4. C++ do not check if the index is correct it write the value at a memory location where the index we storing value into would have existed and it may cause unexpected behaviour.
1 Like

#107

Hello @ivan and dear Community,


  1. What is an array?

An array is a “aggregated” variable that can store multiple values within itself.

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

int index = 1; array[index];

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

It’s fixed and need compile-time constants when created.

  1. How can you initialize an array?

int iArray[5]; int iArray[] {1, 2, 3, 4};

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

int iArray[5]; std::cout << sizeof(iArray) << endl;

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

int iArray[]{1, 2, 3}; std::cout << sizeof(iArray) / sizeof(iArray[0]) << endl;

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

Unpredictable behaviour will result.


Kind regards
Kevin

0 Likes

#108
  1. What is an array?
    A index list of object for a given 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?
    It the array length * by element size.

  4. How can you initialize an array?
    int array[2] = {4,1}

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

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

  7. 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.

0 Likes

#109

Pt 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? An array can be sized as one pleases based on a program’s limit. And the size of an array can be declared in different ways.

Pt2

  1. How can you initialize an array? You can initialize the size and fill each element (per index) by declaring them one by one. But a better way is to use an initializer list holding the set of elements inside a brace.
  2. How can you get the size of an array in bytes? Use the sizeof operator (after inititializing your array properly)
  3. How can you use the function sizeof in order to calculate the number of elements in an array? You can divide the size of the entire array by the size of an array element, i.e. referring to the 1st element (index 0). In both dividend and divisor, the operator sizeof is used. But in C++17 or later, one can use std::size() once the <iterator> header is included.
  4. What happens when you try to write outside the bounds of the array? It may seemingly accept it without harmful effects, but it leads to undefined behavior. This is because C++ does not do any checking to ensure indices are valid for the length of an array.
0 Likes

#110
  1. What is an array?
    Ans: An array is a sequence of objects of the same data type.
  2. Which index would you use to access the 2nd variable in an array?
    Ans: Index-1 as arrays always start at 0 in C++
  3. What can you say about the size of an array?
    Ans: Common arrays have fixed size except “dynamic arrays”

Part-2

  1. How can you initialize an array?
    Ans: Arrays can be initialized by specifying type of array, name of array followed by square brackets containing the size of an array e.g. int testScore[30]; will initialize an array called “testScore” having size of 30 elements.
  2. How can you get the size of an array in bytes?
    Ans: Arrays are simply pointers to an arbitrary amount of memory. If you do sizeof(array) it will return the size of a pointer - 4 bytes on 32 bit systems, and 8 bytes on 64 bit systems (if the program is compiled as 64 bit).
  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    Ans: First, note that the size of the entire array is equal to the array’s length multiplied by the size of an element. Put more compactly: array size = array length * element size. Therefore
    sizeof(array) / sizeof(array[0]) can give us size of entire array.
  4. What happens when you try to write outside the bounds of the array?
    Ans: It may cause undesired behavior and may cause the program to crash.
0 Likes

#111
  1. What is an array?
  • A variable that can store a list of variables.
  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?
  • The size of an array is the array’s length multiplied by size of its elements
  1. How can you initialize an array?
  • int nameOfArray[size];
  • int nameOfArray2[size] = {0, 0, 0…};
  1. How can you get the size of an array in bytes?
  • sizeof(arrayName);
  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?
  • You basically overwrite other parts of memory outside the array, which can result in unpredictable behaviour that is difficult to debug
0 Likes

#112
  1. What is an array?
    An array is a 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?
    For static(fixed) arrays the size of the array is predefined at initialization

  4. How can you initialize an array?
    By using the initializer list or assigning element by element.

  5. How can you get the size of an array in bytes?
    With the sizeof operator denoted as sizeof(array)

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

  7. What happens when you try to write outside the bounds of the array?
    It will cause undefined and potentially risky behavior. It’s the developers’ responsibility to ensure that the indices are valid for the range of the array.

0 Likes

#113

1. What is an array?
It is a way to store multiple values through variables called elements of the array. The elements in an array need to be of the same type.

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?
The size needs to be known at compile time, hence we cannot define the array size through user input or let it be variable.

  1. How can you initialize an array?
    You can use the round braces after defining the array, where you set up its elements.

  2. How can you get the size of an array in bytes?
    By applying the sizeof function.The array size is the result of the multiplying of its elements with the element’s size in bytes.

  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 array to the size of its first element:
    sizeof(array) / sizeof(array[0])

  4. What happens when you try to write outside the bounds of the array?
    It will try to store the value in memory as an element of the array, but since no such element has been defined to exist,it will give us undefined behaviour, with potentially weird and dangerous consequences.

0 Likes

#114

What is an array?
A list of items

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

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 testScore is instantiated, the compiler will allocate 30 integers.

Think about the following questions while reading:

How can you initialize an array?
One way to initialize an array is to do it element by element
Fortunately, C++ provides a more convenient way to initialize entire arrays via use of an initializer list.

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

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

What happens when you try to write outside the bounds of the array?
It sets elements to ‘0’

0 Likes

#115

1. What is an array?
Is a data type object that let us store multiple values of a single type of variable in an indexed way.
2. Which index would you use to access the 2nd variable in an array?
index 1, since arrays start at index 0 (witch will be the 1st variable in the array)
3. What can you say about the size of an array?
Arrays on c++ can be fixed by the length of it and number of elements it contains, also u can have dynamic arrays that would change their size(length) while their operating in a procedure of the application.

  1. How can you initialize an array?
    int startArray[10];
    startArray[0] = 1

    OR: int startArray[10] = {1,2,3,4,5};
  2. How can you get the size of an array in bytes?
    “sizeof” operator will return the value of a variable in their size in bytes (memory).
    Example: 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?
    Unexpected errors by the compiler, if for example u get an array of 5 elements and call for the 6th (is not set on the array, so the compiler will dont know what to do exactly, bum, unexcpected error).
0 Likes

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

  2. 1

  3. The size of an array is the array length multiplied by element size.

  4. An array can be initialized several ways:
    int array[5] = {1, 2, 3, 4, 5 }
    int array[5] = { }
    double array [5] = { }

  5. sizeof(array)

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

  7. Could cause unexpected behaviour of the programme.

0 Likes

#117
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. You need to declare, just like for any other variable, the type of data the array will have.

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

Arrays use 0 base indexing, so you should use 1 so array[1].

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

The size of the array is declared in the brackets following its name. Using an initializer list, you don’t need to put a length, the size of the list will do it for you.

int array[5] = { 0, 1, 2, 3, 4 }; // explicitly define length of the array
int array[] = { 0, 1, 2, 3, 4 }; // let initializer list set length of the array

A fixed array is an array for which the length is known at compile time. This induces limitations, which is why you have dynamic arrays.

4. How can you initialize an array?

In an array variable declaration, 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). Then to allocate a value to each index, you can either do it element by element, or using an initializer list, which is preferable, especially when the array gets larger. If the list is larger than the length declared, you will get a compiler error. If there are less, then the default value of 0 will be attributed to the element.

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

You can use sizeof operator, but the array has to be in the function, not a function parameter, that calculates something else. So for example:

int main()
{
    int array[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
    std::cout << sizeof(array) << '\n'; // will print the size of the array
    return 0;
}
6. How can you use the function sizeof in order to calculate the number of elements in an array?

You can divide the size of the array by the size of an element of the array

int main()
{
    int array[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
    std::cout << "The array has: " << sizeof(array) / sizeof(array[0]) << "elements\n";
 
    return 0;
}

You use array[0] because it is the only one guaranteed to exist and you have to do that inside the same function, otherwise it wouldn’t work.
A simpler way to do that in C++17, is to use the included function

#include <iterator> // for std::size
 
int main()
{
    int array[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
    std::cout << "The array has: " << std::size(array) << "elements\n";
 
    return 0;
}
7. What happens when you try to write outside the bounds of the array?

The compiler does not check the existence of an array index, so it will successfully insert it and we will get an undefined behavior.

1 Like

#118
  1. An array is a data structure that allows a programmer to store and access a collection of data types using the same identifier.

  2. ‘1’ e.g if the array identifier was called myArray then to access the 2nd item in the array you would write; myArray[1]

  3. The array size must known at compile time (not runtime), when the programmer is declaring it.


  1. You can initialize an array in various ways once the size if known at compile time e.g
int numArray[4] = {1, 2 , 3, 4};
int nameArraySize = 10;
string nameArray[nameArraySize];
//enum
enum arrayEnum
{
    MAX_ARRAY_LENGTH = 5
};
int postCodes[MAX_ARRAY_LENGTH];
  1. You can get the size of an array of bytes using; sizeof(array);

  2. You can use the sizeof function to calculate the number of elements in the array by doing the following:
    sizeof()/sizeof(<[array index]>) e.g. size of(myArray)/sizeof(myArray[0])

  3. When you try to write outside the bounds of array, unintended consequences will happen bugs may happen such as bugs within your program and security vulnerabilities.

0 Likes

#119

1. What is an array? An array is a collection of variables. Each variable in the array is an element
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? The size of an array must be known at compile time, not at runtime, also, non-const variable numbers are not allowed. Nor is the following example allowed:
int length;
std::cin >> length;
int array[length]; // Not ok – length is not a compile-time constant!

for the part II section:

  1. How can you initialize an array? You can initialize an array several different ways: the first, most labor intensive, is the direct initialization:
    index[0]=1;
    index[1]=2;
    etc…
    there is the curly brace method of direct initializaion: int array[5] = { 7, 4, 5 } (this will initialize the first three elements, and then fill in ‘0’ for the remaining two.
    there is a way to initialize all elements with ‘0’ : int array[5] = {};
    there is an uniform initialization method in c++ 11: int array[5] { 2, 3, 5, 7, 11 };
    if the length of the array is omitted, then c++ will automatically create the correct array size (and maintain it if elements are added or removed later): int array[] { 3, 8, 9, 5, 7 };
    there is also a way to use an enum to initialize an array:
    enum studentNames
    {
    Kenny,
    Kyle,
    Wendy,
    Dave
    MAX_STUDENTS
    }
    if the MAX_STUDENTS is left at the end, then the array will auto-size as enums are added or removed later: int testScores[MAX_STUDENTS];

  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?
    prior to c++ 17, you can use the sizeof() function and divide by the size of one of the elements: sizeof(array)/array[0]. As of c++17, there is a size() function.

  4. What happens when you try to write outside the bounds of the array? you will get ‘undefined behavior’ which could include crashing your program.

0 Likes

#120

1. What is an array?
An array is an aggregate data type that lets us access many different variables of the same type through an 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?
It must be declared pre-compile time and cannot be changed.

1. How can you initialize an array?
You could add empty square brackets or use curly brackets listing the value for each element.
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?
You can take the sizeof function and divide it by the size of an element in array to calculate the number of elements in the array.
4. What happens when you try to write outside the bounds of the array?
C++ will let you do this and this can cause undefined behavior.

0 Likes