C++ Array Structure Example

5 min read Jul 05, 2024
C++ Array Structure Example

C++ Array Structure Example

In C++, arrays are a fundamental data structure used to store a collection of elements of the same data type. Arrays can be used to represent various data structures like vectors, matrices, and even other arrays.

Here's an example of using an array to represent the monthly average temperatures for a year:

#include 

using namespace std;

int main() {
  // Declare an array of 12 elements (one for each month)
  double averageTemperatures[12];

  // Assign values to the array
  averageTemperatures[0] = 10.5;
  averageTemperatures[1] = 12.8;
  averageTemperatures[2] = 15.2;
  averageTemperatures[3] = 18.7;
  averageTemperatures[4] = 22.1;
  averageTemperatures[5] = 25.6;
  averageTemperatures[6] = 27.3;
  averageTemperatures[7] = 26.9;
  averageTemperatures[8] = 24.4;
  averageTemperatures[9] = 20.8;
  averageTemperatures[10] = 17.5;
  averageTemperatures[11] = 13.2;

  // Display the average temperatures for each month
  cout << "Monthly Average Temperatures:" << endl;
  for (int i = 0; i < 12; i++) {
    cout << "Month " << i + 1 << ": " << averageTemperatures[i] << " degrees Celsius" << endl;
  }

  return 0;
}

Explanation:

  1. #include <iostream>: This line includes the input/output stream library, which provides functionalities for interacting with the console.
  2. using namespace std;: This line brings all the elements of the standard namespace into scope, avoiding the need to write std:: before elements like cout and endl.
  3. int main() { ... }: This defines the main function where program execution begins.
  4. double averageTemperatures[12];: This line declares an array named averageTemperatures capable of storing 12 elements of type double (for representing temperatures).
  5. averageTemperatures[0] = 10.5; ... averageTemperatures[11] = 13.2;: These lines assign the average temperature values for each month to the corresponding array elements. Remember, array indexing starts from 0, so averageTemperatures[0] refers to the first element, averageTemperatures[1] to the second, and so on.
  6. cout << "Monthly Average Temperatures:" << endl;: This line prints a descriptive message to the console.
  7. for (int i = 0; i < 12; i++) { ... }: This for loop iterates over each element of the array:
    • int i = 0;: Initializes the loop counter i to 0.
    • i < 12;: Continues the loop as long as the loop counter i is less than 12 (the number of elements in the array).
    • i++: Increments the loop counter i after each iteration.
  8. cout << "Month " << i + 1 << ": " << averageTemperatures[i] << " degrees Celsius" << endl;: Inside the loop, this line prints the month number (starting from 1) and the corresponding average temperature value from the array.

Key points:

  • Arrays are fixed-size data structures, so their size must be declared at compile time.
  • Elements in an array are accessed using their index, starting from 0.
  • Arrays can be used to represent various data structures, making them versatile for data storage and manipulation.

This simple example illustrates the basic usage of arrays in C++ to store and access data. You can build upon this foundation to implement more complex data structures and algorithms.