Arrays in C++ - Reading Assignment

article 1
1. what is an array?
A=
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 acces the 2nd variable in an array?
A=
myArray[1];
we start counting from [0], [1], [2]… etc.

3. what can you say about the size of an array?
A=
we can declare how many variables we want in a fixed array like this:

int myArray[30]{};

Fixed arrays cannot have a length based on either user input or other value calculated at run time, and they cannot be changed. For that there is dynamic array which the length can be set and changed at runtime.

article 2
1. how can you initialize an array?
A=
Array elements are treated just like normal variables, and as such, they are not initialized when created, one way to initialize an array is to do it element by element OR with an initializer list:

int myarray[3]{"Alpha", "Bravo", "Charlie"};

however if there are less initializers in the list than the array can hold, the remaining elements are initialized to 0 (zero initialization).

2. how can you get the size of an array in bytes?
A=
by using sizeof operator, 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?
A=
we can determine the length of a fixed array by dividing the size of the entire array by the size of an array element:

#include <iostream>
int main()
{
      int myArray[]{1,2,3,4,5};
      std::cout<<"the array has: "<<sizeof(myArray)/sizeof(array[0])
      <<" elements"<<endl;

      return 0;
}

4. what happens when you try to write outside the bounds of the array?
A=
C++ does NOT checks to make sure that your indices are valid for the length of the array. It will get undefined behavior. For example, this could cause an overwrite on the value of another variable, or crashing the program.

1 Like
  1. How can you initialize an array?

One way to “initialize” an array is to do it element by element:

1

2

3

4

5

6 int prime[5]; // hold the first 5 prime numbers

prime[0] = 2;

prime[1] = 3;

prime[2] = 5;

prime[3] = 7;

prime[4] = 11;

However, this is a pain, especially as the array gets larger. Furthermore, it’s not initialization, but assignment. Assignments don’t work if the array is const .

Fortunately, C++ provides a more convenient way to initialize entire arrays via use of an initializer list . The following example initializes the array with the same values as the one above:

1 int prime[5]{ 2, 3, 5, 7, 11 }; // use initializer list to initialize the fixed array

If there are more initializers in the list than the array can hold, the compiler will generate an error.

However, if there are less initializers in the list than the array can hold, the remaining elements are initialized to 0 (or whatever value 0 converts to for a non-integral fundamental type – e.g. 0.0 for double). This is called zero initialization .
to initialize all the elements of an array to 0, you can do this:

1

2

3

4

5

6

7

8 // Initialize all elements to 0

int array[5]{ };

// Initialize all elements to 0.0

double array[5]{ };

// Initialize all elements to an empty string

std::string array[5]{ };

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

By using enumeration.

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

This can be solved by setting up an enumeration where one enumerator maps to each of the possible array indices.

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

You’ll get a compiler error.

1 Like

Part 1

  1. its an aggregated database that lets you access many variables of the same type through a single identifier.
  2. 1, because we start counting from 0.
  3. the total length of an array is N-1 (array[N])

Part 2

  1. To initialize an array write " array[5]{1,2,3,4,5} // you could also write " array[]{1,2,3,4,5,…} - the latter version lets you add numbers into it without having to change the array size every single time.
  2. sizeof(array)
  3. sizeof(array)/sizeof(array element)
  4. memory location outside of an array will be resulting in an unpredictable way.
1 Like

Part 1

  1. An array is a set of variables with same data types.

  2. The index of 2nd variable in an array is 1.

  3. An array size is array length multiplied by size of data type.

Part 2

  1. One way to initialize an array:
    int prime[5]{ 2, 3, 5, 7, 11 };
  1. sizeof(a) gives the size of the array (a) in bytes.

  2. Number of elements can be calculated by:

    sizeof(a)/sizeof(a[0])
  1. If you access outside the bounds of the array, you will get undefined behavior. For example, overwriting another value.
1 Like
  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?
  4. How can you initialize an array?
  5. How can you get the size of an array in bytes?
  6. How can you use the function sizeof in order to calculate the number of elements in an array?
  7. What happens when you try to write outside the bounds of the array?

1: an array is a set of elements of the same type. All these variables have the same identifier.

2: i would write: array[1];

3: the size of an array can be fixed at initialization or defined by subscripts in {}.

4: like this: array[]{};

5: like that: cout<<size(array);

6: like this(and a…^^): cout <<sizeof(array) / sizeof(array[0]);

7: its going to end in undefinable behavior.

1 Like

Array part I

  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 C++ there is two types of array sizes which is a fixed-size array and dynamic array.

Array part II

  1. Using an initializer list.
  2. Using the sizeof operator
  3. Array length multiplied by the element size or array size = array length * element size
  4. Get undefined behavior or cause the program to crash.
1 Like

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.

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

[1]

What can you say about the size of an array?

The size of an array is based on the number of array elements minus 1, as the count of an array always starts at 0 instead of 1. This is called the array's range.

How can you initialize an array?

You can do it element by element or via use of an initializer list. eg int prime[5]{ 2, 4, 6, 8, 10 };

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

Using sizeof operator.

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

The sizeof operator can be used on arrays and it will return the total size of the array (array length multiplied by element size) or the number of elements present in it.

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

C++ doesn't do any checking to make sure that your indices are valid for the length of your array.
If you go out of bounds on your array, you will get undefined behaviour -- For example, this could overwrite the value of another variable, or cause your program to crash.

1 Like

Part1

  1. An array provides a structure for which variables of the same type can be stored. This prevents duplication of code.

  2. As the first starts with [0] it would be [1].

  3. The size of an array is the array length * data type size.

Part 2

  1. By creating an initializer list.

  2. sizeof (array);

  3. sizeof(Array) / sizeof(datatype of array)

  4. It caused undefined behaviour

1 Like

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?

The size of my array is defined is how many variables are in it.
There are arrays with a fixed length and dynamic.

  1. How can you initialize an array?

You can initialize an array using an initializer list.2.

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

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

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

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

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

C++ does not do any checking to make sure that your indices are valid for the length of your array. When this happens, you will get undefined behaviour – For example, this could overwrite the value of another variable, or cause your program to crash.

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. Index 1 will access the second variable in an array.

  3. A fixed size array is declared and the length is known at compile time.

  4. Arrays can be initialized via the use of an initializer list.

  5. Determine the size of an array in bytes by use of the sizeof() operator.

  6. The sixeof() function can be used to find the the number of elements in an array by dividing the total size of the array by the size of the array element.

  7. Writing outside the bounds of an array can result in undetermined behavior.

1 Like

Part I:
1. What is an array?

  • Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value.

  • The array data type 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 indexes start with 0: [0] is the first element. [1] is the second element, etc.
string fruits[4] = {"Apple", "Banana", "Orange", "Strawberry"};

cout << fruits[1];
// Banana

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

Part ||:

  1. How can you initialize an array?
  • To declare an array, define the variable type, specify the name of the array followed by square brackets and specify the number of elements it should store.

  • To insert values to it, we can use an array literal, placing the values in a comma-separated list (initializer list), inside curly braces.

int myNum[3] = {10, 20, 30};
  1. How can you get the size of an array in bytes?
  • 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)
  1. How can you use the function sizeof in order to calculate the number of elements in an array?
  • To get number of elements in an array you must divide arrays’s the size by the size of single element.
int main () {
    int arr[5] = {2, 7, 1, 9, 10};
    int len = sizeof(arr)/sizeof(arr[0]);

    cout << "The length of the array is: " << len << endl;

    return 0;
}
// The length of the array is: 5
  1. What happens when you try to write outside the bounds of the array?
  • Undefined behavior may occur if an array is accessed out of bounds and/or attempts to access memory that has not been properly allocated. Results may be completely unpredictable and it will lead to some extremely buggy problems that will be hard to find.
1 Like
  1. An array is an aggregate data type that lets us access many variables of the same type through an identifier.

  2. index[1].

  3. size of an array = array.length * element size.

  4. The recommended practice is to develop an initializer list.

  5. sizeof(array).

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

  7. The desired value will be entered as the next element in the slot the next element would go; causing undefined behavior.

2 Likes

Sizeof will give you the size of the actual array, not its pointer :slight_smile: some data types like class objects will only return the pointer size indeed.

2 Likes

Got it, thank you for the clarification :). I was a little confused with all the different stuff I found about sizeof.

1 Like

Part 1

Q: What is an array?

A: An array is a data type that gives us access to many variables in a single identifier.

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

A: int array[1] {}; - they can also be made from other data types like double and struct.

Q: What can you say about the size of an array?

A: It can be fixed in the square brackets or dynamic.

Part 2

Q: How can you initialize an array?

A: One way is to do it element by element but it will be painful if it is long. The other way is:

Int prime [3]{4,5,6};

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

A: sizeof(array);

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

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

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

A: It will add it to the memory and you will get undefined behavior like crashing or a overwrite of a value.

2 Likes

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

2: Using the Array name and a subscript operator. like arrayName[4] for the 5th index. because 0 is technically first.

3: the length of arrays can be fixed or dynamic.

PART TWO

1: plenty of ways but one way is to print int array[ ] = { 1, 2, 3,}; this would be setting the length of the array.

2: sizeof(array);

3: sizeof(array)/sizeof();

4:undefined behavior or the program crashing.

2 Likes
  1. What is an array? An aggregate data type that allows you to access many variables of the same type under on 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? The max size is limited by the amount of memory the computer has available. Also, if there are more initializers in the list than the array can hold the compiler will return an error. **

2 Likes
  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. An array can be any size you want it to be. However a Fixed length array is limited and size is known at compile time.

1.Arrays can be initialized element by element or by using an Initializer list.

2.The sizeof operator can be used to determine array size in bytes. std::cout << sizeof(array) << ‘\n’; // will print the size of the array multiplied by the size of an int

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

  2. Undefined behavior occurs when you try to write outside of the bounds of an array.

1 Like
  1. An array is an aggregate data type that allows the user to access many variables of the same type using a single identifier.

  2. To access the 2nd variable in an array use array name followed by the subscript operator and subscript [1]

  3. Fixed arrays have a memory allocated when the program code is compiled which means that the array length cannot be changed after compilation. However, the length of a dynamic array can be set at runtime and then changed.

  4. You can initialise an array by using an initialiser list, eg. int even[5]{2,4,6,8,10};
    If you omit the length and type int even[]{2,4,6,8,10}; the compiler will work out the length using the number of elements in the array.

  5. Use the function sizeof(array) to find the size of an array in bytes.

  6. To determine the number of elements in an array, divide the size of the array in bytes by the size of each element in bytes.

  7. Trying to write outside the bounds of an array will lead to undefined behaviour.

1 Like

What is an array?
An array is a data type where you can access many variables of the same type through a single identifier

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

What can you say about the size of an array?
Ha, first of all it is unlimited, but wait the limit is the sky and
in our case it is defined by the operating system.

How can you initialize an array?
Elemen by element. As example

myArray[0] = 10;
myArray[1] = 20;

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

How can you use the function size of in order to calculate the number of elements in an array?
Using size of array …sizeof(myArray) / sizeof(myrray[0])

What happens when you try to write outside the bounds of the array?
This will lead to an undefined behaviour

1 Like