Arrays in C++ - Reading Assignment

Welcome to the discussion about the reading assignment about Arrays in C++.

Leave your answers to the questions below in this thread. If you have any questions or you want to discuss something connected to the assignment feel free to do it in this thread as well, but please everything to the topic.

Read this article (http://www.learncpp.com/cpp-tutorial/61-arrays-part-i/).

Think about the following questions while reading:

1. What is an array?
2. Which index would you use to access the 2nd variable in an array?
3. What can you say about the size of an array?

Read this article (http://www.learncpp.com/cpp-tutorial/62-arrays-part-ii/).

Think about the following questions while reading:

  1. How can you initialize an array?
  2. How can you get the size of an array in bytes?
  3. How can you use the function sizeof in order to calculate the number of elements in an array?
  4. What happens when you try to write outside the bounds of the array?
16 Likes

i-1. an indexed list of object of a given type
i-2. 1
i-3. it is fixed at array length multiplied by the element size

iv-1. comma separated values in curly braces, can initialize all or some elements, empty braces will initialize all elements to 0 for int
iv-2. sizeOf(arr)
iv-3. sizeof(arr)/sizeof(int) for other types sub type for int or in the article sizeof(arr)/sizeof(arr[0]) but I like my way better, it is more clear
iv-4. undefined behavior

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.

1. How can you initialize an array?
Many possibilities :

  • int array[3] = { 7 }; // only initialize the first element
  • int array[3] = { 0, 1, 2 }; // explicitly define length of the array
  • int array[] = { 0, 1, 2 }; // let initializer list set length of the array

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>);
Example with array of int : sizeof(array)/sizeof(int);

4. What happens when you try to write outside the bounds of the array?
You get undefined behavior. You can overwrite a value in memory, the program can crash…

11 Likes
  1. it’s like a storage and lookup table for variables of the same type

  2. 1

  3. the size (= length) of an array is either fixed or dynamic

  4. many ways, e.g.: myArray[3] = {1, 36, 23}; (using an initializer list)

  5. array length multiplied by element size

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

  7. When you write outside the bounds of the array, the value gets stored nevertheless, but this may and probably will result in undefined behaviour.

part 1
1. What is an array?
a data type that collects information to assign to multiple variables that is assigned the name of the array (the identifier)
2. Which index would you use to access the 2nd variable in an array?
int answer2[1];
3. What can you say about the size of an array?
the size which (also be known as the length) can be determined w/an array written on the stack( known as a standard array) that tells the compiler its size.

Part 2
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. What is an array?
    An array is a stucture that stores many variables of the same type inside a single data type

  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?
    Size of the array is defined at initialization (array length * data type size) and can not be changed (for static arrays).

  4. How can you initialize an array?
    Via initializer list when the array is defined, or element whise

  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 elemnt)

  7. What happens when you try to write outside the bounds of the array?
    the memory location outside of the array scope will be written resulting in a unpredictable result.

1. What is an array?

An array is a named list of similar type variables.

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

int myarray [ 1 ] because 1st variable in array starts at position 0

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

The size of my array is defined is how many variables are in it.
int myarray [ 1 ] has 2 variables.

1. How can you initialize an array?

You can initialize an array by declaring what type of array, name of array, using [ ] to define it’s variable(s) length(SCOPE) and by using the initializer list to define it’s variables values.

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

If the array is initialized then sizeof(array).

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

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

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

int prime[5];

// 5 Elements in array.

prime[5] = 13;

// Asking for 6th element because we start at 0.

Will cause undefined behavior because 13 will be put into memory where 6th element would have been.

First part:
1- An array is the way you can store many same type variables in a row
2- To access the second variable in an array, you have to use index 1
3- Size of an array are the sum of all its elements multiplied by the total bytes the type is

Second part:
1- You can initialize an array like this:
int arr[] = {1,2,3,4}
int arr[1]; arr[0] = 10; arr[1] = 20;
2- You can get the size of an array in bytes like this
sizeof(arr);
3- You can get the number of elements of an array like this:
sizeof(arr) / sizeof(arr[0]);
4- If you write outside the bounds of an array your program behavior could be what you not expect

Part 1:

What is an array?
- An array is a way to organize many of the same type of data under one identifier.

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

What can you say about the size of an array?
- There are two types of arrays, one is fixed and the other is dynamic. The difference is the ability to determine length of the array on the fly after compiling, dynamic does and fixed does not.

Part 2:

How can you initialize an array?
- It is similar to any data type definition however it also has square brackets [] to define it as an array.
// datatype name [];
- int array [5] = {0,1,2,3,4}; //fully defined
- int array [] = {} //Initializing zeros

How can you get the size of an array in bytes?
- sizeof(array);

How can you use the function sizeof in order to calculate the number of elements in an array?
- If you know that the machine uses 4 byte integers and teh size of the array is 32 bytes, then by division, we know this array has 8 elements (32/4=8).

What happens when you try to write outside the bounds of the array?
- Undefined behaviour and unintended data manipulation, aka, bad things.

Part I

1. Aggregate data type that allows to access many variables of the same type through a single identifier.

2. 1

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

Part II

1.

int array[5] = {0, 1, 2, 3, 4};
int array2[5] {0, 1, 2, 3, 4};
int array3[] = { 0, 1, 2, 3, 4 };

2. sizeof(array)

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

4. It will write outside the allocated memory of the array. This will result in undefined behavior and could even allow an “attacker” to insert code into your programm. In C++ it is important to ensure that the indices are valid for the range of the array!

  1. What is an array?
  2. Like in JavaScript, an array is a data type that allows you to store many values under a single identifier. Each value in an array is called an element. Like in JavaScript, arrays are declared using square brackets, but unlike java-script the array length must be declared as well. For example:

    int x[3];
    

    will generate an array with 3 elements.

  3. Which index would you use to access the 2nd variable in an array?
  4. Like in JavaScript, the index of elements in the array start at index 0. The last index in a string is always the array length minus 1. The second element in an array is at index 1.

  5. What can you say about the size of an array?
  6. The size of an array is determined by the number of elements. For an array of length N, the elements from element 0 to element N -1 is termed the arrays range.

  1. How can you initialize an array?
  2. You can initialize arrays assigning 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;
    

    Another easier way to initialize an array is by using an initializer list.

    int prime[5] = { 2, 3, 5, 7, 11 };
    
  3. How can you get the size of an array in bytes?
  4. By using the sizeof operator, which will return the total size of the array (array length multiplied by element size).

    sizeof(array); 
    

    By getting the sizeof a single element in an array we get the size of the pointer itself.

    sizeof(array[i]); 
    
  5. How can you use the function sizeof in order to calculate the number of elements in an array?
  6. The length of a fixed array can be calculated by dividing the size of the entire array by the size of the array element.

       int array[] = {0,1,2,3,4};
       cout << ((sizeof(array)/sizeof(array[0]))<< endl;
    
  7. What happens when you try to write outside the bounds of the array?
  8. As in the EOS variability, writing outside of an array range will cause unidentified behavior. Unlike JavaScript where the browser aids in memory management, in C++ devs have full responsibility for memory management. Writing outside array range causes you to use memory that has not been properly allocated, possibly overwriting necessary memory for normal intended program behavior.

2 Likes
What is an array?

An array is a collection of multiple elements all sharing the same type. An array is indexed and starts at 0.

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

Something like this (1):
int secondElement = fakeArray[1];

What can you say about the size of an array?

It can either be fixed or dynamic, dependant on how the programmer declares it.

How can you initialize an array?

int arrayLength = 3;
int fixedSizeArray[arrayLength];

or

int dynamicSizeArray[] = {};

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

Using the sizeof(arrayToBeMeasured); function.

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

Getting the size of the entire array and the size of one element of the corresponding array, we can divide the first number by the second to get the array length.

int lengthOfArrray = sizeof(array) / sizeof(array[0]);
cout << lengthOfArray << endl;

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

This will cause undefined behaviour and should be a security risk!

  1. a data structure consisting of a collection of elements that can be accessed by its index

  2. index 1

  3. arraysize = array length * element size

  4. datatype arrayName [array length] = { elements, …}

  5. sizeof(yourArray)

  6. sizeof(yourArray) / sizeof(datatype used for this array)

  7. undefined behavoir or even a crash of your program

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

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


  1. Element by element or with initializer list

  2. sizeof(array)

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

  4. undefined behavior

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- 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).array length(size) is a number in between of those brackets

part two :
1- One way to initialize an array is to do it element by element, which can be extremely hard. C++ provides a more convenient way to initialize entire arrays via use of an initializer list. for example:
int prime[5] = { 2, 3, 5, 7, 11 }; // use initializer list to initialize the fixed array.
2- 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- we can determine the length of a fixed array by dividing the size of the entire array by the size of an array element. sizeof(array) / sizeof(array[0]) ;
4- C++ does not do any checking to make sure that your indices are valid for the length of your array and inserts extra elements into the memory. When this happens, you will get undefined behavior . For example, this could overwrite the value of another variable, or cause your program to crash.

Quiz)
1- double highTemperature[365]={ 0.0 };

Part 1:

  1. An array is a data set
  2. arrayExample[1];
  3. It is the number of elements. Not sure what else to say. Do you mean how to find the length?

Part 2

  1. Here is one way:
    int prime[5] = { 2, 3, 5, 7, 11 };
  2. Thanks to @Ttoine for this. I wasn’t completely sure so I read his answer and it is:
  1.  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;
    
    }
  2. Undefined behavior
    I do have 1 question about this lesson though. What is enum? I don’t remember seeing it in any of the other lessons. Is this something that is taught later?
2 Likes

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

1- int prime[5] = {1,2,3,4,5}; or
int prime[5] = {}; // initialize all elements to 0

2- sizeof(array);

3- sizeof(array);/sizeof(array[0]);

4- undefined behavior

Think about the following questions while reading:

*1. **What is an array?struct to aggregate many different data types into one 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 atrubitte of the array called length

Think about the following questions while reading:

  1. ***How can you initialize an array? can be 0
  2. ***How can you get the size of an array in bytes? total size of the array
  3. ***How can you use the function sizeof in order to calculate the number of elements in an array? Yes, the sizeofarray()/size[index of element]
  4. ***What happens when you try to write outside the bounds of the array? 0

Arrays Part 1
1. What is an array?
An array is a variable that can store multiple values of the same type.

2. Which index would you use to access the 2nd variable in an array?
Arrays are indexed starting at 0, therefore the 2nd variable is indexed with 1.

3. What can you say about the size of an array?
There are two ways to declare an array. One being the dynamic array, where the size is not specified when it is declared and can vary in size. The other being the fixed array, where the size is specified by a compile-time constant on declaration and cannot be changed.

Arrays Part 2
1. How can you initialize an array?
Arrays can be initialised after the declaration by specifying the index and assigning the value.

int array[3];
array[0] = 10;

Though a much neater way (especially when initialising much larger arrays) is to use an initialiser list on declaration.

int array[] = {10, 4, 8};

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

3. How can you use the function sizeof in order to calculate the number of elements in an array?
You use it by dividing the size of the array by the size of the type of element in the array.

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

4. What happens when you try to write outside the bounds of the array?
What you’ll end up doing is overwriting other parts of memory, which can result in unpredictable behaviour that is very difficult to debug or even program crashes.