Arrays in C++ - Reading Assignment

@Imhotep, I really love your take on the above task especially your code snippets they were very user friendly… .:ok_hand:

1 Like
  1. What is an array?
    An array is like a list in python. It is a series of values that are indexed.

  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 is fixed in size

  4. How can you initialize an array?
    int prime[5]{ 2, 3, 5, 7, 11 };

  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(array[0])

  7. What happens when you try to write outside the bounds of the array?
    You gain access to memory that was not intended by the developer, that could be exploited.

3 Likes

Arrays with a fixed size have a set length with elements set to zero value or are uninitialized. :slight_smile:

2 Likes

Fixed, used to appending lists in python. Thank you!

As I understanding this append function actually takes an list, makes a new list that is one index bigger, then fills it with original values and then adds the additional value. So your 100% right. They are fixed.

2 Likes

Yea in case you have an array you some languages do that behind the scenes. Though you can also have a list or tree like data structures that append elements with some reference to previous elements. But I wouldn’t call these structures arrays since they work differently and are more complex structures.

Bitcoin blockchain for example resembles a Linked list data structure:
https://en.wikipedia.org/wiki/Linked_list

If you seriously want to torture yourself then Introduction to Algorithms is the book for you! :smile:

2 Likes

Arrays (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.
  2. Which index would you use to access the 2nd variable in an array?

    • int secondVariable[1]
  3. What can you say about the size of an array?

    • Fixed arrays cannot have a length based on either user input or some other value calculated at runtime.
    • Fixed arrays have a fixed length that can not be changed.
    • The length of a dynamic array can be set at runtime, and their length can be changed.

Arrays (Part II)

  1. How can you initialize an array?

    • One way to “initialize” an array is to do it element by element.
    • Via use of an initializer list.
  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[0])
  4. What happens when you try to write outside the bounds of the array?

    • It could overwrite the value of another variable, or cause your program to crash.
2 Likes

@Alko89, “Introduction to Algorithms” is one of the outstanding books published by the Massachusetts Institute of Technology(MIT) printing press. I have not read the latest edition but if you read the original edition, it gives you a unique insight into the mathematical functionality of Algorithms. I see the book as light reading which expands the mind into understanding how Algorithms basically work. When you get to the intermediate or advanced reading on Algorithms, now that is what I call utter torture…lol

1 Like

This is a very good follow on book after “Introduction to Algorithms”:

https://www.amazon.com/Algorithm-Design-Manual-Steven-Skiena/dp/1849967202/ref=pd_sbs_4?pd_rd_w=3ox6E&pf_rd_p=2419a049-62bf-452e-b0d0-ca5b7e35a7b4&pf_rd_r=P7PZYKRK531JBKKCHC7E&pd_rd_r=de2f10a6-fcb7-4ae7-907d-6e76b181269a&pd_rd_wg=cWiFw&pd_rd_i=1849967202&psc=1

You would need to divide the sizeof the full array with this array element size. :slight_smile:

1 Like

Man I wish I had time to read all these books, have like 10 of them in waiting in the lager :smile:

Thanks! :slightly_smiling_face:

Part 1:

  1. An array is a list of similar type variables
  2. 1 because an array in C++ starts at 0
  3. The size of an array can be defined fixed or dynamic, if you restrict an array to a certain size like array[2], the size of it will be 3 (n+1), if it’s dynamic the size of it will be determined by how many value it stores.

Part 2:

  1. int array[2] = {12} // initialize the first element of the array
  2. sizeof(array);
  3. sizeof(array)/sizeof(array[0])
  4. Undefined behavior.
1 Like

Actually the length of the array will be 2. Initializing array with a length is not the same as its index. :slight_smile:

1. What is an array?

an array is group of data that has access to many variable of the same type and with one single identifier.

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

to access the 2nd variable you would need to call testscore[3] because we have to count 0 as the first one.

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

you can have an array that is fixed in length , or you can have one with the length of N.

  1. How can you initialize an array?

by creating an initializer list.

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

if you times it by 4 you will get the number of bits.’

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

sizeof(array)

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

it will have an unpredictable result.

1 Like

What would that do? Why not just use sizeof? :slight_smile:

This will give you the size of the array, to get the length, you have to divide by the element size.

i get it now, thank you for the response Alko! much appreciated.

  1. What is an array? an array is an aggregate data type that allows us to access many variables of a single data 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? - you can fix the length of an array at compile time, or the length of a dynamic array can be set at runtime.

  4. How can you initialize an array? by creating an initialiser list in the {}

  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(array[0]) - by dividing the size of an array by the size of an array element

  7. What happens when you try to write outside the bounds of the array? could overwrite another value, crash the program - undefined behaviour.

1 Like
  1. An array is an aggregate data type that allows us access many variables of the same type via a single identifier.
  2. arrayName[1];
  3. Depends if the array is fixed or dynamic. A fixed array has to be a compile-time constant, this requires the length of the fixed array to be known at compile time, the length of the fixed array cannot be changed. Dynamic arrays can be set at runtime, and the length of the dynamic arrays can be changed.
  4. You could initialize an array element by element, or you could use an initializer list.
  5. size(array)
  6. sizeof(array)/sizeof(array[0])
  7. You will get an undefined error, leading to the program crashing or the value of another variable to be overwritten.
1 Like

1. What is an array?
A data structure for storing a sequence of values of the same type.

2. Which index would you use to access the 2nd variable in an array?
Since the 1st variable is present at index 0, so 2nd will be present at index 1.

3. What can you say about the size of an array?
For a static array, the length must be known at the compile time.
For a dynamic array, the length can be set at run time.

1. How can you initialize an array?
By using the initializer list, an array can be initialized.
For instance:
int arr[5]{}; // all the elements are initialized to 0
int arr[5] {2,3,4,5,8}; // elements are mapped to the corresponding values in the initializer list.
int arr[] {2,3,4,5,8}; //we don’t need to specify the size.
int arr[5] {2}; // First element is set to 2 while remaining are set to 0.

2. How can you get the size of an array in bytes?
int arr[5]{};
int sizeOfArray = sizeof(arr); //20 B

3. How can you use the function sizeof to calculate the number of elements in an array?
int arr[5]{};
int numberOfElements = sizeof(arr) / sizeof(int);

4. What happens when you try to write outside the bounds of the array?
It can result in undefined behavior. However, the compiler allows you to write outside the allocated memory with a warning.

1 Like

1 An array is an aggregate data type that let us access many variables of the same type through a single identifier.
2 array[1].
3 the size of an fixed array must be known.

2nd part
1 int nameofthearray [] {}
2 sizeof(array)
3 array length * element size
4 undefined behavior

1 Like