Arrays
Arrays
They are derived from the primitive datatypes. Derived data types include Arrays, Pointers,
References and Functions.
ARRAYS
Store multiple values in a single variable. All values should belong to the same datatype. Values
in arrays are place in consecutive memory location and have indices starts from zero. Arrays
index start from 0. The memory location in the array are known as elements and the total
number of elements in the array is called its length. Arrays are used to store large amount of
similar data. For example book is an example of array.
COPYING ARRAY
Copying an array in C++ involves creating a new array and then copying the elements from an
existing array into the new one. Arrays are fixed-size data structures, so you need to create a
new array with the desired size to hold the copied elements. There are different methods to
copy arrays, ranging from manual element-by-element copying to using library functions.
int main() {
return 0;
CHARACTER ARRAY
A character array in C++ is an array that holds a sequence of characters. It's often used to
represent strings of text. In C++, strings are actually implemented as character arrays. Each
character in the array corresponds to a single character in the text. The character array is
terminated with a special character called the null terminator, which has the value `'\0'` (ASCII
value 0).
Character arrays are declared by specifying the data type as `char` and specifying the size of the
array. Here's the basic syntax for declaring a character array:
char charArray[size];
```cpp
```cpp
#include <iostream>
int main() {
std::cout << "Message length: " << length << " characters" << std::endl;
return 0;
Keep in mind that character arrays have a fixed size, and you need to ensure that you don't
write beyond the allocated space, as this can lead to undefined behavior or memory corruption.
In modern C++, you may prefer using `std::string` from the `<string>` library, which provides a
more convenient and safer way to work with strings.
SEARCHING IN ARRAYS
Searching is a process of finding the required data in array. Searching becomes more important
when the length of array is very large.
Simple way to search the array for desired value. It follows the following steps
1. Visit the first element of the array and compare its value with the required value.
2. If the value of array matches with the desired value, the search is complete.
3. If the value of array doesnot match , move to next element and repeat same process.
Here's a simple C++ program that performs a linear search on an array without using a separate
function. The program searches for a target value in an array and prints whether the target
value is found or not.
```cpp
#include <iostream>
int main() {
int size;
int arr[size];
std::cout << "Enter " << size << " elements: ";
int target;
if (arr[i] == target) {
std::cout << "Target value found at index " << i << std::endl;
found = true;
if (!found) {
std::cout << "Target value not found in the array." << std::endl;
return 0;
In this program:
1. The user inputs the size of the array and its elements.
3. The program uses a loop to search for the target value in the array. If found, it prints the
index and exits the loop. If not found, it indicates that the target value was not found.
SORTING IN ARRAYS
Sorting is a process of arranging the values of array in particular order. An array can be sorted in
two orders. Ascending and Descending
BUBBLE SORT
Bubble Sort: Bubble sort compares adjacent elements and swaps them if they are in the wrong
order. It repeatedly goes through the array, swapping adjacent elements until the entire array
is sorted. While bubble sort is easy to grasp conceptually, its performance is not very efficient,
especially for larger arrays, as it involves a lot of unnecessary swaps.
#include <iostream>
int main() {
int size=10;
int arr[size];
arr[j + 1] = temp;
}
}
return 0;
In this program:
The user inputs the size of the array and its elements.
The program uses a nested loop to repeatedly compare adjacent elements and swap
them if they are in the wrong order.
After the sorting is complete, the program outputs the sorted array.
MULTIDIMENSIONAL ARRAY
TWO DIMENSIONAL
Can be considered as a table that consists of rows and column. Each element in 2D array is
referred with the help of two indexes. One index for row and other for column.
In order to access each element of array two for loops are used.
ARRAY MANIPULATION
Array manipulation in programming involves performing various operations on arrays to modify
their contents, rearrange elements, or extract specific information. Here are some common
array manipulation tasks and how they can be performed:
To add elements to an array, you typically need to create a new array with a larger size, copy
the existing elements, and then add the new elements.
```cpp
int newSize = 5;
int newArray[newSize];
newArray[i] = originalArray[i];
```
To remove elements from an array, you can copy the elements excluding the one(s) you want
to remove into a new array.
```cpp
int newSize = 4;
int newArray[newSize];
int indexToRemove = 2;
if (i == indexToRemove) {
newArray[i] = originalArray[j];
```
Iterate through the array while keeping track of the maximum and minimum values.
```cpp
int size = 5;
maxVal = arr[i];
}
minVal = arr[i];
}}```
```cpp
int size = 5;
arr[size - 1 - i] = temp;
```