# Parallel Array in C++

The parallel array is also called the structure array.

Definition − A parallel array can be defined as multiple arrays in which the ith elements are closely related, and together, they constitute an entity. An array is a fundamental feature in the C++ language. Making parallel arrays helps us in comparing two or more arrays.

For instance,

first_name = ['John', 'Dexter', 'Fredd', 'Hank', 'james']
last_name = ['Jocab', 'Jonas', 'smith', 'lee', 'banner']
height = [160, 148, 231, 153, 162]

## Approach for Making a Parallel Array

Searching and sorting are some of the essential features required to form a parallel array.

Searching

Searching is based on specific values of an entity. For example, we need to find the address of a person whose height is less than 180cm. So we search the criteria of the height array having a value less than 180. Finally, when we get the results, we can print them.

We can follow these steps to perform searching.

• Search the required value in the respective array

• Store the index where the values are obtained.

• Print the values.

Sorting

sorting, we sort all the arrays with the same index and values. For example, we need to sort the height index in ascending order. So when we swap two heights, we also swap their values in other arrays. We can sort arrays in numerical or alphabetical ways.

We need to follow these steps to sort an array.

• Find the index in the array.

• Now swap the values of 2 calculated indices of all arrays.

Implementation

• The given code stores the name, second name, and height.

• The given code stores the name, second name, and height.

• We need to search for the name of the second tallest student, the third shortest student.

Then the student has a height equal to 158 cm in the record.

## Example

#include <iostream>
using namespace std;
int partition(string first_name[], string
last_name[],
int height[], int low, int high){
int pivot = height[high]; // pivot
int i = (low - 1); // Index of smaller element
for (int j = low; j <= high - 1; j++) {
if (height[j] <= pivot) {
i++;
string temp = first_name[i];
first_name[i] = first_name[j];
first_name[j] = temp;
temp = last_name[i];
last_name[i] = last_name[j];
last_name[j] = temp;
int temp1 = height[i];
height[i] = height[j];
height[j] = temp1;
}
}
string temp = first_name[i + 1];
first_name[i + 1] = first_name[high];
first_name[high] = temp;
temp = last_name[i + 1];
last_name[i + 1] = last_name[high];
last_name[high] = temp;
int temp1 = height[i + 1];
height[i + 1] = height[high];
height[high] = temp1;
return (i + 1);
}
void quickSort(string first_name[], string last_name[],
int height[], int low, int high){
if (low < high) {
int pi = partition(first_name, last_name, height, low, high);
quickSort(first_name, last_name, height, low, pi - 1);
quickSort(first_name, last_name, height, pi + 1, high);
}
}
void binarySearch(string first_name[], string
last_name[],
int height[], int value, int n){
int low = 0, high = n - 1;
int index;
while (low <= high) {
index = (high + low) / 2;
if (height[index] == 158) {
cout << "Person having height 158"
" cms is "
<< first_name[index]
<< " " << last_name[index] << endl;
return;
}
else if (height[index] > 158)
high = index - 1;
else
low = index + 1;
}
cout << "Sorry, no such person with"
" height 158 cms";
cout << "is found in the record";
}
void printParallelArray(string first_name[],
string last_name[], int height[], int n){
cout << "Name of people in increasing";
cout << "order of their height: " << endl;
for (int i = 0; i < n; i++) {
cout << first_name[i] << " "
<< last_name[i] << " has height "
<< height[i] << " cms\n";
}
cout << endl;
}
int main(){
int n = 4;
string first_name[] = { "John", "Dexter", "Fredd", "Hank", "james"};
string last_name[] = { "Jocab", "Jonas", "smith", "lee", "banner"};
int height[] = {160, 148, 231, 153, 162};
quickSort(first_name, last_name, height, 0, n - 1);
printParallelArray(first_name, last_name, height, n);
cout << "Name of the second tallest person" " is "
<< first_name[n - 2] << " "
<< last_name[n - 2] << endl;
cout << "Name of the third shortest person is "
<< first_name[2] << " " << last_name[2]
<< endl;
binarySearch(first_name, last_name, height, 158, n);
return 0;
}

## Output

Name of people in increasingorder of their height:
Dexter Jonas has height 148 cms
Hank lee has height 153 cms
John Jocab has height 160 cms
Fredd smith has height 231 cms

Name of the second tallest person is John Jocab
Name of the third shortest person is John Jocab
Sorry, no such person with height 158 cmsis found in the record

• In some cases, they can save a significant amount of space by avoiding alignment issues. Some architectures, for example, work best if 4-byte integers are always stored at memory locations that are multiples of 4. If the previous field was a single byte, it could throw 3 bytes away. Many modern compilers can automatically avoid such issues. Still, in the past, some programmers would explicitly declare fields in decreasing order of alignment constraints.

• When the number of items in an array is small, array indices can take up much less space than entire pointers, especially on some architectures.

• Examined in order

• Examining a single field of each record in the array sequentially is fast on modern machines because it amounts to a linear traversal of a single array with an ideal locality of reference and cache behavior.

• Because the various arrays can be stored randomly far apart, they have significantly worse locality of reference when trying to visit the records non-sequentially and examining multiple fields of each record.

• They obscure the connection between fields in a single record (e.g., no information relates to the index between them, which may be misused).

• They have little direct language assistance (the language and its syntax typically express no relationship between the arrays in the parallel array and cannot catch errors).

• Because collecting fields is not a "thing," passing it around is time-consuming and error-prone. For example, the function must accept the fields as separate arguments rather than calling a function to act on a single record (or structure or object). Many parameter lists must change when a new field is added or changed. In contrast, passing objects as a whole would avoid such changes entirely.

• They are costly to expand or contract because each of several arrays must be reallocated. Multi-level arrays can help with this problem, but they hurt performance due to the additional indirection required to find the desired elements.

## Conclusion

In this tutorial, we learned how to make a parallel array along with the c++ code. We can also write this code in java, python, and other languages. Arrays are one of the fundamental and most helpful features of the C++ programming language. They are used for various purposes, such as sorting and searching. We hope you find this tutorial helpful.