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:
#include <iostream>
: This line includes the input/output stream library, which provides functionalities for interacting with the console.using namespace std;
: This line brings all the elements of the standard namespace into scope, avoiding the need to writestd::
before elements likecout
andendl
.int main() { ... }
: This defines the main function where program execution begins.double averageTemperatures[12];
: This line declares an array namedaverageTemperatures
capable of storing 12 elements of typedouble
(for representing temperatures).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, soaverageTemperatures[0]
refers to the first element,averageTemperatures[1]
to the second, and so on.cout << "Monthly Average Temperatures:" << endl;
: This line prints a descriptive message to the console.for (int i = 0; i < 12; i++) { ... }
: Thisfor
loop iterates over each element of the array:int i = 0;
: Initializes the loop counteri
to 0.i < 12;
: Continues the loop as long as the loop counteri
is less than 12 (the number of elements in the array).i++
: Increments the loop counteri
after each iteration.
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.