Introduction
One-dimensional arrays in C++ are used to store a sequence of elements of the same type in a linear structure. They provide a simple way to manage collections of data, such as lists of numbers or strings. Understanding how to use one-dimensional arrays is essential for efficient data manipulation and storage in your programs.
Declaring and Initializing One-Dimensional Arrays
Syntax for Declaring an Array
dataType arrayName[arraySize];
Example: Declaring and Initializing an Array
#include <iostream>
using namespace std;
int main() {
int numbers[5]; // Declares an array of 5 integers
// Initializing the array
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
// Accessing and printing the array elements
for (int i = 0; i < 5; i++) {
cout << "numbers[" << i << "] = " << numbers[i] << endl;
}
return 0;
}
Output
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
Explanation
int numbers[5];
declares an array namednumbers
of size 5.- Each element of the array is accessed and initialized using the index.
- The
for
loop is used to iterate through the array and print each element.
Initializing Arrays at Declaration
You can also initialize an array at the time of declaration.
Example: Initializing an Array at Declaration
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {10, 20, 30, 40, 50}; // Declares and initializes an array
// Accessing and printing the array elements
for (int i = 0; i < 5; i++) {
cout << "numbers[" << i << "] = " << numbers[i] << endl;
}
return 0;
}
Output
numbers[0] = 10
numbers[1] = 20
numbers[2] = 30
numbers[3] = 40
numbers[4] = 50
Explanation
int numbers[5] = {10, 20, 30, 40, 50};
declares and initializes the array in a single line.
Array Size
The size of an array must be a constant expression and cannot be changed once declared. Using the sizeof
operator, you can determine the size of the array.
Example: Determining the Size of an Array
#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, 20, 30, 40, 50}; // Array without explicitly specifying the size
int size = sizeof(numbers) / sizeof(numbers[0]); // Calculate the size of the array
cout << "The size of the array is: " << size << endl;
return 0;
}
Output
The size of the array is: 5
Explanation
int size = sizeof(numbers) / sizeof(numbers[0]);
calculates the number of elements in the array by dividing the total size of the array by the size of one element.
Example Programs
Example 1: Finding the Largest Element in an Array
#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, 25, 5, 30, 15};
int size = sizeof(numbers) / sizeof(numbers[0]);
int max = numbers[0]; // Assume the first element is the largest
for (int i = 1; i < size; i++) {
if (numbers[i] > max) {
max = numbers[i]; // Update max if the current element is larger
}
}
cout << "The largest element in the array is: " << max << endl;
return 0;
}
Output
The largest element in the array is: 30
Explanation
- The program initializes an array
numbers
and calculates its size. - It iterates through the array to find the largest element and prints it.
Example 2: Summing Elements of an Array
#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers) / sizeof(numbers[0]);
int sum = 0;
for (int i = 0; i < size; i++) {
sum += numbers[i]; // Add each element to sum
}
cout << "The sum of the elements in the array is: " << sum << endl;
return 0;
}
Output
The sum of the elements in the array is: 150
Explanation
- The program initializes an array
numbers
and calculates its size. - It iterates through the array to sum the elements and prints the total.
Example 3: Reversing an Array
#include <iostream>
using namespace std;
int main() {
int numbers[] = {10, 20, 30, 40, 50};
int size = sizeof(numbers) / sizeof(numbers[0]);
cout << "Original array: ";
for (int i = 0; i < size; i++) {
cout << numbers[i] << " ";
}
cout << endl;
// Reverse the array
for (int i = 0; i < size / 2; i++) {
int temp = numbers[i];
numbers[i] = numbers[size - 1 - i];
numbers[size - 1 - i] = temp;
}
cout << "Reversed array: ";
for (int i = 0; i < size; i++) {
cout << numbers[i] << " ";
}
cout << endl;
return 0;
}
Output
Original array: 10 20 30 40 50
Reversed array: 50 40 30 20 10
Explanation
- The program initializes an array
numbers
and calculates its size. - It iterates through the array to print the original elements.
- It reverses the array by swapping elements from the start with elements from the end.
- It iterates through the array again to print the reversed elements.
Conclusion
One-dimensional arrays in C++ provide a way to store and manage collections of data in a linear structure. This chapter covered how to declare, initialize, and access one-dimensional arrays. It also included example programs to demonstrate finding the largest element in an array, summing the elements of an array, and reversing an array. Understanding how to use one-dimensional arrays effectively will help you manage and process data efficiently in your programs. In the next chapter, we will explore multi-dimensional arrays in C++.