Arrays in C++ - Reading Assignment

R1

  1. What is 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?

  3. What can you say about the size of an array?
    Array length multiplied by element size.

R2

  1. How can you initialize an array?
    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?
    To get the number of elements in an array you must divide the array’s size by size of single element.

  4. What happens when you try to write outside the bounds of the array?
    Undefined behavior – For example, this could overwrite the value of another variable, or cause your program to crash.

1 Like

FORUM QUESTIONS
Part 1

  1. What is an array?
    An array is a collection of values assigned to some given variable. This could also be described as a list of values, where each value has a specific index in relation to the array it is a part of.
    An array could also be considered the aggregation of some collection of variables which share type and meaning, into one indentifier.

  2. Which index would you use to access the 2nd variable in an array?
    Element indexing in arrays begin from zero, therefore to access the 2nd varriable in an array, you would access it as cout<< array[1];

  3. What can you say about the size of an array?
    The size of an array is also commonly referred to as the length. More specifically, this is the number of individual elements within the array.
    If a programmer wishes to have the length of the array known at the time of compiling, they can define a fixed array (or fixed size array).
    This is where the programmer will know exactly how long the length of the array should be and can predetermine this when initializing the array, like follows:
    int array[20]{}. In this instance, the array will take 20 values, each with a default value of 0 if the array is not filled properly.
    Alternatively, a programmer could initialize a dynamic array where their size and length can change after initialization.

Part 2

  1. How can you initialize an array?
    An array can be initialized a few ways.

    • A fixed array can be initialized on its own, such like: int array[10]; and then the array’s values can be injected separately, like: array[0] = 1;, array[1] = 5; etc.
    • A fixed array can also be initialized as a full array, like: int array[4] = {1, 4, 6, 9};
    • A programmer could also initialize an array without explicitly defining the length, but allowing the compiler to calculate the length, such as: int array[] = {1, 4, 6, 9}; where the initializer sets the length.
  2. How can you get the size of an array in bytes?
    The size of an array is the length of an array (number of individual elements inside array) multiplied by the element size (in bytes).

  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    You can use some basic alegbra to calculate the array length (number of elements in an array).
    We know that array size = array length * element size, therefore:
    array length = array size / element size. We can use the sizeof(array) value and sizeof(array[0]) to find the array size and element size, respectively. We can then plug these values into the array length function and find the number of elements in a given array.

  4. What happens when you try to write outside the bounds of the array?
    When you try to write outside the bounds of an array, your value will be inserted into memory where that value would have been, and the result is undefined behaviour. The compiler will run it as it is the programmers job to make sure this is not an issue, however it could overwrite another variable or crash the program.

ARTICLE QUESTIONS

  1. Declare an array to hold the high temperature (to the nearest tenth of a degree) for each day of a year (assume 365 days in a year). Initialize the array with a value of 0.0 for each day.
    double highTemp[365] = {0.0};

  2. Set up an enum with the names of the following animals: chicken, dog, cat, elephant, duck, and snake. Put the enum in a namespace. Define an array with an element for each of these animals, and use an initializer list to initialize each element to hold the number of legs that animal has.
    Write a main function that prints the number of legs an elephant has, using the enumerator.

#include<iostream>
#include<string>

using namespace std;

namespace animals {
  enum animals {
   chicken,
   dog,
   cat,
   elephant,
   duck,
   snake,
   max_animals
};
}

int main() {

  int numLegs[animals::max_animals] = {2, 4, 4, 4, 2, 0};
  cout<<"An elephant has " << numLegs[animals::elephant] << " legs."<< endl;

return 0;
}
1 Like

Reading 1

  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 (or length) of an array is the number of same-type variables that are allocated at compile-time (for fixed arrays). Array size is determined by the integer between square brackets.

1 Like

Reading 2

  1. You can do it element by element, but it’s easier to use an initializer list.

  2. By using the std::size() function.

  3. The sizeof operator will return the array length times element size.

  4. C++ still records that information as if the element existed. However, it may lead to undefined behaviour.

You would have to use index 1, because array indexes start with 0. :slight_smile:

Size will give you the length of the array, not the size in bytes. :slight_smile:

Thank you so much for making sure I knew the answer!

  1. What is an array?

A type of variable that lets one access particular variables within the array variable. These inner variables are called elements.

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

The index is [1].

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

There are two types of arrays that determines an array’s size:

  • Fixed arrays which have lengths that are known at compile time.
  • Dynamic arrays which have their length set at runtime and can be changed.
  1. How can you initialize an array?

Either by accessing the array element by element, or by using an initializer list which places the array elements in curly braces rather than on separate lines.

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

The sizeOf() operator lets one access the size of an array in bytes.

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

One can access the number of elements in an array by dividing the size of an array by an array element.

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

If one tries to index an array outside of its range, the user will receive ‘undefined’ as the output.

1 Like

/1variables put in a group

  1. prime1

  2. you can hold the first custom amount of inputs as a max

  3. int prime[5];

  4. sizeof(array);

3sizeof(array)/sizeof();

4 it will not work and bug the code

1 Like

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…

1 Like

First article answers:

  1. An array is an aggregate data type that lets us access many variables of the same type through a single identifier.
  2. The index to access the 2nd variable in an array should be [1]
  3. 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.

Second article answers:

  1. One way to initialize an array is doing it element by element, however its complicated when the array gets too big, C++ makes it easier and provides a more convenient way to initialize entire arrays via use of an initializer list .
  2. By sizeof(array);
  3. . sizeof(array)/sizeof(<type of element in your array>);
    Example with array of int : sizeof(array)/sizeof(int);
  4. You get undefined behavior and the program can crash.
1 Like

1. What is an array?
An array is a data container like a variable but it can store multiple data items all of the same type.

2. Which index would you use to access the 2nd variable in an array?
To access the second item in an array one would need to use an index of 1.

3. What can you say about the size of an array?
The size of an array is fixed and cannot change during program runtime. Also, the size of an array is allocated during compile time so therefore it cannot be determined during runtime (for e.g. via user input).

  1. How can you initialize an array?
    Arrays can be initialized using the list initialization syntax as with single variables. Empty brackets indicate an array of all zero values for the particular type ( for e.g. an array of strings would be initialized to an array of empty strings, array of ints to 0s and so on).

  2. How can you get the size of an array in bytes?
    To get the size of an array in bytes the sizeof operator can be used. This operator returns the number of elements in an array times the number of bytes each element takes up on the particular machine the sizeof operator is being run on.

  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    By dividing the result of the sizeof operator on the array by the result of the sizeof operator on the first element of the array -> sizeof(arr) / sizeof(arr[0]) // here ‘arr’ is an array variable

  4. What happens when you try to write outside the bounds of the array?
    C++ doesn’t do any checking to see if indices are in bounds so undefined behavior will result.

1 Like

What is an array? an array is a data type that allow us to access many variables of the same type through a single identifier.

which index would you use to access the 2nd variable in an array? 1 (starts at 0, 1).

what can you say about the size of an array? an array size can be declared at compile time (paints[40]{}) or you can do paints[]{2,3,4,5,5,6,7} and it will automatically pick the length.

You can initialize an array as follows: int paints[30]{}; int paints[3] = 33, you can also use enum.

You can get size of an array in bytes by using sizeof. sizeof(paints)

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

if you try to write outside the bounds of the array, you can get unintentional behavior, errors.

1 Like

Questions:
. An array is a data type that allows for users to access multiple variables through one identifier.
. The index [1] accesses the second variable.
. The array size is the length multiplied by the element size, while the range contains numbers zero through to the number one less than the length of the array

. Use the array name with square brackets and then place it equal to the list of elements in curly brackets.
. Use the function, sizeof(), to calculate the array size.
. Place the name of the array inside the brackets.
. Undefined behaviour can cause issues with the program.

1 Like

1.What is an array?
a data structure consisting of a collection of similar elements which can be accessed by their indices

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

  2. What can you say about the size of an array?
    The size of an array is how many elements it contains

How can you initialize an array?
int arrayname[size] = {e,l,e,m,e,n,t,s}

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

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

What happens when you try to write outside the bounds of the array?
Best not to do it if unsure.

You must divide with the size of the array element, not its value. :slight_smile:

I’m not sure what you mean. How would this give you the number of elements in the array?

Hi. Sorry I think I answered the wrong question. The way you calculate the number of elements is by doing, sizeof(array)/sizeof(array[0]).

1 Like

Part 1

  1. An array is an aggregate data type that allows us to store many variables of the same type in one identifier.

  2. [1]

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

Part 2

  1. Arrays can be initialized by putting each element inside curly braces and separated by commas.
    int array[3]{ 2, 4, 6 };

  2. By using the sizeof operator.
    sizeof(array)

  3. By dividing the size of the array by the size of an element in the array.
    sizeof(array) / sizeof(array[0])

  4. You will get undefined behavior.

1 Like
  1. It is an aggregate data type that allows access to a number of variables of the same type through an identifier.
  2. index[1]
  3. You can set the amount of elements to be within an array when initializing the array or the system can automatically do it for you.

Pt.2:

  1. By either enlisting each individual element to the array or by using an initializer list.
  2. By using the sizeof() function.
  3. By taking the size of the array and dividing it by the first element within the array.
  4. It will give a result that is classified as undefined behavior.
1 Like