Arrays Flashcards
What is an array in C++?
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 do you declare an array in C++?
int myArray[5];
How do you initialize an array in C++?
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
Explain the concept of contiguous memory allocation as it relates to arrays in C++
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.
Discuss the difference between arrays and pointers in C++
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.
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; }
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.
Explain the concept of array decay in C++ and provide an example
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;
}
Discuss the advantages and disadvantages of using arrays in C++ compared to other data structures.
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.
Analyze the time complexity of accessing, inserting, and deleting elements in an array in C++.
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.
Explain the difference between a static array and a dynamic array in C++
A dynamic array has a pointer to elements stored in a fixed-size memory, that can be dynamically resized as needed.
What is a multi-dimensional array?
Each element is identified by multi indexes; it is like a matrix that has elements in the table, which has rows and columns.
How does C++ handle array index out-of-bound errors?
You have to use static array with static size implementation, you can use also vector, that automatically manage memory.
Discuss the advantages and disadvantages of using arrays over vectors in C++
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 would you find the size of an array in C++?
int sizeOfArray = sizeof(array)/sizeof(array[0])
Explain how you can pass an array to a function.
By reference or as a pointer;
What is an array of pointers? Give an example of its usage.
Array of pointers has pointers as the elements. It can store for example objects
How would you initialize an array of integers with all elements set to zero in C++?
int myArray[n] = {};
int* myArray = new intn;
delete[] myArray;
What is the difference between passing an array to a function by value and by reference in C++?
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.
Explain the concept of array decay in C++. When does it occur, and how does it affect function parameters?
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.
Discuss the time complexity of searching for an element in an unsorted array vs. a sorted array.
Unsorted: O(n)
Sorted: O(logn) (by binary search)
What are the potential pitfalls of using arrays in C++?
Memory management
How would you dynamically allocate memory for an array in C++? What considerations should be taken into account when using dynamic arrays?
int* dynArr = new int[10]; // Dynamic memory allocation
delete[] dynArr; // Freeing allocated memory
Can you discuss some common algorithms or operations performed on arrays, such as sorting and traversing/searching?
traversing:
- linear
- reverse
- binary search
sorting:
- insert sort
- select sort
- bucket sort
- merge sort
- quick sort
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