user3093567
user3093567

Reputation: 21

Using Multidimensional Arrays to Keep Track of Indices in C++

I'm working on a project where I need to sort an array from least to greatest, but save the values of the indices. For example, with the array {2, 7, 8, 1, 3}, the sorted indices would be {3, 0, 4, 1, 2}. I thought that I could use a two dimensional array to accomplish this; I would add the index to each component, sort the array, then retrieve the original index from the second element. It hasn't been working for me as well as I hoped though, I have my current code below and it keeps giving me a segmentation fault. I don't know what I'm doing wrong, but I'm assuming its something in my for loops.

#include <iostream>
#include <algorithm>

using namespace std;

const int SIZE = 7;

int main()
{
  int intArray[SIZE] = {5, 3, 32, -1, 1, 104, 53};
  int i, j, k, l = 0;
  int temp[SIZE][2];

  //fills multidimensional array temp with data from intArray or index #
  for(i = 0; i < 7; i++){
    for(j = 0; j < 2; j++){
      switch (j)
    {
    case '0':
      temp[i][j] = *intArray;
      break;
    case '1':
      temp[i][j] = i;
      break;
      }
    }
  } 

  sort(intArray, intArray + SIZE);

  //prints each component of temp individually
  cout << "Sorted Array looks like this." << endl;
  for (k = 0; i < 7; i++){
    for (l = 0; j < 2; i++){
      cout << &temp[k][l] << endl;
    }
  }
  return 0;
}

Upvotes: 2

Views: 120

Answers (3)

user3093567
user3093567

Reputation: 21

I ended up using what Frank Puffer said and chose to use a simple bubble sort instead of using any of the built in functions. Here is a link to what my final program looked like http://ideone.com/cpEgGA

#include <iostream>
#include <algorithm>

using namespace std;

const int SIZE = 7;

int main()
{
  int intArray[SIZE] = {5, 3, 32, -1, 1, 104, 53};
  int i, j, k, l, m = 0;
  int temp2[SIZE][2];
  int indices[SIZE] = {};

  for(i = 0; i < 7; i++){
    temp2[i][0] = intArray[i];
    temp2[i][1] = i;
  }

  cout << "Unsorted Array looks like this." << endl;
  for (j = 0; j < 7; j++){
    cout << temp2[j][0];
    cout << " : ";
    cout << temp2[j][1] << endl;
  }

  for(k = 0; k < SIZE; k++)
    {
      for(j = 0; j < SIZE-1-k; j++)
    {
      if(temp2[j+1][0] < temp2[j][0])
        {
          l = temp2[j][0];
          temp2[j][0] = temp2[j+1][0];
          temp2[j+1][0] = l;
          l = temp2[j][1];
          temp2[j][1] = temp2[j+1][1];
          temp2[j+1][1] = l;
        }
    }
    }

  cout << "Sorted Array looks like this." << endl;
  for (m = 0; m < SIZE; m++)
    {
      cout << temp2[m][0];
      cout << " : ";
      cout << temp2[m][1] << endl;
    }

  for(i = 0; i < SIZE; i++){
    indices[i] = temp2[i][1];
  }

  cout << "Indices of Sorted Array look like this." << endl;
  for(i = 0; i < SIZE; i++){
    cout << indices[i] << endl;
  }

  return 0;
}

Upvotes: 0

Max ZS
Max ZS

Reputation: 175

Use std::map. The code will be the easiest. Test on cpp.sh

#include <iostream>
#include <map>

int main()
{
    std::map<int,int>arr = {{5,0}, {3,1}, {32,2}, {-1,3}, {1,4}, {104,5}, {53,6}};
    for(auto&x:arr) std::cout << x.first << " - > " << x.second << std::endl;
  return 0;
}

Upvotes: 0

Frank Puffer
Frank Puffer

Reputation: 8215

The following loop is much simpler and does what it should:

for(i = 0; i < 7; i++){
    temp[i][0] = intArray[i];
    temp[i][1] = i;
}

One error in your code is the line

temp[i][j] = *intArray;

This always assigns the 1st element of intArray.

The thing that causes the segmentation fault is probably the & in the output statement, just remove it.

Aside from that, I agree with the recommendation in the comment by RyanP.

Upvotes: 1

Related Questions