-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patharrays_program.cpp
More file actions
33 lines (26 loc) · 872 Bytes
/
arrays_program.cpp
File metadata and controls
33 lines (26 loc) · 872 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
int main() {
// Declare and initialize an integer array
int numbers[5] = {10, 20, 30, 40, 50};
// Access and print elements of the array
std::cout << "Elements in the array:" << std::endl;
for (int i = 0; i < 5; ++i) {
std::cout << numbers[i] << " ";
}
std::cout << std::endl;
// Modify an element of the array
numbers[1] = 25;
// Access and print elements using a range-based for loop (C++11 and later)
std::cout << "Elements in the array (using range-based for loop):" << std::endl;
for (const auto& num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
// Calculate the sum of the elements
int sum = 0;
for (int i = 0; i < 5; ++i) {
sum += numbers[i];
}
std::cout << "Sum of the elements: " << sum << std::endl;
return 0;
}