Assume that empName and empID are two parallel arrays of siz

Assume that empName and empID are two parallel arrays of size numEmp that hold employee data. Write a pseudocode algorithm that sorts the empID array in ascending ID number order (using any sort you wish), such that the two arrays remain parallel. That is, after sorting, for all indexes in the arrays, empName [index] must still be the name of the employee whose ID is in empID[index].

Solution

Please find the required program along with its output. Please see the comments against each line to understand the step.


#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main()
{

string empName[] = {\"John\",\"Thomas\",\"George\",\"Mary\",\"Sarah\"};   //initialize empName array
int empId[] = {4,1,5,3,2};   //initialize empId array
  
int numEmp = 5, temp;
  
string str;
  
for(int i=0; i<numEmp; i++){   //outer loop which iterates numEmp times

for(int j=i; j<numEmp; j++){   //inner loop for comparision
  
if(empId[i]>empId[j]){   //check for bigger empId, if not in ascending order, just swap the two Ids positions also
  
temp = empId[i];
empId[i] = empId[j];
empId[j] = temp;

               swap(empName[i],empName[j]);   //swap corresponding empName also
}
}
  
}
  
  
cout << \"empId :\"<< flush;   //print empId array
  
for(int i=0; i<numEmp; i++){

cout << empId[i] << \" \" << flush;   
  
}
  
cout << \"\ empName :\"<< flush;   //print empName array
  
for(int i=0; i<numEmp; i++){

cout << empName[i] << \" \" << flush;   
  
}
cout << endl;


}

-----------------------------------------------------------------------------------

OUTPUT:

 Assume that empName and empID are two parallel arrays of size numEmp that hold employee data. Write a pseudocode algorithm that sorts the empID array in ascend
 Assume that empName and empID are two parallel arrays of size numEmp that hold employee data. Write a pseudocode algorithm that sorts the empID array in ascend

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site