What is an Array?
(Very Basic)
Array = collection of same type data stored together
Instead of writing:
int a, b, c, d, e;
We write:
int arr[5];
Why array?
Easy to manage large data
Less code
Fast access
Real-Life Example
Think of students’ marks:
Without array:
int m1, m2, m3, m4, m5;
With array:
int marks[5];
Each box stores one value.
How Array is Stored in Memory
Important interview point:
Array elements are stored in contiguous memory locations
Example:
int arr[5] = {10, 20, 30, 40, 50};
Index Value
0 10
1 20
2 30
3 40
4 50
Index always starts from 0
Declaring an Array(suntax)
dataType arrayName[size];
Example:
int arr[5];
Initializing an Array
Method 1
int arr[5] = {1, 2, 3, 4, 5};
Method 2 (auto size)
int arr[] = {10, 20, 30};
Method 3 (partial)
int arr[5] = {1, 2};
Output:
12000
Accessing Array Elements
cout << arr[0]; // first element
cout << arr[4]; // last element
arr[5] is INVALID (out of bounds)
What is Index in an Array?
An index is the position number of an element in an array.
Think of it like:
Index = Address (location) of an element inside the array.
Taking Input in Array (Very Important)
int arr[5];
for(int i = 0; i < 5; i++){
cin >> arr[i];
Printing outout in a array
for(int i = 0; i < 5; i++){
cout << arr[i] << " ";
Why Loop is Best Friend of Array
Because array size can be large (100, 1000, 1 lakh).
for(int i = 0; i < n; i++){
cout << arr[i];
Common Beginner Mistakes
Accessing out of range
arr[10] = 5; // wrong
Using garbage values
int arr[5];
cout << arr[0]; // garbage
Array & Address (Memory Concept)
cout << &arr[0]; // address of first element
Formula:
Address of arr[i] = base_address + (i * size_of_data_type)
What does “function with array” mean?
It means:
Passing an array to a function so the function can use or modify the array.
Example:
You have an array in main()
You send it to a function
Function prints it, modifies it, finds max, sum, etc.
Example 1: Passing array to function (Print array)
#include <iostream>
using namespace std;
void printArray(int arr[], int size) {
for(int i = 0; i < size; i++) {
cout << arr[i] << " ";
int main() {
int arr[5] = {10, 20, 30, 40, 50};
printArray(arr, 5); // function call
Output:
10 20 30 40 50
Important Concept (Behind the scenes)
When you pass array like this:
printArray(arr, 5);
Actually it behaves like:
printArray(&arr[0], 5);
That means:
Array is passed as address (pointer), not full copy.
So changes inside function affect original array.
Example 2: Function modifying array
#include <iostream>
using namespace std;
void update(int arr[], int size) {
arr[0] = 100; // change first element
int main() {
int arr[3] = {10, 20, 30};
update(arr, 3);
cout << arr[0]; // output will be 100
Output:
100
Why?
Because function got the same memory address of array.
Syntax to pass array to function
All are valid:
void fun(int arr[], int n)
void fun(int arr[5], int n)
void fun(int* arr, int n)
Internally all are treated as:
void fun(int* arr, int n)
Example 3: Sum of array using function
int sum(int arr[], int size) {
int s = 0;
for(int i = 0; i < size; i++) {
s += arr[i];
return s;
Call:
int arr[5] = {1,2,3,4,5};
cout << sum(arr, 5);
Output:
15
What is a Vector in C++?
A vector is a dynamic array provided by the C++ Standard Library.
That means:
It can grow and shrink in size automatically
You don’t need to fix the size in advance
It stores elements in continuous memory like arrays
You get many ready-made functions (push, pop, size, etc.)
You must include:
#include <vector>
Example:
vector<int> v = {10, 20, 30};
Array vs Vector (Simple Comparison)
| Feature | Array | Vector |
|--------|-------|
| Size | Fixed | Dynamic (can change) |
| Memory | Stack | Heap |
| Built-in functions | No | Yes (push_back, pop_back, etc.) |
| Safer | | |
| Preferred in modern C++ | Less | More |
How to Declare a Vector
vector<int> v; // empty vector
vector<int> v(5); // size 5, all values = 0
vector<int> v(5, 10); // size 5, all values = 10
vector<int> v = {1,2,3}; // initializer list
Common Functions of Vector (Very Important)
Function Meaning Example
push_back(x) Add element at end v.push_back(10)
pop_back() Remove last element v.pop_back()
size() Total elements [Link]()
empty() Check if empty [Link]()
clear() Remove all [Link]()
at(i) Access with safety [Link](2)
v[i] Fast access v[2]
Accessing Elements in Vector
vector<int> v = {5, 10, 15};
cout << v[0]; // 5
cout << v[1]; // 10
cout << v[2]; // 15
Loop through Vector
Using for loop:
for(int i = 0; i < [Link](); i++) {
cout << v[i] << " ";
Using for-each:
for(int x : v) {
cout << x << " ";