Question: Hello C++ I have this code that takes 5 files, source1.txt-source5.txt each is a 1x10000 file with numbers 1-10000 for a ranking I created code
Hello
C++
I have this code that takes 5 files, source1.txt-source5.txt each is a 1x10000 file with numbers 1-10000 for a ranking
I created code to assign a page number in a second row for each rank in the source1.txt-source5.txt files
I created the code to make a 6th file called combRank, this file combines the ranks from each source by each row.
EX:
| 123 | 4532 | 3241 | 231 | 2313 | 10440 |
| 324 | 2343 | 4453 | 42 | 4211 | 11373 |
| 2421 | 2344 | 1242 | 21 | 4214 | 10242 |
I defined a quicksort algorithm to find the ascending ordering of best rank (lowest value) to highest value.
This combRank array also has a assigned page number for the sorted algorithm.
These ranks were sorted with the quicksort and like I said in ascending order, with a specific page number for that rank
*******************
I need help creating an algorithm to sort each source by how the combRank is sorted by page number in right column
Then creating an algorithm to find the number of inversions when each source sorts themselves to how their ranks are order in ascending
*******************
Here is the code I have
#include
void swap(int* a, int* b) { int x = *a; *a = *b; *b = x; }
int partition(int array[][2], int low, int high) { int pivot = array[high][0]; int i = (low - 1); for (int j = low; j <= high - 1; j++) { if (array[j][0] < pivot) { i++; swap(&array[i][0], &array[j][0]); swap(&array[i][1], &array[j][1]); } } swap(&array[i + 1][0], &array[high][0]); swap(&array[i + 1][1], &array[high][1]); return (i + 1); }
void quickSort(int array[][2], int low, int high) { if (low < high) { int pi = partition(array, low, high); quickSort(array, low, pi - 1); quickSort(array, pi + 1, high); } }
int main() { cout << "Hello World! "; int x;
int source1[10000][2]; //each source array holds 10000 ranks and 10000 page numbers int source2[10000][2]; int source3[10000][2]; int source4[10000][2]; int source5[10000][2]; int combRank[10000][2]; //Combined rank array ifstream file1("source1.txt"); int cnt= 0; //counts how many times through while loop while (file1 >> x) //Series of while loops that load a seperate array for each source file with data { source1[cnt][0] = x; source1[cnt][1] = cnt+1; cnt++; } file1.close();
ifstream file2("source2.txt"); cnt= 0; while (file2 >> x) { source2[cnt][0] = x; source2[cnt][1] = cnt+1; cnt++; } file2.close();
ifstream file3("source3.txt"); cnt= 0; while (file3 >> x) { source3[cnt][0] = x; source3[cnt][1] = cnt+1; cnt++; } file3.close();
ifstream file4("source4.txt"); cnt= 0; while (file4 >> x) { source4[cnt][0] = x; source4[cnt][1] = cnt+1; cnt++; } file4.close();
ifstream file5("source5.txt"); cnt= 0; while (file5 >> x) { source5[cnt][0] = x; source5[cnt][1] = cnt+1; cnt++; } file5.close();
//couts to test make sure arrays work correctly cout << " source array test "; cout << source1[9999][0] << " : " << source1[9999][1] <<" "; cout << source2[2][0] << " : " << source2[2][1] <<" "; cout << source3[0][0] << " : " << source3[0][1] <<" "; cout << source4[0][0] << " : " << source4[0][1] <<" "; cout << source5[0][0] << " : " << source5[0][1] <<" ";
for (int i = 0; i < 10000; i++) //for loop that sums each rank from source arrays and stores them into the combined rank array { int rankSum = source1[i][0] + source2[i][0] + source3[i][0] + source4[i][0] + source5[i][0]; combRank[i][0] = rankSum; combRank[i][1] = i + 1; } cout << " Comb Rank array test "; cout << combRank[0][0] << " : " << combRank[0][1] <<" "; cout << combRank[1][0] << " : " << combRank[1][1] <<" "; cout << combRank[2][0] << " : " << combRank[2][1] <<" "; cout << combRank[3][0] << " : " << combRank[3][1] <<" "; cout << combRank[4][0] << " : " << combRank[4][1] <<" ";
quickSort(combRank, 0, 10000);
for (int i = 0; i < 10000; i++) //prints entire combrank array for testing { cout << combRank[i][0] << "\t\t:" << combRank[i][1] << " "; } cout << " "; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
