#include <iostream> /* cout, cin, endl etc ... */ #include <algorithm> /* Allows
ID: 3661039 • Letter: #
Question
#include <iostream> /* cout, cin, endl etc ... */
#include <algorithm> /* Allows use of sorting templates/containers */
#include <vector> /* Allows use of vector template */
#include <cstdlib> /* c++ 's version of stdlib for srand() and rand() */
using namespace std;
/*Data sample with time stamp */
struct sample_t {
int length; // datum
int stamp; // time stamp for datum
int colour; // 0=red, 1=blue, 2=green.
};
/* overload the insertion operator to work with sample_t */
ostream& operator<<(ostream& output, const sample_t &A)
{
output << " length = " << A. length << " time= " << A.stamp <<" colour= " << A.colour;
return output;
}
/* let a sorting algorithm access our structure */
bool myRule (sample_t A,sample_t B)
{
return (A. length < B. length); // these are the variables we want it to sort.
}
int main ()
{
const int NSAMPLES = 10; // anysize.
srand(365); /* seed the pseudo-random number generator */
vector <sample_t, allocator<sample_t> > captured_data(0); /* default is size 0 */
vector <sample_t, allocator<sample_t> >::iterator iter; /* allows programmer inside vector */
////////////////////////////////////////////////////////////////////////////////
/* simulate getting sampled data */
for(int i=0; i<NSAMPLES; i++)
{
sample_t myDatum; // our structure to fill with simulated data
myDatum.stamp = i; // integer time stamp
myDatum.colour = rand()%3; // 0, 1, 2.
myDatum. length = rand(); // 0,..., 9.
captured_data.push_back(myDatum);
}
///////////////////////////////////////////////////////////////////////////////
cout << "RAW unsorted data samples ";
for ( iter = captured_data.begin(); iter != captured_data.end(); ++iter)
{
// cout << iter-> length << ' ' << (*iter).stamp << endl;
cout << *iter << endl;
}
cout << ' ';
///////////////////////////////////////////////////////////////////////////////
sort (captured_data.begin(), captured_data.end(), myRule);
////////////////////////////////////////////////////////////////////////////////
cout << "Sorted Data samples by value ";
cout << "value time ";
for ( iter = captured_data.begin(); iter != captured_data.end(); ++iter)
{
cout << (*iter). length << ' ' << iter->stamp << endl;
}
std::cout << ' ';
return 0;
} // end of main()
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////// END OF PROGRAM //////////////////////////
///////////////////////////////////////////////////////////////////////////////
Explanation / Answer
#include <iostream> /* cout, cin, endl etc ... */
#include <algorithm> /* Allows use of sorting templates/containers */
#include <vector> /* Allows use of vector template */
#include <cstdlib> /* c++ 's version of stdlib for srand() and rand() */
using namespace std;
/*Data sample with time stamp */
struct sample_t {
int length; // datum
int stamp; // time stamp for datum
int colour; // 0=red, 1=blue, 2=green.
};
/* overload the insertion operator to work with sample_t */
ostream& operator<<(ostream& output, const sample_t &A)
{
output << " length = " << A. length << " time= " << A.stamp <<" colour= " << A.colour;
return output;
}
/* let a sorting algorithm access our structure */
bool myRule (sample_t A,sample_t B)
{
return (A. length < B. length); // these are the variables we want it to sort.
}
void print_sorted_data_by_value(vector<sample_t, allocator<sample_t> > captured_data)
{
vector <sample_t, allocator<sample_t> >::iterator iter; /* allows programmer inside vector */
cout << "Sorted Data samples by value ";
cout << "value time ";
for (iter = captured_data.begin(); iter != captured_data.end(); ++iter)
{
cout << (*iter).length << ' ' << iter->stamp << endl;
}
std::cout << ' ';
}
void print_raw_data(vector<sample_t, allocator<sample_t> > captured_data)
{
vector <sample_t, allocator<sample_t> >::iterator iter; /* allows programmer inside vector */
cout << "RAW unsorted data samples ";
for (iter = captured_data.begin(); iter != captured_data.end(); ++iter)
{
cout << *iter << endl;
}
cout << ' ';
}
int main ()
{
const int NSAMPLES = 10; // anysize.
srand(365); /* seed the pseudo-random number generator */
vector <sample_t, allocator<sample_t> > captured_data(0); /* default is size 0 */
vector <sample_t, allocator<sample_t> >::iterator iter; /* allows programmer inside vector */
////////////////////////////////////////////////////////////////////////////////
/* simulate getting sampled data */
for(int i=0; i<NSAMPLES; i++)
{
sample_t myDatum; // our structure to fill with simulated data
myDatum.stamp = i; // integer time stamp
myDatum.colour = rand()%3; // 0, 1, 2.
myDatum. length = rand(); // 0,..., 9.
captured_data.push_back(myDatum);
}
print_raw_data(captured_data);
sort (captured_data.begin(), captured_data.end(), myRule);
print_sorted_data_by_value(captured_data);
return 0;
} // end of main()
///////////////////////////////////////////////////////////////////////////////
//////////////////////////////// END OF PROGRAM //////////////////////////
///////////////////////////////////////////////////////////////////////////////
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.