hi everyone,
I need to write a piece of code that sort an array of double values named distance in the following code. The counter i is the index for another array as well therefore I don't want to change the order of the array. I got hint from this post:
http://www.daniweb.com/software-development/cpp/threads/351064
but my code still doesn't work.
#include <iostream>
#include <fstream>
#include <strstream>
#include <math.h>
#include <stdio.h>
using namespace std;
int main()
{
int L = 10;
int size = L*L*L;
int x,y,z=0;
double distance[size];
int idx[size];
int j = 0;
int i = 0;
int swp;
for ( x = 0 ; x<L ; x++){
for ( y = 0 ; y<L ; y++){
for ( z= 0 ; z<L ; z++){
distance[j] = sqrt ((x-(L-1)/2)*(x-(L-1)/2)+(y-(L-1)/2)*(y-(L-1)/2)+(z-(L-1)/2)*(z-(L-1)/2));
//cout << distance[j] << " " << j << endl;
j +=1;
}}}
// cout << j << endl;
for ( i = 0; i < j; i++) {
idx[i]=i;
//cout << idx[i];
}
for ( i = 0; i < j-1; i++) {
if (distance[idx[i]] > distance[idx[i + 1]]) {
swp = idx[i];
idx[i] = idx[i + 1];
idx[i + 1] = swp;
}
// cout << idx[i]<< endl;
}
for ( i = 0; i < j-1; i++) {
cout << distance[idx[i]] << endl;
}
}
Any suggestion would be greatly appreciated.