Arrays Flashcards

1
Q

What is an array in C++?

A

Key: memory
Array: Chunk of memory

RAM ( Random Access Model ) model of computation

An array in C++ is a data structure used to store elements of the same type in sequential and indexed manner.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you declare an array in C++?

A

int myArray[5];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you initialize an array in C++?

A

An array can be initialized by providing a comma-separated list of elements within curly braces

int myArray[] = {1, 2, 3, 4, 5}; // static array
int *myArray = new int[10]; // dynamic array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain the concept of contiguous memory allocation as it relates to arrays in C++

A

Contiguous memory allocation means that the elements of an array are stored in adjacent memory locations.
Imagine chunks of memory allocated near themselves, one by one. Where each element occupies a fixed-size memory slot.

It has elements in a fixed-size memory which are allocated side by side.

This allows to use indexing for efficient RAM.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Discuss the difference between arrays and pointers in C++

A

Pointers point on the memory addresses.
Arrays store elements in a fixed-size memory.

Arrays and pointers are often confused, but they have distinct differences. Arrays are a fixed-size collection of elements of the same type stored in contiguous memory locations, while pointers are variables that store memory addresses. Arrays can decay into pointers when passed to functions or assigned to pointers, but they retain their size information.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

include <iostream></iostream>

Analyze the following C++ code snippet and explain its output:

using namespace std;

int main() {
int arr[3] = {1, 2, 3};
int* ptr = arr;

cout << *ptr << " ";   // Output: 1
cout << *(ptr + 1) << " ";  // Output: 2
cout << *(ptr + 2) << endl;  // Output: 3

return 0; }
A

This code snippet declares an integer array arr with 3 elements and initializes it.
Then, it declares a pointer ptr and assigns it the address of the first element of the array.
The cout statements dereference the pointer to print the values of the array elements sequentially. So, the output will be 1 2 3.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the concept of array decay in C++ and provide an example

A

Array decay refers to the automatic conversion of an array to a pointer to its first element in certain contexts, such as passing an array to a function or assigning it to a pointer variable.
For example:

void printArray(int arr[]) {
// Code to print array elements
}

int main() {
int myArray[5] = {1, 2, 3, 4, 5};
printArray(myArray); // Here, myArray decays into a pointer to its first element
return 0;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Discuss the advantages and disadvantages of using arrays in C++ compared to other data structures.

A

ONE TYPE DATA STRUCTURE ALLOCATION
Vector gives us the opportunity to manage with data without worry about memory management, also gives us many useful functions.
Dynamic array allows us to direct memory access and to extend data structure methods. With amortization, adding elements to the end has constant time.
Static array has linear time in adding elements to the memory. Only getter and setter have constant time.
Linked lists are giving you time, they are better in adding and deleting elements from the beginning.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Analyze the time complexity of accessing, inserting, and deleting elements in an array in C++.

A

Accessing elements in an array has constant time complexity O(1), as it involves simple arithmetic to calculate the memory address of the element.

Inserting and deleting elements in an array have time complexity O(n), as it may require shifting elements to maintain contiguous memory.

However, inserting or deleting at the end of the array can be done in constant time if sufficient space is available.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain the difference between a static array and a dynamic array in C++

A

A dynamic array has a pointer to elements stored in a fixed-size memory, that can be dynamically resized as needed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a multi-dimensional array?

A

Each element is identified by multi indexes; it is like a matrix that has elements in the table, which has rows and columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does C++ handle array index out-of-bound errors?

A

You have to use static array with static size implementation, you can use also vector, that automatically manage memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Discuss the advantages and disadvantages of using arrays over vectors in C++

A

Dynamic arrays benefits lies in management and development; it is data structure; solution.

Vector assigns for simplicity; it is ADT (Abstract Data Type/ Abstract Data Structure), interface; problem.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How would you find the size of an array in C++?

A

int sizeOfArray = sizeof(array)/sizeof(array[0])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain how you can pass an array to a function.

A

By reference or as a pointer;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is an array of pointers? Give an example of its usage.

A

Array of pointers has pointers as the elements. It can store for example objects

17
Q

How would you initialize an array of integers with all elements set to zero in C++?

A

int myArray[n] = {};
int* myArray = new intn;
delete[] myArray;

18
Q

What is the difference between passing an array to a function by value and by reference in C++?

A

By value, you will work on the copy of the array and by reference you will work on the actual elements allocated in fixed-size memory.

19
Q

Explain the concept of array decay in C++. When does it occur, and how does it affect function parameters?

A

You pass the first element of an array and the size of an array is lost. You can move from element to element, but you do not know the size.
Furthermore, you have to be aware of that problem, because you can leave the array.

20
Q

Discuss the time complexity of searching for an element in an unsorted array vs. a sorted array.

A

Unsorted: O(n)
Sorted: O(logn) (by binary search)

21
Q

What are the potential pitfalls of using arrays in C++?

A

Memory management

22
Q

How would you dynamically allocate memory for an array in C++? What considerations should be taken into account when using dynamic arrays?

A

int* dynArr = new int[10]; // Dynamic memory allocation
delete[] dynArr; // Freeing allocated memory

23
Q

Can you discuss some common algorithms or operations performed on arrays, such as sorting and traversing/searching?

A

traversing:
- linear
- reverse
- binary search

sorting:
- insert sort
- select sort
- bucket sort
- merge sort
- quick sort

24
Q

Which of the following statements correctly declares a dynamic array of integers in C++?

A) int* arr = new int[10];
B) int arr[10];
C) int* arr = new int(10);
D) int* arr = new int;

A

A

25
Q

Which of the following functions correctly finds the maximum element in an array myArray of integers in C++?

int myArray[] = {10, 20, 30, 40, 50};
A) int max = *std::max_element(myArray, myArray + 5);
B) int max = std::max(myArray);
C) int max = std::max_element(myArray, myArray + 5);
D) int max = *std::max(myArray);

A

A

26
Q

What is the time complexity of searching for an element in a sorted array of size n using binary search?

A) O(1)
B) O(n)
C) O(log n)
D) O(n log n)

A

C

27
Q

What happens if you attempt to access an element beyond the bounds of an array in C++?

A) The program throws a compilation error.
B) The program executes normally with no side effects.
C) The program may crash or exhibit undefined behavior.
D) The compiler automatically resizes the array.

A

C / garbage data

28
Q

Which statement correctly frees the memory allocated for a dynamic array arr in C++?
int* arr = new int[10];

A) free(arr);
B) delete arr;
C) delete[] arr;
D) delete[] arr[];

A

C

29
Q

What is the correct way to find the length of an array arr in C++?
int arr[] = {1, 2, 3, 4, 5};

A) int length = sizeof(arr);
B) int length = arr.size();
C) int length = sizeof(arr) / sizeof(arr[0]);
D) int length = arr.length();

A

C

30
Q

What is the difference in this two implementation for an array:
- malloc/free/realloc
- new/delete/for…loop

A

Malloc is better for smaller size array, for example of size 40, because malloc finds free bytes right after the allocated chunk of memory and in the difference to new it doesn’t have to always reallocate the full chunk of memory just add some additional space.

31
Q

What about making other data structures on dynamic array?

A

Do this but not directly, it means that you should use the same type of implementation but don’t use the other DS to build DS.
Just use the knowledge, not the notes.
And why? Because it is faster.