-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;
}
而你的問題是什麼? – Ceros
如何打印排序的位置 –
請參見[創建已排序向量的索引向量](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)等 –