2-dim array sorting?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

2-dim array sorting?

 
0
  #1
Aug 11th, 2008
I want a 2-dim array to print out on the same line as string word. I cant seem to figure out how to get a 2-dim array to do this, does anyone know how to do this?

  1. void Sort(string word[],float grades[][8], int number)
  2. {
  3. int i,j=o, temp;
  4. string aName;
  5. float aName2[][];
  6.  
  7. for (i=0; i<(names - 1); i++)
  8. {
  9. temp = i;
  10. aName = worde[i];
  11. aName2=grades[i][j];
  12.  
  13. for(int k=i+1; k<names; k++)
  14. {
  15. if (word[k] < aName)
  16. {
  17. aName = word[k];
  18. aName2[i][j]=grades[k][8];
  19. temp = k;
  20. }
  21. }
  22. word[temp] = word[i];
  23. grades[temp][j]=grades[i][j];
  24. word[i] = aName;
  25. grades[i][j]=aName2[][];
  26.  
  27. }
  28.  
  29. }
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 351
Reputation: Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about Radical Edward has a spectacular aura about 
Solved Threads: 62
Radical Edward's Avatar
Radical Edward Radical Edward is offline Offline
Posting Whiz

Re: 2-dim array sorting?

 
1
  #2
Aug 11th, 2008
The ideal way to do this is to encapsulate the data into an object that defines a comparison operator. Then you can sort the objects instead of trying to sort parallel arrays:
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <stdexcept>
  4. #include <string>
  5.  
  6. namespace EdRules {
  7. using namespace std;
  8.  
  9. class Student {
  10. string _name;
  11. double _grades[8];
  12. public:
  13. Student(const string& name);
  14. const string& name() const;
  15. double& operator[](int i);
  16. const double& operator[](int i) const;
  17. bool operator<(const Student& student);
  18. friend ostream& operator<<(ostream& os, const Student& student);
  19. };
  20.  
  21. Student::Student(const string& name)
  22. : _name(name)
  23. {
  24. for (int i = 0; i < 8; ++i)
  25. _grades[i] = 0;
  26. }
  27.  
  28. const string& Student::name() const { return _name; }
  29.  
  30. double& Student::operator[](int i)
  31. {
  32. if (i < 0 || i >= 8)
  33. throw out_of_range("Index must be between 0 and 8");
  34.  
  35. return _grades[i];
  36. }
  37.  
  38. const double& Student::operator[](int i) const
  39. {
  40. if (i < 0 || i >= 8)
  41. throw out_of_range("Index must be between 0 and 8");
  42.  
  43. return _grades[i];
  44. }
  45.  
  46. bool Student::operator<(const Student& student)
  47. {
  48. return _name < student._name;
  49. }
  50.  
  51. ostream& operator<<(ostream& os, const Student& student)
  52. {
  53. os << student._name;
  54.  
  55. for (int i = 0; i < 8; ++i)
  56. os << (i == 0 ? '{' : ',') << student._grades[i];
  57.  
  58. return os << '}';
  59. }
  60. }
  61.  
  62. void ShowStudents(EdRules::Student a[], int size)
  63. {
  64. for (int i = 0; i < size; ++i)
  65. std::cout << a[i] << '\n';
  66. std::cout << '\n';
  67. }
  68.  
  69. int main()
  70. {
  71. using EdRules::Student;
  72.  
  73. Student a[5] = {
  74. Student("test c"),
  75. Student("test b"),
  76. Student("test d"),
  77. Student("test a"),
  78. Student("test e")
  79. };
  80.  
  81. for (int i = 0, k = 0; i < 5; ++i) {
  82. for (int j = 0; j < 8; ++j)
  83. a[i][j] = ++k;
  84. }
  85.  
  86. ShowStudents(a, 5);
  87. std::sort(a, a + 5);
  88. ShowStudents(a, 5);
  89. }
This way you don't have to worry about how to index the arrays, how to copy them, and all of the other limitations that come up with the native array type.
If at first you don't succeed, keep on sucking until you do succeed.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 371 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC