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:
//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.