2017-04-24 119 views
-1

我想比較矩陣中每行的元素,從最小到最大。 但我不想打印排序後的數組我想打印原始位置。C++比較數組中的元素並打印位置

0  11.80 79.34 78.23 
11.80 0  65.23 45.19 
79.34 65.23 0  90.27 
78.23 45.19 90.27 0 

在這個矩陣的第一行,我想打印1,2,4,3

到目前爲止我的代碼:

#include <iostream> 
#include <fstream> 
#include <iomanip> 

using namespace std; 

int main() { 

    string dummy; 
    double myArray[4][4]; 
    int i; 
    int j; 
    int y; 


    ifstream infile("dist.dat"); 

    cout << "Open file " << "dist.dat" <<" for reading." << endl; 

    for (i = 0; i < 4; i++) { 
    for (j = 0; j < 4; j++) { 
     infile >> myArray[i][j]; 
     if (!infile) { 
     cout << "***There was a problem trying to read element [" << i << "][" << j << "]" << endl; 
     return 0; 
     } 
    } 
    } 
    infile.close(); 

    cout << "Here's the array from the file" << endl; 
    cout << fixed << setprecision(2); 
    for (i = 0; i < 4; i++) { 
    for (j = 0; j < 4; j++) { 
     cout << setw(10) << myArray[i][j]; 
    } 
    cout << endl; 
    } 
    cout << endl; 
    int x = myArray[i][j]; 
    for (i = 0; i < 4; i++) { 
    for (j = 0; j < 4; j++) { 
     if(myArray[i][j] >= x) { 
     x = j; 
     } 
     else { 
     x = j + 1; 
     } 
    } 
    cout << x << endl; 
    } 
    return 0; 
} 
+0

而你的問題是什麼? – Ceros

+0

如何打印排序的位置 –

+0

請參見[創建已排序向量的索引向量](http://stackoverflow.com/questions/25921706/creating-a-vector-of-indices-of-a-sorted-vector ),[C++排序跟蹤指數](http://stackoverflow.com/questions/10580982/c-sort-keeping-track-of-indices),[C++排序和跟蹤索引](http:// stackoverflow.com/questions/1577475/c-sorting-and-keeping-track-of-indexes)等 –

回答

0

你需要保持它具有的指標另一個矩陣每行,然後對其應用相同的操作,以便在原始矩陣的每一行上應用它們進行排序。

下面是代碼:

#include <iostream> 
#include <fstream> 
#include <iomanip> 

using namespace std; 

void swap(int &x, int &y) 
{ 
int temp = x; 
x = y; 
y = temp; 
} 

void swap(double &x, double &y) 
{ 
double temp = x; 
x = y; 
y = temp; 
} 

int main() 
{ 
    string dummy; 
    double myArray[4][4]; 
    int i; 
    int j; 
    int y; 
    int k; 

    ifstream infile("dist.dat"); 

    cout << "Open file " << "dist.dat" <<" for reading." << endl; 

    for (i = 0; i < 4; i++) 
    { 
    for (j = 0; j < 4; j++) 
    { 
     infile >> myArray[i][j]; 
     if (!infile) 
     { 
     cout << "***There was a problem trying to read element [" << i << "][" << j << "]" << endl; 
     return 0; 
     } 
    } 
    } 

    infile.close(); 

    cout << "Here's the array from the file" << endl; 
    cout << fixed << setprecision(2); 

    for (i = 0; i < 4; i++) 
    { 
    for (j = 0; j < 4; j++) 
    { 
     cout << setw(10) << myArray[i][j]; 
    } 
    cout << endl; 
    } 

    cout << endl; 

    int sortedIndices[4][4]; 

    for (i = 0; i < 4; i++) 
    { 
    for (j = 0; j < 4; j++) 
    { 
    sortedIndices[i][j] = j+1; 
    } 
    } 

    int x; 

    for (i = 0; i < 4; i++) 
    { 
    for (j = 0; j < 4; j++) 
    { 
    x = j; 

    for(k = j+1; k < 4; k++) 
    { 
     if(myArray[i][k] < myArray[i][x]) 
     { 
      x = k; 
     } 
    } 

    swap(sortedIndices[i][j], sortedIndices[i][x]); 
    swap(myArray[i][j], myArray[i][x]); 
    } 
    } 

    for (i = 0; i < 4; i++) 
    { 
    for (j = 0; j < 4; j++) 
    { 
    cout << setw(10) << sortedIndices[i][j]; 
    } 

    cout<<endl; 
    } 

    return 0; 
}