Arrays in C++ - Reading Assignment


#61
  1. Array is a data type that allows us to access many variables of the same type trough a single identifier.

  2. To access 2nd element I would use index 1.

  3. There are two types of arrays, fixed and dynamic. Fixed array size has to be defined. Once compiler allocates spacem the array size can not change. Dynamic arrays are complicated to instantiate, but their size can vary during runtime. Size is equal to number of elements * element size [in bytes].

  4. type arrayName[arraySize] = {}; example: int score[10] = {}; --> this would initialize an array named score containing 10 zeroes

  5. sizeof(array)

  6. sizeof(array) / sizeof(item type)

  7. If you try to manipulate an element with index out of array bounds C++ will actually manipulate the data out of the array memory and result in undefined behaviour.


#62

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?
To access the 2nd variable of an array we should use the 1 index since index starts from 0.

What can you say about the size of an array?
A fixed array (also called a fixed length array or fixed size array) is an array where the length is known at compile time.
C++ supports a second kind of array known as a dynamic array. The length of a dynamic array can be set at runtime, and their length can be changed.

How can you initialize an array?
A convenient way to initialize a fixed array is via use of an initializer list

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

How can you get the size of an array in bytes?
The sizeof operator can be used on arrays, and it will return the total size of the array (array length multiplied by element size in bytes).

How can you use the function sizeof in order to calculate the number of elements in an array?
We can determine the length of a fixed array by dividing the size of the entire array by the size of an array element:

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;
}

This prints:
The array has 8 elements

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 behavior – For example, this could overwrite the value of another variable, or cause your program to crash.

Source: Learn CPP


#63

What is an array?

  • 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?
  • no of elements
    How can you initialize an array?
  • int array[] = {};
    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?
  • sizeof(array)/sizeof(array[0])
    What happens when you try to write outside the bounds of the array?
  • error

#64

First part:

  1. An array is a variable that can hold multiple values stored in different elements.

  2. Index number 1, it always starts from 0.

  3. The size (meaning the length) of an array is adjusted when you declare it

Second part:

  1. Example: int array[4]; // Gives us 4 elements
    array[0];
    array[1];
    array[2];
    array[3];

  2. By typing sizeof(arrayName)

Example: sizeof(arrayName) / sizeof(arrayName[0])

  1. You will probably get an undefined behaviour that can make the program crash.

#65

Part1

  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?
    testScore[1]

  3. What can you say about the size of an array?
    Length of a fixed array must be known at compile time, and their length can not be changed
    Length of a dynamic array can be set at runtime, and their length can be changed.

Part 2
1.How can you initialize an array?
a.Element by element e.g
int prime[5];
prime[0] = 2;
prime[1] = 3;
prime[2] = 5;
prime[3] = 7;
prime[4] = 11;
b.An initializer list e.g
int prime[5] = { 2, 3, 5, 7, 11 };

4.What happens when you try to write outside the bounds of the array?
you will get undefined behavior


#66

pt 1

  1. An array is a collection of variables of the same datatype under one identifier.
  2. 2, because counting starts with 0.
  3. The size is defined in square brackets [] during the declaration.

pt 2

You can initialize an array element by element or by an list in curly brackets {}
By using sizeof() function.
By dividing the outcome of sizeof() function by the size in bytes of the arrays elements
A compile error will happen.


#67

1. What is an array? it 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? to output the size of an array we use sizeof

How can you initialize an array? int arrayExample[number] = {};
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? sizeof(array)/sizeof(array[0])
What happens when you try to write outside the bounds of the array?
it will add up that variable to the array


#68
  1. An array is an object that can store multiple values of the same data type through a single identifier.

  2. myArray[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. Arrays can be initialize arrays is to do it element by element or arrays can be initialized via a the use of an initializer list. Ex. int prime[5] = { 2, 3, 5, 7, 11 }; Another option is to initialize all elemnts to 0. Ex. intarray[5] = { };
  2. sizeof(array)
  3. sizeof(array)/sizeof(array[0])
  4. The program would turn into an error.

#69
  1. What is an array?
    A set of many variables of the same type, which are all part of a single identifier.

  2. Which index would you use to access the 2nd variable in an array?
    Specify the point in brackets ie; arrayName[2] to access the 2nd element

  3. What can you say about the size of an array?
    The size of an array is declared from the start. Fixed arrays cannot have a length based on anything known at runtime. Once declared cannot be changed

  4. How can you initialize an array?
    By treating each element as it’s own variable to define(sucks). Or, to space the elements between commas within curly brackets. Unfilled element slots will return 0 for number arrays. You can also have an open array without a set length and will be able to add or subtract elements on the fly

  5. How can you get the size of an array in bytes?
    Sizeof(yourArrayHere) will get the array length times element size, doesn’t work when the array is passed to a function

  6. How can you use the function sizeof in order to calculate the number of elements in an array?
    Sizeof(array) / sizeof(array[0]) = elements because array size = array length times element size

  7. What happens when you try to write outside the bounds of the array?
    Horrible terrible things, like your program breaking

#include

using namespace std;

double highTemp[365] = { };

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

int main()
{
int legs[6] = {2,4,4,4,2,0};

cout << "Hello world! Here's a number of legs on an elephant:   " << legs[animals::elephant]<< endl;
return 0;

}


#70

Part 1

  1. An array stores a fixed-size sequential collection of elements of the same type.

  2. index 1

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

Part 2

  1. by individually defining the values or create an initializer list

  2. If the array is initialized then sizeof(array).

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

  4. It is assigned to the next byte in memory which runs as an undefined behavior that can cause errors


#71

Part 1
1.What is an array?
An array is a data structure where multiple variables of the same type can be in the same data structure.

2.Which index would you use to access the 2nd variable in an array?
You would use the index, 1. Since the index starts from zero, the second variable would have an index number of 1 Example: myArray[1]

3.What can you say about the size of an array?
Size can be fixed or dynamic. For a fixed size array, the size is specified during the declaration. For a dynamic size array, its size can be specified during runtime (possibly by a variable that changes during runtime).

Part 2
1.How can you intialize an array?
We can initialize an array by declaring the type of array, how many elements in that area (this is optional), and then the elements within the array can be declared within the curly brackets {}.
Here is an example:

int minCoins [] = {100, 50, 20, 10, 5 , 1};

2.How can you get the size of an array in bytes?
The size of an array is given by the number of bytes each element contains, multiplied by the number of elements in the array. We can find the size of the array by using the operator ‘sizeof’.

3.How can you use the function sizeof in order to calculate the number of elements in an array?
We can calculate the number of elements in an array by divided the sizeof an array by the sizeof an individual element within the array. This is assuming the array is a fixed size array.
The number of elements can be calculated by:
sizeof(array) / sizeof(array[0])

4.What happens when you try to write outside the bounds of the array?
The computer will overwrite some data that already exist within its memory, to make space for the element that is out of bounds of the array, resulting in undefined behaviour.


#72

1 What is an array?

An array is a 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] index to access the 2nd variable in an array.

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

Fixed arrays provide an easy way to allocate and use multiple variables of the same type so long as the length of the array is known at compile time.

4 How can you initialize an array?

int prime[5]; // hold the first 5 prime numbers.
prime[0] = 2;
prime[1] = 3;
prime[2] = 5;
prime[3] = 7;
prime[4] = 11;

second way :

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

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

The sizeof operator can be used on arrays, and it will return the total size of the array (array length multiplied by element size)

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

We can determine the length of a fixed array by dividing the size of the entire array by the size of an array element.

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

When this happens, you will get undefined behavior – this could overwrite the value of another variable or cause your program to crash.


#73
  1. an array is a collection of types of the same type.

  2. index 1

  3. size is fixed when the array is declared

  4. int x[3] = { 0,1,2};

  5. using the sizeof operator.

  6. sizeof operator on the array / sizeof of the individual array entry.
    4.memory error.


#74
  1. It is data type which allow us to store and access large number of variables of the same type.

  2. Index nr. 1

  3. Arrays can be either fixed (the length has to be known at compile time) or dynamic (the length can be set at run time and is changeable).

  4. It can be done either each element at a time or with help of Initilizer list, which is much quicker, specialy in case of large arrays.

  5. It can be determined with help of sizeof operator.

  6. The size of the array devided by size of the array element. sizeof(array) / sizeof(array[0]);

  7. We would get undefined behaviour (change of the element value in the array or crash)


#75

1. What is an array?

1. An array is a 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?

2 array[1] to access the 2nd variable in an array.

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

Fixed arrays provide an easy way to allocate and use multiple variables of the same type so long as the length of the array is known at compile time.

  1. How can you initialize an array?
    int prime[5]; // hold the first 5 prime numbers.
    prime[0] = 2;
    prime[1] = 3;
    prime[2] = 5;
    prime[3] = 7;
    prime[4] = 11;

second way :

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

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

The sizeof operator can be used on arrays, and it will return the total size of the array (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?

We can determine the length of a fixed array by dividing the size of the entire array by the size of an array element.

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

When this happens, you will get undefined behavior – this could overwrite the value of another variable or cause your program to crash.


#76

Arrays in C++ Reading Assignment Part 1

1.An array is a finite list of variables much like in js however the number of elements has to be known before compilation. 2.Array[2] ; same as in javascript 3.It seems that no C++ Genius has written a function to do this. The closest thing is the sizeof() function which returns the size in bytes of an array or array element. The find the length you have to do sizeof(array) / sizeof(array[0])

Arrays in C++ Reading Assignment Part 2

1. int Array[3] = {1,2,3}; 2. sizeof(array) 3. The find the length you have to do sizeof(array) / sizeof(array[0]) 4.it wont compile or you get garbage.

#77

PART I

What is an array?
An array is a group of variables of the same type

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

What can you say about the size of an array?
The size of an array is determined when it gets defined, in the case of fixed arrays. It has to be known at compile time.

PART II

How can you initialise an array?
int myArray[3];
First we specify the type of array (according to the variables it will hold), then its name, and finally its size.

How can you get the size of an array in bytes?
we can use the sizeof operator

How can you use the function sizeof in order to calculate the number of elements in an array?
We can use this providing that we’re dealing with a fixed-length array, and we’re using it in the same function where the array is declared. As such, we can use calculate the number of elements by using sizeof and calculating sizeof(myArray) / sizeof(myArray[0])

What happens when you try to write outside the bounds of the array?
We’ll then have an undefined behaviour, from overwriting the value of another variable, to cause your program to crash.


#78

PART1
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 depends on how many variables to allocate (called the array length).

PART2
How can you initialize an array?
Via the use of an initializer list

How can you get the size of an array in bytes?
The sizeof operator can be used on arrays.

How can you use the function sizeof in order to calculate the number of elements in an array?
Divide the sizeof the total array by the size of an array element.

What happens when you try to write outside the bounds of the array?
you will get undefined behavior


#79

Arrays part I

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

2 If we have an array named “theArray” then we access the 2nd variable by typing “theArray[1]”, because the computer starts counting from 0 instead of 1.

3 The size of the array is fixed when we declare an array. Static array size can not be changed.

Arrays part II

1 Initializing an array element by element

int anArray[4]
anArray[0] = 1;
anArray[0] = 2;

Or by using an initializer list.

int anArray[4] = {1,2,3,4};

Giving more variables to the array than the array can hold will give an error, or if less variables are initiated, then the elements not assigned a value will be 0.

2

int array[] = {1,2,3…}
sizeof(array)

3

int array[] = {1,2,3…};
sizeof(array)/sizeof(array[0]);

4 Undefined behavior. Could overwrite the value of another variable or cause program to crash or any other problem.


#80
  1. What is an array? It is a collection of multiple variables of the same 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? With a fixed length array, the length has to be known at compile time. The length cannot be changed, and cannot rely on user input or some other value calculated at runtime.

  4. How can you initialize an array? By assigning an initial value.

  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 may overwrite the value of another variable, or the program may crash