In this program I have 2 arrays, proBT[], which is unsorted and sortedBT[]. Both have the exact same elements. I'm trying to map the indices of proBT[] elements to the matching elements of sortedBT[] in a 3rd array, proBTIndex[]. In other words if proBT[] = {9, 3, 6}, sortedBT would look like sortedBT[] = {3, 6, 9}, and proBTIndex[] = {1, 2, 0}.
The indexing portion of my code begins at line 59, it is the only portion that isn't working.
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <utility>
#include <map>
#include <initializer_list>
usingnamespace std;
int main (){
int SIZE = 3;
int AT = 0;// AT = arrival time
int proBT[SIZE], BT, proPrty[SIZE], priority;//BT = burst time
/*output to screen "Collecting Data*/
cout <<"\n\n\t\t Collecting Data\n\n"
<<"Each Process Burst Time:"<<"\n\n";
for(int i = 0; i < SIZE; i++) {
cout <<"Enter process P" <<i<< " burst time in milliseconds: ";
cin >>BT;
if(BT < 1){
cout <<"\nThe burst time must be at least 1 millisecond."<<"\n";
cout <<"Try again."<<"\n";
i = 0;
cout <<"\nEnter process P" <<i<< " burst time in milliseconds: ";
cin >>BT;
}//check that the BT is not less than 1
proBT[i] = BT;
}//burst time for loop
/*make a copy of ProBT array for sorting for shortest job 1st*/
int sortedBT[SIZE];
for(int i = 0; i < SIZE; i++){
sortedBT[i] = proBT[i];
}//for Luke to copy burst time array
/*BT sorting*/
for(int i = 0; i < SIZE; i++){
int j = i;
while(j > 0 && sortedBT[j] < sortedBT[j - 1]) {
int hold = sortedBT[j];
sortedBT[j] = sortedBT[j - 1];
sortedBT[j - 1] = hold;
j--;
}//while
}//BT sorting
for(int i = 0; i < SIZE; i++){
cout <<proBT[i]<<"\n";
}
for(int i = 0; i < SIZE; i++){
cout <<sortedBT[i]<<"\n";
}
/*indexing the sorted array*/
int proBTIndex[SIZE];
for(int i = 0; i < SIZE; i++) {
for(int j = 0; j < SIZE; j++){
if(proBT[i] == sortedBT[j]) {
break;
}//if to find the index number of the original array
proBTIndex[j] = i;
j++;
}
i++;
}
cout <<"\n";
/*printing the indexed array*/
for(int i = 0; i < SIZE; i++){
cout <<proBTIndex[i]<<"\n";
}
}// main
You will sort the array proBTIndex. You have to swap, when values are not it correct order. You had that test on line 42. Perhaps proBT[ proBTIndex[j] ] < proBT[ proBTIndex[ j-1 ] ]