some combination of std::distance/std::max_element ( ? )
Given a sequence with n elements. For instance:
double arr [ 6 ] = { 0., 2., 4., 5., 6., 7.}
I'll define - what I call 'test zones' . Within these test zones I'll
seach for _and_ store max values and position found.
For instance: Lets assume 2 test zones. This would equate to:
Zone 1 elements: 0., 2., 4
Zone 2 elements: 5., 6., 7
Max value - zone 1 = 4, location 2 ( element index)
Max value - zone 2 = 7, location 5 ( element index)
The code:
std::deque<double> sequence1;
int t_zone = 3;
//initialization of the sequence1 deque
void init(){
sequence1.push_back ( 2.3123 );
sequence1.push_back ( 4.3445 );
sequence1.push_back ( 1.343 );
sequence1.push_back ( 1.4222 );
sequence1.push_back ( 2.3123 );
sequence1.push_back ( 1.5554 );
sequence1.push_back ( 1.9936 );
sequence1.push_back ( 4.3445 );
sequence1.push_back ( 2.3123 );
sequence1.push_back ( 7.3555 );
sequence1.push_back ( 6.222 );
sequence1.push_back ( 6.222 );
}
//prints sequence1 and the test zones
void print_list(){
cout<<"The list is :";
for (std::deque<double>::iterator it = sequence1.begin(); it !=
sequence1.end() ; it++)
cout<< " "<< *it;
cout<<endl;
cout<<"The test zones" << endl ;
int count = 0;
int cc = 0;
for (std::deque<double>::iterator it = sequence1.begin(); it !=
sequence1.end() ; it++){
if (count == 0)
{ cout<< "Test zone "<<(char) ('A'+cc)<<" : "; cc++; }
cout<< " "<< *it;
count++;
if (count%t_zone == 0) { cout<<endl; count=0; }
}
}
//The solution to the second requirement
void run_it(){
cout<<"The second method"<<endl;
std::map< int, double > mMap;
int count = 0;
int pos;
int max_p;
double max;
// find the max values and insert them in the map
std::deque<double>::iterator it;
for ( it= sequence1.begin() ,pos = 0; it != sequence1.end() ; it++ ,
pos++){
if (count == 0) {
max = (*it);
max_p = pos;
}
else if (*it > max) {
max = (*it);
max_p = pos;
}
count++;
if (count == t_zone) {
count = 0;
mMap[max_p] = max;
}
}
std::map< int, double >::iterator itt;
for ( itt = mMap.begin() ,count = 0; itt != mMap.end() ; itt++ ,
count++){
cout<< "Test zone "<<(char) ('A'+count)<<" "<<itt->second<<"
location: element "
<<itt->first<<endl;
}
}
// void run_it2 () // option2 - just use 2 deques for both index and
max value
int main()
{
cout<<"Please enter the test zone ";
cin>>t_zone;
init();
print_list();
run_it();
//char c; //uncoment for the program to wait for a character, helps
see the results better
//cin>>c;
return 0;
}
At issue: I'm thinking some combination std::distance and
std::max_element would work better. Sample source on how to achieve
this greatly appreaciated.
Thanks
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]