Incorrect output to array search

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.

Please help.

Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <utility>
#include <map>
#include <initializer_list>

using namespace 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 have already lost information when you try to find the indices after sorting.

Start with proBT[] = {9, 3, 6}, proBTIndex[] = {0, 1, 2}
Sort via indices to get: proBT[] = {9, 3, 6}, proBTIndex[] = {2, 0, 1}

Then create the sortedBT, if needed.
1
2
3
4
for ( int i = 0; i < SIZE; ++i ) {
  sortedBT[i] = proBT[ proBTIndex[i] ];
  cout << sortedBT[i] << '\n';
}

Thank you so much for that answer!

It makes so much sense.

Can you tell me how to sort the of the indices? I've never tried that before.
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 ] ]
Topic archived. No new replies allowed.