| | |
Vectors, Functions, and Sorting... oh my!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 4
Reputation:
Solved Threads: 0
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:
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.
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:
C++ Syntax (Toggle Plain Text)
//This part makes sense, the basic stuff #include <iostream> #include <fstream> #include <string> #include <vector> #include<iomanip> using namespace std; template<class t1, class t2, class t3, class t4, class t5, class t6> void SORT (vector<t1> ,vector<t2> ,vector<t3> ,vector<t4> ,vector<t5> ,vector<t6> ); template<class type> void sort(const vector<type>&); template<class type> void print(vector<type> v); //This part I knew, no question about it. //Initializing everything to zero and getting it read and in the function int main() { vector<string> AminoAcid(0); vector<int> O(0); vector<int> C(0); vector<int> N(0); vector<int> S(0); vector<int> H(0); int n; ifstream fin("input.txt"); string test; if(fin.fail()) {cerr<<"Problem opening file";} else { string word; int atoms; int i = 0; while(!fin.eof()) { fin>>word; AminoAcid.push_back(word); fin>>atoms; O.push_back(atoms); fin>>atoms; C.push_back(atoms); fin>>atoms; N.push_back(atoms); fin>>atoms; S.push_back(atoms); fin>>atoms; H.push_back(atoms); i++; } n = i-1; } SORT(AminoAcid,O,C,N,S,H); ///Simply spitting back the values to see if I got the initial function stuff // right, which I did. for(int j = 0;j<n;j++) { cout<<setw(15)<<Amino[j]<<setw(10); cout<<O[j]<<setw(10); cout<<C[j]<<setw(10); cout<<N[j]<<setw(10); cout<<S[j]<<setw(10); cout<<H[j]<<endl; } fin.close(); return 0; } /* FUNCTION SORT */ template<class t1, class t2,class t3,class t4, class t5, class t6> void SORT(vector<t1> v1,vector<t2> v2,vector<t3> v3,vector<t4> v4,vector<t5> v5,vector<t6> v6) { sort(v2); print(v2); // This is where we do that sorting I described above, but I have no idea HOW // to do that (TA gave us the two lines right above this comment line, but // I don't know how it relates or where to go from here at all) return; } // This is the part the TA COULD help me with, of course it had to be // what I already knew and similar to programs we've already covered... template<class type> void sort(vector<type>& x) { int n = x.size(); int hold; // Implement selection sort algorithm. for (int k = 0; k <= n - 2; k++) { for (int j = k + 1; j <= n - 1; j++) { if (x[k] > x[j]) { hold = x[j]; x[j] = x[k]; x[k] = hold; } } } } // Another part the TA gave just today, but I'm not too sure how it relates.... template<class type> void print(vector<type> v) { int n = v.size(); for( int i = 0; i < n-1; i++ ) {cout<<v[i]<<endl;} }
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.
•
•
Join Date: Dec 2004
Posts: 4
Reputation:
Solved Threads: 0
Will this work to begin sorting in descending order by vector?
Or do I need to do something radically different?
C++ Syntax (Toggle Plain Text)
if (int y = 0; int z = 1; O[y] > O[z]; y++; z++;) { return 1; } else if (O[y] < O[z];) { return -1; } else { if (Amino[y] > Amino[z];) { return 1; } else if (Amino[y] < Amino[z];) { return -1; } else { if (C[y] < C[z];)
Or do I need to do something radically different?
•
•
Join Date: Dec 2004
Posts: 4
Reputation:
Solved Threads: 0
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..
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..
•
•
Join Date: Dec 2004
Posts: 4
Reputation:
Solved Threads: 0
Another chunk of code based off some help I was given:
Only error I get is "unexpected end of file" but other than that, will this give me what I want?
C++ Syntax (Toggle Plain Text)
#include <algorithm> #include <iostream> #include <string> #include <fstream> #include <vector> #include <string> using namespace std; int main() { vector<string> Amino(0); vector<int> O(0); vector<int> C(0); vector<int> N(0); vector<int> S(0); vector<int> H(0); int n; struct row { std::string m_1; int m_2; int m_3; int m_4; int m_5; bool const operator<(row const & r) const { 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; } } } } } }; typedef std::vector<row> rows; template<typename container_type> void my_sort(container_type & c) { 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; } void print_predicate(row const & r) { std::cout << r.m_1 << " " << r.m_2 << " " << r.m_3 << " " << r.m_4 << " " << r.m_5 << std::endl; } int main(int, char*) { rows l_rows; ifstream fin("AAM.txt"); string test; if(fin.fail()) {cerr<<"Problem opening file";} else { string word; int atoms; int i = 0; while(!fin.eof()) { fin>>word; Amino.push_back(word); fin>>atoms; O.push_back(atoms); fin>>atoms; C.push_back(atoms); fin>>atoms; N.push_back(atoms); fin>>atoms; S.push_back(atoms); fin>>atoms; H.push_back(atoms); i++; } n = i-1; } my_sort(l_rows); std::for_each(l_rows.begin(), l_rows.end(), print_predicate); fin.close(); return 0; }
![]() |
Similar Threads
- Random number with ascending order help (C++)
- Array Struct Sort Help!! Urgent! (C)
- linked list library (C)
Other Threads in the C++ Forum
- Previous Thread: Integer data type
- Next Thread: Getting a Grade Average
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






