Vectors, Functions, and Sorting... oh my!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 4
Reputation: Marivel is an unknown quantity at this point 
Solved Threads: 0
Marivel Marivel is offline Offline
Newbie Poster

Vectors, Functions, and Sorting... oh my!

 
0
  #1
Dec 8th, 2004
Yeah, it's your typical homework help topic, but the problem is this is stuff we NEVER COVERED IN CLASS and it was NOT IN OUR BOOK, even our TA can't figure out why our professor assigned this to our 101 class.

I have a good chunk of it done, I think. The stuff we DID learn in class. But of course, the one part I don't know where to begin on (aka the one part that's not in our book at all or discussed in lecture) is the only part our TA was specifically told he could NOT help us with...

It involves functions and vectors. Basically we need to be able to sort a chunk of input like this:

3 4 2 5
1 5 2 5
1 6 3 2
2 4 5 6

So that it sorts it by whatever column that is specified, and if the value is the same in two or more rows, it goes onto sorting by the next column specified, and so on so forth. We just repeat it a bunch of times with a different column order and send it to an output file, actually a different output for each sort.

So here's the program so far:

  1. //This part makes sense, the basic stuff
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <vector>
  6. #include<iomanip>
  7. using namespace std;
  8.  
  9. template<class t1, class t2, class t3, class t4, class t5, class t6>
  10. void SORT
  11. (vector<t1> ,vector<t2> ,vector<t3> ,vector<t4> ,vector<t5> ,vector<t6> );
  12.  
  13.  
  14. template<class type>
  15. void sort(const vector<type>&);
  16.  
  17. template<class type>
  18. void print(vector<type> v);
  19.  
  20. //This part I knew, no question about it.
  21. //Initializing everything to zero and getting it read and in the function
  22. int main()
  23. {
  24. vector<string> AminoAcid(0);
  25. vector<int> O(0);
  26. vector<int> C(0);
  27. vector<int> N(0);
  28. vector<int> S(0);
  29. vector<int> H(0);
  30. int n;
  31.  
  32. ifstream fin("input.txt");
  33. string test;
  34.  
  35. if(fin.fail())
  36. {cerr<<"Problem opening file";}
  37. else
  38. {
  39. string word;
  40. int atoms;
  41. int i = 0;
  42. while(!fin.eof())
  43. {
  44. fin>>word;
  45. AminoAcid.push_back(word);
  46. fin>>atoms;
  47. O.push_back(atoms);
  48. fin>>atoms;
  49. C.push_back(atoms);
  50. fin>>atoms;
  51. N.push_back(atoms);
  52. fin>>atoms;
  53. S.push_back(atoms);
  54. fin>>atoms;
  55. H.push_back(atoms);
  56. i++;
  57. }
  58. n = i-1;
  59. }
  60. SORT(AminoAcid,O,C,N,S,H);
  61.  
  62. ///Simply spitting back the values to see if I got the initial function stuff
  63. // right, which I did.
  64. for(int j = 0;j<n;j++)
  65. {
  66. cout<<setw(15)<<Amino[j]<<setw(10);
  67. cout<<O[j]<<setw(10);
  68. cout<<C[j]<<setw(10);
  69. cout<<N[j]<<setw(10);
  70. cout<<S[j]<<setw(10);
  71. cout<<H[j]<<endl;
  72. }
  73.  
  74.  
  75. fin.close();
  76. return 0;
  77. }
  78.  
  79.  
  80. /* FUNCTION SORT */
  81.  
  82. template<class t1, class t2,class t3,class t4, class t5, class t6>
  83. void SORT(vector<t1> v1,vector<t2> v2,vector<t3> v3,vector<t4> v4,vector<t5>
  84. v5,vector<t6> v6)
  85. {
  86. sort(v2);
  87. print(v2);
  88. // This is where we do that sorting I described above, but I have no idea HOW
  89. // to do that (TA gave us the two lines right above this comment line, but
  90. // I don't know how it relates or where to go from here at all)
  91. return;
  92. }
  93.  
  94.  
  95. // This is the part the TA COULD help me with, of course it had to be
  96. // what I already knew and similar to programs we've already covered...
  97. template<class type>
  98. void sort(vector<type>& x)
  99. {
  100. int n = x.size();
  101. int hold;
  102.  
  103. // Implement selection sort algorithm.
  104.  
  105. for (int k = 0; k <= n - 2; k++)
  106. {
  107. for (int j = k + 1; j <= n - 1; j++)
  108. {
  109. if (x[k] > x[j])
  110. {
  111. hold = x[j];
  112. x[j] = x[k];
  113. x[k] = hold;
  114. }
  115. }
  116. }
  117. }
  118.  
  119. // Another part the TA gave just today, but I'm not too sure how it relates....
  120. template<class type>
  121. void print(vector<type> v)
  122. {
  123. int n = v.size();
  124.  
  125. for( int i = 0; i < n-1; i++ )
  126. {cout<<v[i]<<endl;}
  127.  
  128. }


So yeah. I just don't know where to begin on that one part, nor what to look for in a tutorial... I just keep getting how-to guides on sorting just one vector. I just don't know how to get the input from the first part sorted..

As the code stands now it gives errors, which doesn't surprise me. Probably due to the fact that just that one piece is missing, really. But here it is in case it's something more:

c:\documents and settings\chris\my documents\ass4mod.cpp(72) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,st
d::basic_string<char,std::char_traits<char>,std::allocator<char> > const &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > const *,int>' : identifier was truncated to '255' characters in the debug information
c:\documents and settings\chris\my documents\ass4mod.cpp(72) : warning C4786: 'std::reverse_iterator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::bas
ic_string<char,std::char_traits<char>,std::allocator<char> > &,std::basic_string<char,std::char_traits<char>,std::allocator<char> > *,int>' : identifier was truncated to '255' characters in the debug information
c:\documents and settings\chris\my documents\ass4mod.cpp(80) : error C2065: 'v2' : undeclared identifier
c:\documents and settings\chris\my documents\ass4mod.cpp(56) : see reference to function template instantiation 'void __cdecl SORT(class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,clas
s std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >,class std::vector<int,class std::allocator<int> >,class std::vector<int,class std::allocator<int> >,class std::vector<int,class std::allocat
or<int> >,class std::vector<int,class std::allocator<int> >,class std::vector<int,class std::allocator<int> >)' being compiled
Error executing cl.exe.

1 error(s), 2 warning(s)

Also, once I get that one chunk of code... would I have to repeat the code X number of times for X different output files, or is there a way to put it in a loop but write to a different output file each time? If it's possible, I think I know what to do for it, but I'm more concerned with the program itself right now....

I've been working on this for quite a while so any help is greatly appriciated.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4
Reputation: Marivel is an unknown quantity at this point 
Solved Threads: 0
Marivel Marivel is offline Offline
Newbie Poster

Re: Vectors, Functions, and Sorting... oh my!

 
0
  #2
Dec 9th, 2004
Will this work to begin sorting in descending order by vector?

  1. if (int y = 0; int z = 1; O[y] > O[z]; y++; z++;)
  2. {
  3. return 1;
  4. }
  5.  
  6. else if (O[y] < O[z];)
  7. {
  8. return -1;
  9. }
  10. else { if (Amino[y] > Amino[z];)
  11. {
  12. return 1;
  13. }
  14. else if (Amino[y] < Amino[z];)
  15. {
  16. return -1;
  17. }
  18. else { if (C[y] < C[z];)

Or do I need to do something radically different?
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Vectors, Functions, and Sorting... oh my!

 
0
  #3
Dec 9th, 2004
Do you have to use vector?
If you can use list instead, that has built-in sorting functionality, all you need is to pass the sort function of list a pointer to a boolean function that compares 2 elements.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4
Reputation: Marivel is an unknown quantity at this point 
Solved Threads: 0
Marivel Marivel is offline Offline
Newbie Poster

Re: Vectors, Functions, and Sorting... oh my!

 
0
  #4
Dec 9th, 2004
Yeah, we have to use vectors - that's about the only certain bit of info we were given.

This is what I have now, and it looks promising (if not lenghty...)

[php]
for (z == size() );

{
int y = 0;
int z = 1;
int c;

if (O[y] > O[z]);
{
c = 1;
return 0;
}

else if (O[y] < O[z]);
{
c = -1;
return 0;
}
else { if (Amino[y] > Amino[z]);
{
c = 1;
return 0;
}
else if (Amino[y] < Amino[z]);
{
c = -1;
return 0;
}
else { if (C[y] > C[z]);
{
c = 1;
return 0;
}
else if (C[y] < C[z]);
{
c = -1;
return 0;
}
else { if (N[y] > N[z]);
{
c = 1;
return 0;
}
else if (N[y] < N[z]);
{
c = -1;
return 0;
}
else {if (S[y] > S[z]);
{
c = 1;
return 0;
}
else if (S[y] < S[z]);
{
return 0;
}
else {if (H[y] > H[z]);
{
c = 1;
return 0;
}
else if (H[y] < H[z]);
{
c = -1;
return 0;
}




if (c = -1);
{
hold = O[z];
O[z] = O[y];
O[y] = hold;

hold = Amino[z];
Amino[z] = Amino[y];
Amino[y] = hold;

hold = C[z];
C[z] = C[y];
C[y] = hold;

hold = N[z];
N[z] = N[y];
N[y] = hold;

hold = S[z];
S[z] = S[y];
S[y] = hold;

hold = H[z];
H[z] = H[y];
H[y] = hold;

}
}
}
y++;
z++;
return 0;
}
[/php]

Does this look about right to sort in descending order yet keep the horizontal rows the same (merely shifting their position)?

The only reason I'm hesitant to try it out is I have to do 6 iterations of this sorting order, and it'll get really long REALLY fast so there must be a shorter way.

Not to mention, I'm not sure where exactly this is supposed to go in my program other than "towards the bottom..." part of me doesn't want to monkey around with what I was already given but it doesn't seem to work otherwise..
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Vectors, Functions, and Sorting... oh my!

 
0
  #5
Dec 9th, 2004
Deeply nested conditionals always tick me off. There's always (well, almost) a better (read, cleaner) way of doing things.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4
Reputation: Marivel is an unknown quantity at this point 
Solved Threads: 0
Marivel Marivel is offline Offline
Newbie Poster

Re: Vectors, Functions, and Sorting... oh my!

 
0
  #6
Dec 9th, 2004
Another chunk of code based off some help I was given:

  1. #include <algorithm>
  2. #include <iostream>
  3. #include <string>
  4. #include <fstream>
  5. #include <vector>
  6. #include <string>
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. vector<string> Amino(0);
  12. vector<int> O(0);
  13. vector<int> C(0);
  14. vector<int> N(0);
  15. vector<int> S(0);
  16. vector<int> H(0);
  17. int n;
  18.  
  19. struct row
  20. {
  21. std::string m_1;
  22. int m_2;
  23. int m_3;
  24. int m_4;
  25. int m_5;
  26.  
  27. bool const operator<(row const & r) const
  28. {
  29. for (z =< size() );
  30.  
  31. {
  32. int y = 0;
  33. int z = 1;
  34. int c;
  35.  
  36. if (O[y] > O[z]);
  37. {
  38. c = 1;
  39. return 0;
  40. }
  41.  
  42. else if (O[y] < O[z]);
  43. {
  44. c = -1;
  45. return 0;
  46. }
  47. else { if (Amino[y] > Amino[z]);
  48. {
  49. c = 1;
  50. return 0;
  51. }
  52. else if (Amino[y] < Amino[z]);
  53. {
  54. c = -1;
  55. return 0;
  56. }
  57. else { if (C[y] > C[z]);
  58. {
  59. c = 1;
  60. return 0;
  61. }
  62. else if (C[y] < C[z]);
  63. {
  64. c = -1;
  65. return 0;
  66. }
  67. else { if (N[y] > N[z]);
  68. {
  69. c = 1;
  70. return 0;
  71. }
  72. else if (N[y] < N[z]);
  73. {
  74. c = -1;
  75. return 0;
  76. }
  77. else {if (S[y] > S[z]);
  78. {
  79. c = 1;
  80. return 0;
  81. }
  82. else if (S[y] < S[z]);
  83. {
  84. return 0;
  85. }
  86. else {if (H[y] > H[z]);
  87. {
  88. c = 1;
  89. return 0;
  90. }
  91. else if (H[y] < H[z]);
  92. {
  93. c = -1;
  94. return 0;
  95. }
  96. }
  97. }
  98. }
  99. }
  100.  
  101. };
  102.  
  103. typedef std::vector<row> rows;
  104.  
  105. template<typename container_type>
  106. void my_sort(container_type & c)
  107. {
  108. if (c = -1);
  109. {
  110. hold = O[z];
  111. O[z] = O[y];
  112. O[y] = hold;
  113.  
  114. hold = Amino[z];
  115. Amino[z] = Amino[y];
  116. Amino[y] = hold;
  117.  
  118. hold = C[z];
  119. C[z] = C[y];
  120. C[y] = hold;
  121.  
  122. hold = N[z];
  123. N[z] = N[y];
  124. N[y] = hold;
  125.  
  126. hold = S[z];
  127. S[z] = S[y];
  128. S[y] = hold;
  129.  
  130. hold = H[z];
  131. H[z] = H[y];
  132. H[y] = hold;
  133.  
  134. }
  135. }
  136. }
  137. y++;
  138. z++;
  139. return 0;
  140.  
  141. }
  142.  
  143. void print_predicate(row const & r)
  144. {
  145. std::cout
  146. << r.m_1 << " "
  147. << r.m_2 << " "
  148. << r.m_3 << " "
  149. << r.m_4 << " "
  150. << r.m_5 << std::endl;
  151. }
  152.  
  153. int main(int, char*)
  154. {
  155. rows l_rows;
  156. ifstream fin("AAM.txt");
  157. string test;
  158.  
  159. if(fin.fail())
  160. {cerr<<"Problem opening file";}
  161. else
  162. {
  163. string word;
  164. int atoms;
  165. int i = 0;
  166. while(!fin.eof())
  167. {
  168. fin>>word;
  169. Amino.push_back(word);
  170. fin>>atoms;
  171. O.push_back(atoms);
  172. fin>>atoms;
  173. C.push_back(atoms);
  174. fin>>atoms;
  175. N.push_back(atoms);
  176. fin>>atoms;
  177. S.push_back(atoms);
  178. fin>>atoms;
  179. H.push_back(atoms);
  180. i++;
  181. }
  182. n = i-1;
  183. }
  184. my_sort(l_rows);
  185.  
  186. std::for_each(l_rows.begin(), l_rows.end(), print_predicate);
  187. fin.close();
  188. return 0;
  189. }
Only error I get is "unexpected end of file" but other than that, will this give me what I want?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 13
Reputation: britt_boy is an unknown quantity at this point 
Solved Threads: 1
britt_boy britt_boy is offline Offline
Newbie Poster

Re: Vectors, Functions, and Sorting... oh my!

 
0
  #7
Dec 9th, 2004
Have you thought of using the sort algorithm in the stl, and a compare func

struct containerCompare
{
bool operator()(const Type& p1, const Type& p2) const
{

//compare your elements here
}
};


std::sort(container.begin(),container.end(),containerCompare());
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC