0% found this document useful (0 votes)
10 views

Arrays

Uploaded by

comedystar294
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Arrays

Uploaded by

comedystar294
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ARRAYS

DERIVED DATA TYPES

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.

Array declaration type name [size]


int roll_num[5];

Array initialization roll_num[5] = {101,102,103,104,105};


roll_num = {101,102,103,104,105};
roll_num [0] = 101;
roll_num[1]=102; …..

Array access = roll_num[2]= value displayed will be 103

We can use loop to access each value of an 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.

Here's a basic example of manually copying an array using a loop:


#include <iostream>

int main() {

int originalArray[] = {1, 2, 3, 4, 5};

int newArray[5]; // Create a new array with the same size

for (int i = 0; i < 5; i++) {

newArray[i] = originalArray[i]; // Copy each element

// Display the contents of the new array

for (int i = 0; i < 5; i++) {

std::cout << newArray[i] << " ";

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

char greeting[] = "Hello, world!";


Character arrays can be manipulated using various string functions from the `<cstring>` or
`<string>` header, such as `strlen`, `strcpy`, `strcat`, and so on.

Here's an example of using a character array:

```cpp

#include <iostream>

#include <cstring> // Include the header for string functions

int main() {

char message[] = "Hello, there!";

std::cout << "Original message: " << message << std::endl;

// Modify the message

message[7] = 'X'; // Change 't' to 'X'

std::cout << "Modified message: " << message << std::endl;

// Calculate and display the length of the message

int length = strlen(message);

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.

LINEAR SEARCH/SEQUENTIAL /SERIAL SEARCH

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;

std::cout << "Enter the size of the array: ";

std::cin >> size;

int arr[size];

std::cout << "Enter " << size << " elements: ";

for (int i = 0; i < size; i++) {

std::cin >> arr[i];

int target;

std::cout << "Enter the target value to search: ";


std::cin >> target;

bool found = false;

for (int i = 0; i < size; i++) {

if (arr[i] == target) {

std::cout << "Target value found at index " << i << std::endl;

found = true;

break; // Exit the loop once the target is found

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.

2. The user inputs the target value to search for.

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];

for (int i = 0; i < size; i++) {

std::cin >> arr[i];

// Bubble sort algorithm

for (int i = 0; i < size - 1; i++) {

for (int j = 0; j < size - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

// Swap elements if they are in the wrong order

int temp = arr[j];

arr[j] = arr[j + 1];

arr[j + 1] = temp;

}
}

std::cout << "Sorted array: ";

for (int i = 0; i < size; i++) {

std::cout << arr[i] << " ";

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.

type array_name [row_size][column_size]

int x_2d[3][4]= { {101,102,103,104},


{105,106,107,108},
{109,110,111,112 } }

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:

**1. Adding Elements:**

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 originalArray[] = {1, 2, 3, 4};

int newSize = 5;

int newArray[newSize];

for (int i = 0; i < newSize - 1; i++) {

newArray[i] = originalArray[i];

newArray[newSize - 1] = 5; // Add new element

// Now newArray contains {1, 2, 3, 4, 5}

```

**2. Removing Elements:**

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 originalArray[] = {1, 2, 3, 4, 5};

int newSize = 4;
int newArray[newSize];

int indexToRemove = 2;

for (int i = 0, j = 0; i < newSize; i++, j++) {

if (i == indexToRemove) {

j++; // Skip the element to remove

newArray[i] = originalArray[j];

// Now newArray contains {1, 2, 4, 5}

```

**3. Finding Maximum and Minimum:**

Iterate through the array while keeping track of the maximum and minimum values.

```cpp

int arr[] = {4, 7, 2, 9, 1};

int size = 5;

int maxVal = arr[0];

int minVal = arr[0];

for (int i = 1; i < size; i++) {

if (arr[i] > maxVal) {

maxVal = arr[i];
}

if (arr[i] < minVal) {

minVal = arr[i];

}}```

**4. Reversing an Array:**

Swap elements from both ends of the array to reverse it.

```cpp

int arr[] = {1, 2, 3, 4, 5};

int size = 5;

for (int i = 0; i < size / 2; i++) {

int temp = arr[i];

arr[i] = arr[size - 1 - i];

arr[size - 1 - i] = temp;

```

You might also like