943,648 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 462
  • C++ RSS
Aug 11th, 2008
0

2-dim array sorting?

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Aug 11th, 2008
1

Re: 2-dim array sorting?

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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 361
Solved Threads: 97
Posting Pro
Radical Edward is offline Offline
526 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Writing Byte Array to a File
Next Thread in C++ Forum Timeline: Help with SDL plaese!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC