Bird
Raised Fist0
DSA C++programming

Array Declaration and Initialization in DSA C++

Choose your learning style10 modes available
Mental Model
An array is a row of boxes where each box holds a value and has a number to find it quickly.
Analogy: Think of a row of mailboxes, each with a number, where you can put or get letters easily by knowing the mailbox number.
[0] -> [1] -> [2] -> [3] -> [4]
  ↑     ↑     ↑     ↑     ↑
 val0 val1 val2 val3 val4
Dry Run Walkthrough
Input: Declare an array of size 5 and initialize it with values 10, 20, 30, 40, 50
Goal: Create an array with 5 boxes and fill each box with the given values
Step 1: Declare an array of size 5
[0] -> [1] -> [2] -> [3] -> [4]
  ↑     ↑     ↑     ↑     ↑
  ?     ?     ?     ?     ?
Why: We need to reserve space for 5 values
Step 2: Initialize index 0 with 10
[0] -> [1] -> [2] -> [3] -> [4]
  ↑     ↑     ↑     ↑     ↑
 10     ?     ?     ?     ?
Why: Set first box to 10
Step 3: Initialize index 1 with 20
[0] -> [1] -> [2] -> [3] -> [4]
  ↑     ↑     ↑     ↑     ↑
 10    20     ?     ?     ?
Why: Set second box to 20
Step 4: Initialize index 2 with 30
[0] -> [1] -> [2] -> [3] -> [4]
  ↑     ↑     ↑     ↑     ↑
 10    20    30     ?     ?
Why: Set third box to 30
Step 5: Initialize index 3 with 40
[0] -> [1] -> [2] -> [3] -> [4]
  ↑     ↑     ↑     ↑     ↑
 10    20    30    40     ?
Why: Set fourth box to 40
Step 6: Initialize index 4 with 50
[0] -> [1] -> [2] -> [3] -> [4]
  ↑     ↑     ↑     ↑     ↑
 10    20    30    40    50
Why: Set fifth box to 50
Result:
[0] -> [1] -> [2] -> [3] -> [4]
  ↑     ↑     ↑     ↑     ↑
 10    20    30    40    50
Annotated Code
DSA C++
#include <iostream>
using namespace std;

int main() {
    // Declare an array of size 5
    int arr[5];

    // Initialize each element
    arr[0] = 10; // set index 0 to 10
    arr[1] = 20; // set index 1 to 20
    arr[2] = 30; // set index 2 to 30
    arr[3] = 40; // set index 3 to 40
    arr[4] = 50; // set index 4 to 50

    // Print the array
    for (int i = 0; i < 5; i++) {
        cout << arr[i];
        if (i < 4) cout << " -> ";
    }
    cout << " -> null" << endl;

    return 0;
}
int arr[5];
Reserve space for 5 integer boxes
arr[0] = 10; // set index 0 to 10
Fill first box with 10
arr[1] = 20; // set index 1 to 20
Fill second box with 20
arr[2] = 30; // set index 2 to 30
Fill third box with 30
arr[3] = 40; // set index 3 to 40
Fill fourth box with 40
arr[4] = 50; // set index 4 to 50
Fill fifth box with 50
for (int i = 0; i < 5; i++) { cout << arr[i]; if (i < 4) cout << " -> "; }
Print each box value with arrows between
OutputSuccess
10 -> 20 -> 30 -> 40 -> 50 -> null
Complexity Analysis
Time: O(n) because we initialize and print each of the n elements once
Space: O(n) because we allocate space for n elements in the array
vs Alternative: Compared to dynamic structures like linked lists, arrays use fixed space and allow fast access by index but need size known upfront
Edge Cases
Array size zero
No space is allocated, and no elements can be stored or printed
DSA C++
for (int i = 0; i < 5; i++) { ... } // loop won't run if size is zero
Partial initialization
Uninitialized elements contain garbage values if not explicitly set
DSA C++
arr[0] = 10; ... arr[4] = 50; // all elements explicitly set here
When to Use This Pattern
When you need to store multiple values of the same type in a fixed order and access them by position, use array declaration and initialization.
Common Mistakes
Mistake: Forgetting to initialize all elements, leading to garbage values
Fix: Explicitly assign a value to each array index before use
Mistake: Accessing array index out of bounds
Fix: Always use indices from 0 up to size-1 to avoid invalid access
Summary
An array is a fixed-size collection of elements stored in contiguous memory.
Use arrays when you know the number of elements in advance and need fast access by index.
Remember that arrays reserve space upfront and each element must be initialized before use.

Practice

(1/5)
1. Which of the following is the correct way to declare an integer array of size 5 in C++?
easy
A. int arr = new int[5];
B. array<int> arr(5);
C. int arr[5];
D. int arr();

Solution

  1. Step 1: Understand basic array declaration syntax

    In C++, to declare a fixed-size array, we use the syntax type name[size];.
  2. Step 2: Check each option

    int arr[5]; uses int arr[5]; which is the correct syntax for declaring an integer array of size 5. int arr = new int[5]; uses dynamic allocation syntax which is valid but not a declaration. array<int> arr(5); uses a non-standard syntax. int arr(); declares a function, not an array.
  3. Final Answer:

    int arr[5]; -> Option C
  4. Quick Check:

    Array declaration = int arr[5]; [OK]
Hint: Use type name[size]; for fixed-size arrays [OK]
Common Mistakes:
  • Using parentheses instead of square brackets
  • Confusing dynamic allocation with declaration
  • Using non-standard syntax
2. Which of the following is the correct syntax to initialize an array of 3 integers with values 1, 2, and 3 in C++?
easy
A. int arr[3] = {1, 2, 3};
B. int arr = {1, 2, 3};
C. int arr[3] = (1, 2, 3);
D. int arr[3] = [1, 2, 3];

Solution

  1. Step 1: Recall array initialization syntax

    In C++, arrays are initialized using curly braces with values separated by commas, like int arr[size] = {values};.
  2. Step 2: Evaluate each option

    int arr[3] = {1, 2, 3}; correctly uses curly braces with values. int arr = {1, 2, 3}; misses the size in declaration. int arr[3] = (1, 2, 3); uses parentheses which is invalid. int arr[3] = [1, 2, 3]; uses square brackets which is incorrect for initialization.
  3. Final Answer:

    int arr[3] = {1, 2, 3}; -> Option A
  4. Quick Check:

    Array init uses curly braces = int arr[3] = {1, 2, 3}; [OK]
Hint: Initialize arrays with curly braces and commas [OK]
Common Mistakes:
  • Omitting array size in declaration
  • Using parentheses or square brackets for initialization
  • Missing commas between values
3. What will be the output of the following C++ code?
int arr[4] = {10, 20};
for(int i = 0; i < 4; i++) {
    std::cout << arr[i] << " ";
}
medium
A. 10 20 0 20
B. 10 20 0 0
C. 10 20 10 20
D. 10 20 garbage garbage

Solution

  1. Step 1: Understand partial array initialization

    When an array is partially initialized, the remaining elements are set to zero automatically in C++.
  2. Step 2: Trace the loop output

    The first two elements are 10 and 20 as given. The last two elements are 0 by default. So the output prints: 10 20 0 0
  3. Final Answer:

    10 20 0 0 -> Option B
  4. Quick Check:

    Partial init fills rest with 0 = 10 20 0 0 [OK]
Hint: Uninitialized array elements default to 0 if partially initialized [OK]
Common Mistakes:
  • Assuming uninitialized elements have garbage values
  • Confusing partial initialization with full initialization
  • Misreading loop bounds
4. Identify the error in the following C++ code snippet:
int arr[];
arr = {1, 2, 3};
medium
A. Array size must be specified during declaration
B. Array elements must be initialized with parentheses
C. Missing semicolon after array declaration
D. Cannot assign values to array after declaration like this

Solution

  1. Step 1: Check array declaration rules

    In C++, arrays must have size specified or be initialized at declaration. Here, int arr[]; is invalid because size or initializer is missing.
  2. Step 2: Understand assignment to arrays

    Arrays cannot be assigned values after declaration using = { ... }. This syntax is invalid. Initialization must happen at declaration.
  3. Final Answer:

    Cannot assign values to array after declaration like this -> Option D
  4. Quick Check:

    Arrays can't be assigned with braces after declaration [OK]
Hint: Initialize arrays only at declaration, no later assignment [OK]
Common Mistakes:
  • Trying to assign array values after declaration
  • Omitting array size without initializer
  • Confusing array assignment with pointer assignment
5. You want to declare and initialize a 2D integer array with 2 rows and 3 columns, where all elements are zero. Which is the best way to do this in C++?
hard
A. int arr[2][3] = {0};
B. int arr[2][3] = {{0,0,0},{0,0,0}};
C. int arr[2][3]; memset(arr, 0, sizeof(arr));
D. int arr[2][3] = {0, 0, 0, 0, 0, 0};

Solution

  1. Step 1: Understand zero initialization of multi-dimensional arrays

    In C++, initializing the first element to zero with {0} sets all elements to zero by default.
  2. Step 2: Compare options for simplicity and correctness

    int arr[2][3] = {0}; uses {0} which zero-initializes all elements. int arr[2][3] = {{0,0,0},{0,0,0}}; explicitly lists zeros but is longer. int arr[2][3]; memset(arr, 0, sizeof(arr)); uses memset which works but is less clean. int arr[2][3] = {0, 0, 0, 0, 0, 0}; lists all zeros but is verbose.
  3. Final Answer:

    int arr[2][3] = {0}; -> Option A
  4. Quick Check:

    Use {0} to zero initialize entire array [OK]
Hint: Use {0} to zero initialize all elements in arrays [OK]
Common Mistakes:
  • Not knowing {0} zero initializes entire array
  • Using memset without including <cstring>
  • Writing verbose initializers unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard