-1

I want to compare elements for each line of a matrix, from smallest to biggest. But I don't want to print the sorted array I want to print the original position.

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

In this Matrix for the first line I wanna print 1, 2, 4, 3

My Code so far:

#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;
}
  • And your question is? – Ceros Apr 24 '17 at 13:37
  • How to print the sorted positions – Katha Hast Apr 24 '17 at 13:38
  • See [Creating a vector of indices of a sorted vector](http://stackoverflow.com/questions/25921706/creating-a-vector-of-indices-of-a-sorted-vector), [c++ sort keeping track of indices](http://stackoverflow.com/questions/10580982/c-sort-keeping-track-of-indices), [C++ sorting and keeping track of indexes](http://stackoverflow.com/questions/1577475/c-sorting-and-keeping-track-of-indexes), etc – Chris Drew Apr 24 '17 at 13:41
  • 1
    Have a look at [std::map](http://www.cplusplus.com/reference/map/map/). You could map the array values to their indices. After map has been filled for one line you may output the map from `begin()` to `end()` printing the mapped indices. If multiple array elements with equal values are possible a [std::multimap](http://www.cplusplus.com/reference/map/multimap/) should be used instead. – Scheff's Cat Apr 24 '17 at 13:42
  • My problem is I don't know how to initialize my matrix (every line from the matrix as an own vector) as an vector, because I must get this matrix from an external .dat file... – Katha Hast Apr 24 '17 at 13:50

1 Answers1

0

You need to maintain another matrix which has indices of every line and then apply the same operations on it that you are applying on each line of the original matrix to sort it.

Here is the code:

#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;
}
Ahmed Akhtar
  • 1,444
  • 1
  • 16
  • 28