Arrays in C++ - Reading Assignment

Ans 1:
An array is a data aggregate which allows the complier to store data under one identifier.

Ans 2:
Index # 1

Ans 3:
The size of an array can be set to suit the amount of data that is required to be stored under that identifier. This name for these type are: => fixed ‘size-array’ or fixed ‘length-array’

Ans 1:
The long form for initializing an array is by writing the declaration with the integer amount in square brackets. Then on each corresponding line quote the declared name and in square brackets the index position followed by the ‘=’ symbol then the value that it equals.

The shorter form in C++ is to make the same declaration by using the keyword, ‘int’ followed by the declaration, ‘prime’ followed by the square brackets which nest the size or length of the array, followed by curly brackets where the values/elements by index, from left to right, are nested and separated by comma’s and spaces.

Ans 2:
By using the ‘sizeOf()’ operator to determine the bytes taken up in the RAM for that specified array.

Ans 3:
When using the sizeOf() operator to determine the number elements in an array, just divide the number of overall bytes determined by the base number of the variable type. Bytes for different variable types are: integer = 4, float = 4, double = 8, char = 1.

Ans 4:
When you write outside the bounds of the array it returns an error value.

1 Like

These can vary based on the architecture so to be sure don’t use a fixed number and instead also use the sizeof operator on the type like sizeof(int) :slight_smile:

1 Like

Thanks for the heads up Alko89…I’ll be sure to allow for this…Cheers!!! Johnb3… :wink:

Part 1

  1. Array is data type that hosts many variables of the same type through one identifier.
  2. [1]
  3. Size of an array can be fixed or dynamic

Part 2.

  1. By utilizing the initializer list whether it be defined in length or not.
  2. The size can be determined by taking the array length and multiply it with the element size.
  3. Can use sizeof to calculate the number element in the array without physically counting to determine the length
  4. Can overwrite one of the previous variables or cause the program to crash
1 Like

6.1
1. What is an array?
An array 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, like cryptos[1]
3. What can you say about the size of an array?
Fixed length arrays are defined as follows. int cryptos[999999]. So here the length of the array is fixed with a value of 999999.

6.2

  1. How can you initialize an array?
    Use an initializer list. For example string bigBoys[]{“BTC”, “ETH”};
  2. How can you get the size of an array in bytes? Us the function sizeof
  3. How can you use the function sizeof in order to calculate the number of elements in an array?
    By dividing the size of the entire array by the size of an array element
  4. What happens when you try to write outside the bounds of the array? You will get undefined behavior. You will be allowed write outside the bounds but this might overwrite some other data etc.
1 Like
  1. An array is a set of different elements. These elements contain similar data that is relevant to the array they are in. An example of this would be an array of different users that log onto a website.

  2. You would use index item [1]. This is because arrays start at 0 as opposed to 1.

  3. The size of an array can be whatever length you would like it to be. You can make fixed arrays which have a fixed length depending on what you put it’s length as. You can also make dynamic arrays which change based off various factors. An example would be creating an array list based off a users input into the system which can possibly have varying lengths.

  1. You can initialize an array by adding an initializer list after you specify the array.
    int array[5] = { 3, 5, 8, 3, 7 };

  2. You can get the size of array in bytes by using sizeof() operator with the variable of the array that you’d like to get the size of.

int array[3] { 0, 1, 2 }; 
sizeof(array);
  1. You can use the following formula…
int array[4] { 1, 2, 3 4 }; 
sizeof(array) / sizeof(array[0]) 

This will take the total size of the array in bytes and divide it by the size of an individual index
element. This will end up calculating the number of elements in that array. It is recommended to use the [0] index element b/c every element will have at least a [0] index element for situations in which
you do not know how many elements are in the array.

  1. When you write outside the bounds of an array you will have errors. This is because you are storing data in memory that is outside of the fixed array length.
1 Like
  1. An array is an aggregate data type that allows us to access many variables of the same type through a single identifier.

  2. 1

  3. Arrays can be any size, the size is declared at the declaration or manipulation of the fixed of dynamic arrays respectively.

  4. You can initialise arrays in a number of ways:

string myArray[3]{}; //3 empty integer elements	
int myArray[]{1,2,3} //implied 3 element integer with explicit values	
double myArray[5]{1.1,2.2,3.3} //5 element ‘double’ type array with the last two elements being 0.
  1. Using sizeof e.g sizeof(myArray)
  2. Dividing the size of the array by the size of an element (all elements are the size due to data type) e.g sizeof(myArray)/sizeof(myArray[0])
  3. This will result in undefined behaviour. Some variable value may be overwritten and/or the program may crash.
3 Likes

a1. An array is a data structure that allows for access to multiple values of the same data type through a single identifier.
a2. 1
a3. The size of an array is the aggregate of the size of the values held.

b1. Either by individually specifying each array element’s value directly or by using an initialization list (in curly braces in the line that defines the array and size).
b2. use the function call sizeof(relevant array)
b3. Divide the returned value of a call to sizeof with the array as the parameter by the value of a call to sizeof with an array element as the parameter.
b4. this is undefined behaviour, the program will write outside the bounds of the assigned memory, potentially overwriting existing data and leading to unpredictable results.

2 Likes
  1. What is an Array?
    An Array is a data structure consisting of a collection of elements

  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?
    Array size can be define at point of Array initialisation

  4. How can you initialize an array?
    Int Myscores[2] = {100,1}

  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) divided by sizeof(array[0]) = number of elements

  7. What happens when you try to write outside the bounds of the array?
    Value get store outside the array. And it will result in undefined behaviour. This will cause error and make the program to crash

1 Like

Hello! How are you?
This is intense dude

FIRST PART

  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. In C++ , we have fixed array (also called length array or fixed size array) is an array where the length is known at compile time.

SECOND PART

  1. The elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces {}. For example:
    int foo [3] = {52. 5, 8};

  2. sizeof(array)

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

  4. Undefined behavior, it could overwrite the value of another variable or cause your program to crash.

1 Like

:exploding_head:

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

  2. You access an array element by referring to the index number. Use index 1 to access the 2nd variable in an array!

  3. A fixed array (also called a fixed length array or fixed size array ) is an array where the length is known at compile time. You don’t have to specify the size of the array. But if you don’t, it will only be as big as the elements that are inserted into it.

:thinking:

  1. You should initialize local variables when you declare them. Fortunately, a small array may be initialized at the time it is declared with an initializer list. The following code snippet demonstrates how this is done: float floatArray[5] = {0.0, 1.0, 2.0, 3.0, 4.0};
  2. To determine the size of your array in bytes, you can use the sizeof operator:
int a[17];
size_t n = sizeof(a);
  1. We specify the array name and the first index that is array1[0] which helps us in starting the count from the beginning. It will start from index 0 and count till the end. Once it reached the end it will display the number of elements. It will be returned by this sizeof() function that we have.

  2. Writing outside the bounds of the array is undefined behavior

:face_with_monocle:

1 Like

sizeof gives you the size of the array in bytes. To get the length you can use this number and divide it by the array element type. For example an int array: sizeof(array)/sizeof(int) or better yet you can use the first element in the array that you can be sure it exists sizeof(array)/sizeof(array[0]) :slight_smile:

2 Likes

Thanks for clarity :slightly_smiling_face:. Keeping this in mind :writing_hand:

1 Like
  1. An array is an aggregation of values you can grab with 1 identifier.

  2. You would use index 1

  3. The size of an array can be fixed or dynamic

  4. Arrays can be initialized 1 by 1, or by a list

  5. You could use sizeOf

  6. You can divide the array size by the element size
    7)Youll get undefined behavior

1 Like

2nd Reading:

1.) An Array is an aggregate data type that lets us access many variables of the same/given type through one identifier.

2.) One.

3.) The size/length of an arrays are based on is they are fixed, (set compile time) or dynamic, (set by runtime.)

2nd Reading:

1.) Your can initialize an array by using an initializer list, ie. {2, 3, 5, 7,};

2.) sizeOf(arr)

3.) sizeOf(array) or as (array [0])

4.) Writing outside the bounds of the array will get you undefined behavior.

1 Like

Dividing by the array element will not give the right result and could also crash a program in case its not the correct type. You should divide by the size of the array element instead sizeof(array[0]). :slight_smile:

1 Like

I believe that with the spaces between the / it is read as and/or and not divide.?.?.

Spaces don’t give the code any meaning in C++ its just for readability. The / operator simply means division.

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

  2. index 1

  3. In c++ we have fixed arrays where the length of the array is declared upon initialising. Or dynamic where length is not declared.

  4. Initialise fixed array by declaring length and and contents of the array. int age[3]{15,25,32}.

  5. function sizeof(array)

  6. sizeof(array)/sizeofarray[0]

  7. Writing outside the bounds of an array will cause undefined behaviour to occur or cause the program to crash.

1 Like