Arrays in C++ - Reading Assignment

1. What is an array?

An array is a cumulative data form, which helps us to access several factors of the same form using a single identifier.

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

The first array always starts counting from 0 then 2nd subscript operator would indicate [1].

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

size of an array is referred to an array length which is declared by ([]).

4. How can you initialize an array?

one of the ways how to initialize an array is as a provided example of the following: int prime[4]={1, 3, 5, 7};

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

This refers to array length multiplied by the element size.

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

to calculate the number of elements in an array, we divide an array size by element size. sizeof(array)/sizeof(array[0])

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

You will get undefined behaviour, as overwrite the value of another variable, or cause a program to crash.

1 Like
  1. What is an array?
    1 Itā€™s a variable that lets you 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?

  3. ā€œ1ā€

  4. What can you say about the size of an array?
    3 It shows the number of variables in the array

  5. How can you initialize an array?

  6. An array always has square brackets

  7. How can you get the size of an array in bytes?
    2
    int a[17];
    size_t n = sizeof(a)/sizeof(a[0]);

3.How can you use the function sizeof in order to calculate the number of elements in an array?
3.by using this formula
array size = array length * element size.

  1. What happens when you try to write outside the bounds of the array?
    This could overwrite the value of another variable or cause your program to crash
1 Like

Part I

  1. An array lets us access many variables of the same type through a single identifier.
  2. The count starts at 0 so the second variable would be [1]
  3. The size of an array is the array length multiplied by element size. They can have fixed or dynamic size. The fixed one is quite limiting because it wouldnā€™t able to take extra user input and it also takes up extra RAM. Dynamic one, however, can have a changeable length but itā€™s more difficult to instantiate.

Part II

1.It can be initialized one by one but thatā€™s pain in the ass if we want to do it multiple times.


1 int prime[5]; // hold the first 5 prime numbers
2 prime[0] = 2;
3 prime[1] = 3;
4 prime[2] = 5;
5 prime[3] = 7;
6 prime[4] = 11;

etc.

For these cases, we can use an initializer list.

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

If there are more initializers in the list than the array can hold, the compiler will generate an error. If there are less, the compiler will print out ā€œ0ā€ in its place.

If we want to initialize all the elements of an array to 0, we can do this:

// Initialize all elements to 0
int array[5] = { };
 
// Initialize all elements to 0.0
double array[5] = { };
  1. sizeOf(arr);

  2. sizeof(array)/sizeof();
    For an array with int data: sizeof(array)/sizeof(int);

4.It may result in the program crashing or undefined and unwanted behavior.

1 Like
  1. An array is essentially a vector, that is, an indexed list of elements (with the smallest index being equal to zero).
  2. I would use 1.
  3. The size of an array must be defined at the time a program is compiled. Variable-size arraysā€”or dynamic arraysā€”can apparently be defined as well, but the pertinent definition was not given in the article.
  4. An array can be initialized by using the syntax array_name[array_length].
  5. The length of an array in bytes is sizeof(array).
  6. The number of elements is sizeof(array)/sizeof(array[0]).
  7. Thatā€™s impossible to tell because the resulting behavior is undefined. The program may crash or do something entirely unexpected.
1 Like

Part 1:

  1. An array is a variable where you can store indexed objects.
  2. Int array [1].
  3. The number of variables that it contains.

Part 2:

  1. Initializing an array by for example: int array[] = {0,1,2};
  2. Sizeof(array);
  3. Sizeof(array) / sizeof(array[1])
  4. Unexpected behaviour or crash/error.
    1. List item
1 Like
  1. An aggregate data type that lets us access many variables of the same type through a single identifier.

  2. number 1, like: array[1]

  3. The size can be determined or is defined by how many variables are put in.

  1. One way to initialize an array is to do it element by element. A more convenient way to initialize entire arrays is via use of an initializer list .

  2. Use the sizeof operator. Array length multiplied by element size.

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

  4. Undefined behavior.

1 Like

1. What is an array?

Arrays use square brackets. Arrays aggregate data type that let us use many variables of the same type.

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

The first index is 0. The second index is 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. You can choose the array length between brackets.

  1. How can you initialize an array?

You can initialize arrays by element by element or you can use an initializer list.

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

void printSize(int array[])
{ std::cout << sizeof(array) << ā€˜\nā€™; // prints the size of a pointer, not the size of the array!
}
int main()
{
int array[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
std::cout << sizeof(array) << ā€˜\nā€™; // will print the size of the array
printSize(array);
return 0;
}

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

#include // for std::cout
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;
}

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

You will get a response of undefined behavior.

1 Like
  1. Itā€™s a storage for multiple informations of the same data type which can be selected by their index.

  2. array[1]

  3. The size of the array is depending on the amount of the elements inside

  4. i.e. array[] = {1,2,3}

  5. sizeof(array)

  6. sizeof(arr)/sizeof(int) -> depending on the type of elements inside the array

  7. undefined behavior

1 Like
  1. 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 is fixed. It has to be determined and set before compiling and cannot be changed. Therefore, the length cannot be determined by eventual user input. Also, there is a dynamic array which its size is not fixed.
  1. There are 2 ways,
    1.1 element by element
    1.2 using an initializer list, array[3] = { 2, 5, 7 }
  2. using sizeof(array)
  3. using sizeof(array)/ sizeof(array[0])
  4. The code might be compiled, but the value will be inserted into memory and the result will be an undefined behavior.
1 Like

Hey guys let me know if this is the wrong forum but Iā€™m trying to compile a clone of BTC source code for research purposes, Iā€™m reading A. Antonopolous Book right now and trying to follow along with him thereā€¦ Iā€™m on OSX and keep running into a problem, when I go to ./compile, I receive a message saying ā€œDetected LibreSSL: This is NOT supported, and may break consensus compatibility!ā€ I have different SSL installed as well but cant find a way to point my compiler in the direction of thisā€¦ Iā€™ve checked github and there arenā€™t many helpful answers thereā€¦ Any thoughts would be appreciated or feel free to point me in the right direction of a better thread :slight_smile:

What is an array?

An array is a data type which allows us to store multiple variables of the same type and assign them to one identifier.

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

To access the second variable you would use the index[1]

What can you say about the size of an array?

An arrays size can be either fixed or dynamic. A fixed size array cannot be changed during runtime but a dynamic array can be changed during runtime.

How can you initialize an array?

An array could be initialized in the following ways

int myArray[5]{} //Initilize all elements to 0.0
std::myArray[5]; // Initilize all elements to an empty string.
int myArray[5]{1,2,3,4,5};

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

To get the byte size of an array you would use the sizeof() function

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

You can determine the number of elements by dividing the sizeof the array by the size of an element in the array usually element[0] because element[0] will always be present. The following expression would return the length

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

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

Writing outside the bounds of an array is permissible and wont be stopped but you need to be careful because there is a danger you may overwrite an area of memory which may contain an important variable value and you also have to be careful writing to negative indexes because the same result can be achieve by overwriting important memory areas.

1 Like

1. What is an array?
A variable where multiple elements can be stored and indexed.

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?
It can be declared between the brackets or if left empty, will be initiated when compiled. It also defines how many variables are stored in it.

  1. How can you initialize an array?
    A few ways:
  • int array[] = {1,1,2} // Let initializer list the length after you provide the elements.
  • int array[2] = {2} //Only initialize the 1st element in a 3 element array
  • int array[2] = { 1,1,2} // define length and all elements
  1. How can you get the size of an array in bytes?
    sizeof(array) = total integers * 4

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

  3. What happens when you try to write outside the bounds of the array?
    Itā€™s assigned to the next byte in memory which may cause a break in the program.

1 Like

Excellent answer sir! really well documented! keep it like that please! :muscle:

Carlos Z.

1 Like

1. What is an array?
Instead of declaring separate variables for each value, and array can be used to store multiple values in a single variable.

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?
Arrays can be fixed or dynamic. Fixed array length is known at compile time.

  1. How can you initialize an array?
    Element by element like an variable or via using an initializer list.

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

  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    Dividing the size of entire array by the size of an array element.

  4. What happens when you try to write outside the bounds of the array?
    C++ will compile and undefined behavior could appear

1 Like

A collection of variables of the same type have the same identifier.

1

We have two types of arrays
A fixed size array in which we specify its size in the compile time
A dynamic array in which we can change its size and its size is not predefined in compule time.

1 Like

Part I
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. Each variable in an array is called an element.

2. Which index would you use to access the 2nd variable in an array?
ArrayNameHere[1] because the first element in an array uses index 0. In the same way, the tenth element in an array would use index 9.

3. What can you say about the size of an array?
Fixed arrays have memory allocated at compile time. Their size is therefore predetermined because the length of the array has to be set prior to compile time. This can be limiting in certain situations because it means fixed arrays cannot have a length determined by user input at runtime for example because their length cannot be changed. To use an array that can be changed at runtime we would use a dynamic array instead.

Part II
1. How can you initialize an array?
You can initialize an array element by element but the best way to initialize an array is to do it using an initializer list, e.g. this would initialize an array of five prime numbers:

int ArrayNameHere[5] = { 2, 3, 5, 7, 11 };
int ArrayNameHere[5] { 2, 3, 5, 7, 11 };

The second example above uses the uniform initialization syntax for initializing an array in C++ 11 or later, which is where thereā€™s no equal sign involved.

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

3. How can you use the function sizeof in order to calculate the number of elements in an array?
The size of the entire array is equal to the arrayā€™s length multiplied by the size of an element. In algebraic form, this is: array size = array length * element size. We can therefore use algebra to re-arrange this equation to discover the element size. We use array[0] for the element size when we write this in our code because we know this element will exist regardless of how long the array might be.

int main()
{
int array[]{1,1,1,1};
std::cout<<ā€œThis array has " <<sizeof(array) / sizeof(array[0])<< " elementsā€ <<endl;

return 0;

}

When the above is compiled, it will tell us how many elements there are, in this case it will output ā€œThis array has 4 elementsā€.

4. What happens when you try to write outside the bounds of the array?
You will get undefined behavior, e.g. it could overwrite the value of another variable or cause your program to crash.

1 Like

Excellent answers sir, well documented! Please keep them like that :muscle:

Carlos Z.

  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?
    A fixed array is an array where the length is known at compile time.
    The length of a dynamic array can be set at runtime, and their length can be changed.

  4. How can you initialize an array?
    Element by element like an variable or via use of an initializer list.

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

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

  7. What happens when you try to write outside the bounds of the array?
    C++ will compile and undefined behavior could appear.

1 Like

smoke begins to evolve from the keyboard and you need to start looking for a new computer

1 Like
  1. Array is a type of data, where is possible to storage many different variables with same type.

  2. Index = 1.

  3. Size of array is his length.

  4. Same way as variable. But add [] at the end. It is possible to initialize array directly with data also.

  5. sizeof(array);

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

  7. Program behavior will be undefined.

1 Like