Metadata

  • Date :: 12-04-2025
  • Tags :: cpp

Notes

Detailed and Elaborative Notes on Arrays in C++

Arrays are one of the most essential data structures in C++ and are used to store multiple values under a single variable name. Each value within the array is called an element and is identified by an index number. Understanding arrays will help you store and manipulate collections of data efficiently.

1. What is an Array?

An array is a data structure that can hold multiple values of the same data type. Think of it like a parking garage where each parking spot (element) holds a car (value). The array provides a way to group and organize related data efficiently in memory, and you can access specific elements using an index.

For example:

  • If we need to store the names of cars, we can use an array to hold multiple car names, instead of creating multiple individual variables.

2. Declaring and Initializing an Array

  • Declaring a Single Variable: Normally, a variable holds only one value. For example, a single string variable can hold a name of a car:
string car = "Corvette";
cout << car;  // Output: Corvette
 
  • Declaring an Array: To turn the variable into an array that holds multiple values, use square brackets [] after the variable name. You also enclose the values within curly braces {} to initialize the array with multiple elements.
string cars[] = {"Corvette", "Mustang", "Camry"};
cout << cars;  // This outputs a memory address, not the values inside the array
 

This will not directly print the car names, but instead, it prints the memory address of the array.

3. Accessing Array Elements

  • Indexing: Each element of an array has an index number that allows us to access it. Indexing starts from 0, which means the first element of an array has an index of 0, the second element has an index of 1, and so on.
cout << cars[0];  // Output: Corvette
cout << cars[1];  // Output: Mustang
cout << cars[2];  // Output: Camry
 
  • Array Element Access: In the example above:
    • cars[0] refers to the first element (“Corvette”).
    • cars[1] refers to the second element (“Mustang”).
    • cars[2] refers to the third element (“Camry”).

4. Reassigning Array Elements

You can modify (reassign) the value of an element in an array by accessing the element using its index and assigning a new value:

cars[0] = "Camaro";  // Replaces "Corvette" with "Camaro"
cout << cars[0];     // Output: Camaro
 

Now, the first element in the array is “Camaro,” and if you print cars[0], you will get “Camaro” instead of “Corvette.”

5. Array Size and Homogeneity

  • Arrays Must Contain Values of the Same Type: One important property of arrays is that they can only store values of the same data type. For example, you cannot mix int and string types within the same array:
string cars[] = {"Corvette", "Mustang", 1};  // Error: Mixing string and int types
 

Arrays must store elements that are all of the same type, meaning the type must be consistent across all elements in the array.

  • Declaring Arrays Without Initializing: If you don’t know what values to place in the array at the time of declaration, you can declare the array and assign values later. However, you must still specify the size of the array during the declaration.
string cars[3];  // Declare an array of size 3
cars[0] = "Corvette";  // Assign values later
cars[1] = "Mustang";
cars[2] = "Camry";
 
  • Static Arrays: In C++, arrays are static, which means their size is fixed at the time of declaration. For example, an array of size 3 can only hold 3 values, and this size cannot be changed after the array is created.

6. Declaring Arrays with a Fixed Size

When declaring an array, you need to specify the size of the array, which is the number of elements it can hold. For example, if you want to store 3 car names in an array:

string cars[3];  // Declares an array that can hold 3 elements
 

This array can hold exactly 3 elements, and no more.

  • Assigning Values Later: After declaring an array with a fixed size, you can assign values to its elements later:
cars[0] = "Corvette";  // Assign "Corvette" to the first element
cars[1] = "Mustang";   // Assign "Mustang" to the second element
cars[2] = "Camry";     // Assign "Camry" to the third element
 

7. Example of Arrays with Multiple Data Types (not allowed)

C++ does not allow mixing different data types in an array. For example, trying to add a double to an array of strings will result in a compilation error:

string cars[] = {"Corvette", "Mustang", 1};  // Error: Cannot mix string and int
 

To store different types of values, you would need to use structures or other containers, like std::vector.

8. Using Arrays for Numeric Values

You can also store numbers in arrays. Here’s an example of using an array to store prices of cars:

double prices[] = {5.0, 7.50, 9.99, 15.0};  // Array of prices
 
  • Accessing Values in Numeric Arrays: You can access and display the individual prices in the array by referring to their indices:
cout << prices[0];  // Output: 5.0
cout << prices[1];  // Output: 7.50
cout << prices[2];  // Output: 9.99
cout << prices[3];  // Output: 15.0
 

9. Key Points to Remember

  • Arrays hold multiple values of the same type.
  • Indexing starts from 0 in C++, meaning the first element of the array has an index of 0.
  • The size of the array is fixed at declaration, and you cannot change the size of the array after it’s created (unless you use dynamic memory allocation).
  • You can reassign values to specific elements in the array.
  • Arrays are a simple yet powerful way to store and manage collections of data.

10. Conclusion

Arrays are essential in C++ programming, and they help store multiple related values in a single variable. Whether you’re working with strings, numbers, or other data types, arrays provide an efficient way to organize and access data by index.

Example Code:

#include <iostream>
using namespace std;
 
int main() {
    // Declaring and initializing an array of cars
    string cars[] = {"Corvette", "Mustang", "Camry"};
 
    // Accessing and printing array elements
    cout << "First car: " << cars[0] << endl;  // Output: Corvette
    cout << "Second car: " << cars[1] << endl; // Output: Mustang
    cout << "Third car: " << cars[2] << endl;  // Output: Camry
 
    // Modifying the first element
    cars[0] = "Camaro";
    cout << "Updated first car: " << cars[0] << endl;  // Output: Camaro
 
    // Declaring and displaying an array of prices
    double prices[] = {5.0, 7.50, 9.99, 15.0};
    for (int i = 0; i < 4; i++) {
        cout << "Price " << i << ": " << prices[i] << endl;
    }
 
    return 0;
}
 

This concludes the explanation and notes on arrays in C++. Arrays are a vital part of C++ programming and mastering them will help you handle larger sets of data more efficiently.


References