Lesson-week-7
Lesson-week-7
variable.
It is used to manage collections of data more efficiently than using individual variables for each value. Arrays are
particularly useful when you need to store large amounts of data or need to access and manipulate the data in a
repetitive manner.
Key Concepts of Arrays
Definition
An array is a collection of elements (values) that are stored in contiguous memory locations. All elements in an array
must be of the same data type (e.g., integers, floats, characters). Each element can be accessed individually using
an index.
Array Indexing
Arrays use zero-based indexing, meaning that the index of the first element is 0, the second element is 1, and so on.
For an array of size n, the index values will range from 0 to n-1.
Example:
int numbers [5] = {10, 20, 30, 40, 50};
numbers [0] is 10
numbers [1] is 20
numbers [4] is 50
Declaring an Array
An array must be declared with a specific size, which determines how many elements it can store. The size of an
array cannot be changed once it has been declared.
data_type array_name[array_size];
Example:
int scores[10]; // Array to store 10 integers
float temperatures[5]; // Array to store 5 floating-point numbers
Initializing an Array
You can initialize an array at the time of declaration by providing a list of values. If you don’t initialize all elements, the
remaining elements are automatically set to zero (for numeric data types).
Example:
int numbers[5] = {1, 2, 3}; // First three elements are 1, 2, 3, remaining are set to 0
Accessing Array Elements
Array elements are accessed using their index. To retrieve or modify the value of an element, use the array name
followed by the index in square brackets ([]).
Example:
int numbers[5] = {10, 20, 30, 40, 50};
cout << numbers[2]; // Outputs 30
numbers[0] = 100; // Changes the first element to 100
Array Size
The size of an array is defined at the time of its declaration and cannot be changed dynamically. To determine the
size of an array, you can use the size of operator.
Example:
int numbers[5];
int size = sizeof(numbers) / sizeof(numbers[0]); // Calculates the number of elements in the array
Advantages of Arrays
Efficiency: Arrays allow you to store and manipulate multiple values using a single variable.
Easy Access: You can access and modify any element in constant time using its index.
Simplicity: Arrays provide a simple way to work with lists of related values, such as student grades, temperatures, or
sales figures.
Practical Example
Here’s a simple example demonstrating how to declare, initialize, and access array elements in a C++ program:
#include <iostream>
using namespace std;
int main() {
// Declare and initialize an array of integers
int numbers[5] = {10, 20, 30, 40, 50};
return 0;
}
Output:
Element at index 0: 10
Element at index 1: 20
Element at index 2: 30
Element at index 3: 40
Element at index 4: 50
Modified array:
10 20 100 40 50
In this example, the array numbers holds 5 integer values, which are accessed using a for loop. One element is
modified, and the new values are displayed.
Conclusion
Arrays in C++ are fundamental data structures that allow for efficient storage and manipulation of multiple values.
They are essential for building more complex programs that deal with large datasets, and they lay the groundwork for
other data structures like matrices, lists, and tables.