
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement Parallel Array
A parallel array is a structure that contains multiple arrays. Each of these arrays are of the same size and the array elements are related to each other. All the elements in a parallel array represent a common entity.
Here, we provide the basic example to understand the parallel array in C++ ?
employee_name = { Harry, Sally, Mark, Frank, Judy } employee_salary = {10000, 5000, 20000, 12000, 5000}
Here,
- The name and salary of 5 different employees is stored in two different arrays.
Example of Parallel Arrays
This is an implementation of a parallel array where we are calculating the highest salary from the given salaries of multiple employees. To store salaries and employee names, we used parallel arrays.
To find the highest salary among all array elements, start iterating through the salary array that helps to store the highest salary and index. Then it uses that index to fetch and show the corresponding employee name and department. Thus, this result the highest salary from the array elements.
#include <iostream> #include <string> using namespace std; int main() { int max = 0, index = 0; string empName [ ] = {"Harry", "Sally", "Mark", "Frank", "Judy" }; string empDept [ ] = {"IT", "Sales", "IT", "HR", "Sales"}; int empSal[ ] = {10000, 5000, 20000, 12000, 5000 }; int n = sizeof(empSal)/sizeof(empSal[0]); for(int i = 0; i < n; i++) { if (empSal[i] > max) { max = empSal[i]; index = i; } } cout << "The highest salary is "<< max <<" and is earned by " << empName[index] << " belonging to " << empDept[index] << " department"; return 0; }
Output
The above code produces the following result ?
The highest salary is 20000 and is earned by Mark belonging to IT department
Using a Function with Parallel Arrays
You can also use the function to handle parallel arrays. In the below example, we are using the parallel arrays in a separate function to get the index of the highest salary. Later then display the corresponding employee name, salary, and department using that index.
Example
Following is the illustration of parallel arrays using a separate function:
#include <iostream> #include <string> using namespace std; // finding the highest salary int findMaxIndex(int empSal[], int n) { int max = empSal[0], index = 0; for (int i = 1; i < n; ++i) { if (empSal[i] > max) { max = empSal[i]; index = i; } } return index; } int main() { string empName[] = {"Harry", "Sally", "Mark", "Frank", "Judy"}; string empDept[] = {"IT", "Sales", "IT", "HR", "Sales"}; int empSal[] = {10000, 5000, 20000, 12000, 5000}; int n = sizeof(empSal) / sizeof(empSal[0]); // pass the values int maxIndex = findMaxIndex(empSal, n); // display the output cout << "The highest salary is " << empSal[maxIndex] << " and is earned by " << empName[maxIndex] << " belonging to " << empDept[maxIndex] << " department."; return 0; }
Output
The above code produces the following result ?
The highest salary is 20000 and is earned by Mark belonging to IT department.