TOPIC:ARRAY
Introduction to Arrays
Definition and Brief Explanation:
•An array is a data structure that can hold a fixed number of elements of the same type.
•Arrays allow multiple values to be stored in a single variable, instead of declaring separate
variables for each value.
Why Use Arrays:
•Efficiency: Arrays store data in contiguous memory locations, allowing for quick access
and manipulation.
•Organized Storage: Arrays help organize large amounts of data under a single variable
name.
•Indexed Access: Elements in an array are accessed using an index, which simplifies data
retrieval and manipulation.
Example:
int numbers[5]; // Declares an array of 5 integers
Declaration and Initialization of Arrays
Declaration:
•Arrays must be declared before they can be used.
•Syntax: type array Name[array Size];
•Example: int scores[10];
Initialization:
•Arrays can be initialized at the time of declaration.
•If not explicitly initialized, array elements contain garbage values (random data).
Example:
numbers[5] = {1, 2, 3, 4, 5}; // Initializes the array with specific
values int values[5] = {0}; // Initializes all elements to 0
Properties of Arrays:
Fixed Size: The size of an array is fixed at the time of declaration
and cannot be changed.
Homogeneous Elements:
All elements in an array are of the same type.
Indexed Access: Elements are accessed using an index, starting
from 0.
Contiguous Memory Allocation: Elements are stored in
contiguous memory locations, which allows efficient access and
manipulation.
Program 1:
Declaration and Initialization at Declaration
Program 2:
Declaration and Initialization after Declaration
One-Dimensional Arrays
Definition:
A one-dimensional array is a list of elements stored in
contiguous memory locations and can be accessed using
a single index.
Declaring One-Dimensional Arrays:
Syntax: type arrayName[arraySize];
float temperatures[7]; // Declares an array of 7 floats
Initializing One-Dimensional Arrays:
Arrays can be initialized when they are declared
float temperatures[7] = {98.6, 99.1, 98.9, 98.4, 99.0, 98.7,
99.2}; // Initializes the array
Program 1: Declaring and Initializing a One- Program 2: Modifying and Accessing Elements
Dimensional Array in a One-Dimensional Array
Accessing Individual Elements
Using Array Index:
Each element in an array is accessed using an index.The index is a
number that represents the position of the element in the array.
Array indexing starts at 0.
For example,
in an array ages[5], the first element is ages[0],
the second element is ages[1], and so on.To access an element,
use the syntax arrayName[index].
Example:
Modifying Elements:
You can change the value of an array element by
accessing it with its index and assigning a new value.
This does not change the size of the array; it only
updates the value at the specified index.
Why Loop Through Arrays: Example Program:
•Looping through an array allows you to perform operations on Accessing and Modifying Array Elements
each element, such as printing values, calculating sums, finding
maximum/minimum values, etc.
•Using loops with arrays is efficient because arrays store
elements in contiguous memory locations.
Types of Loops for Arrays:
•For Loop: The most common way to traverse an array.
The loop runs from the first index (0) to the last index
(arraySize - 1).
•While Loop: Can also be used, though less common for
arrays.
•Range-Based For Loop (C++11 and later): Simplifies
loop syntax and improves readability.
Array Operators
Common Array Operations:
Assignment:
You can assign values to array elements individually after
declaration or initialization.Each element can be
accessed using its index and assigned a value.
Example:
int arr[3];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
Access and Modification:
Accessing and modifying elements enable dynamic changes to
data during program execution.
Traversal:
Looping through arrays facilitates processing of multiple
elements efficiently, such as calculating sums or finding
specific values.
Two-Dimensional Arrays
Example Program:
Definition: C++ program that defines and initializes a 2x2 matrix:
A two-dimensional array is an array of arrays, where
each element itself is an array. It can be visualized as a
matrix or table.
Declaration of Two-Dimensional Arrays:
Syntax: type arrayName[rows][columns];
int matrix[3][4]; // Declares a 2D array with 3 rows and 4
columns
Initialization of Two-Dimensional Arrays:
Use row and column indices to access specific elements
Example Program:
int value = matrix[0][2]; How does it find the highest value
Applications and Uses of Arrays in C++
Applications: Example Program
Storing Data Collections:
Arrays are essential for organizing data by storing multiple
values of the same type sequentially in memory. They are used
for storing diverse data like student grades, employee salaries,
or temperature records.
Data Manipulation:
Arrays enable efficient data manipulation through loops, making
operations such as calculating sums, averages, or locating
specific values straightforward and efficient.
Algorithms:
Arrays are pivotal in implementing algorithms like sorting (e.g.,
Bubble Sort, Quick Sort) and searching (e.g., Linear Search,
Binary Search), leveraging efficient element access and
manipulation.
Buffer Storage:
Arrays serve as buffers in applications handling continuous data
streams (e.g., audio or video processing), temporarily storing
data chunks for seamless processing and output continuity.