I am working on a program that takes an arbitrary number of command line arguments (at least 1), and outputs the minimum and maximum value, as well as what position in the array they were--e.g., if 1 2 3 is input, the resulting output will be
1 2 3
Largest: 3 at position 2.
Smallest: 1 at position 0.
In order to take the command line arguments and compare compare them with the int data array, I need to transform the command line arguments into integers. I am trying to use this:
Line 3: data is a simple int array. int arrays have no begin() function. You may want to use the std::array container which does have a begin() member function.
but that still doesn't work. This is the error message:
error: non-type template argument is not a constant expression
array<int, nData> data;
^~~~~
note: read of non-const variable 'nData' is not allowed in a constant
expression
note: declared here
int nData = argc - 1;
^
1 error generated.
I can't use a const because I have a find() later on in the program that const breaks. I was able to use a vector of size nData, but I am getting this error, relating to not using a
1 2 3 4 5 6 7 8 9
const[/code:
[output]
error: variable length array of non-POD element type 'vector<int>'
vector<int> data [nData];
^
1 error generated.
[/output]
I did make [code]nData
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <cassert>
#include <algorithm>
#include <iterator>
#include "minimax.h"
usingnamespace std;
int main (int argc, char **argv)
{
usingnamespace std;
if (argc < 2)
{
cerr << "This program needs at least one command line argument."
<< endl;
return -1;
}
MiniMax mnmx;
int i = 0;
int nData = argc - 1;
vector<int> data (nData);
string str = argv[i+1];
transform ( argv+1, argv+argc, data.begin(), atoi );
istringstream numberIn ( str );
numberIn >> data[i];
observe ( data[i]));
std::for_each ( data.begin(), data.end() );//Problem here
int minPos = std::find ( data.begin(), data.end(), mnmx.getMin() ) -data.begin();
int maxPos = std::find ( data.begin(), data.end(), mnmx.getMax() ) - data.begin();
copy ( argv+1, argv+argc, ostream_iterator<constchar*> ( cout, " "));
cout << endl;
cout << "The smallest value is " << mnmx.getMin()
<< " and can be found in position " << minPos
<< endl;
cout << "The largest value is " << mnmx.getMax()
<< " and can be found in position " << maxPos
<< endl;
return 0;
}
Here is what I am trying to do. I know my transform is right, but now I need to make the for_each perform these operations for each command line argument in the data vector:
Is the best way to do this is write a void function with the command line arguments passed in as parameters and then call that function from the for_each portion? Any suggestions would be appreciated.
Here is what I am trying to do. I know my transform is right, but now I need to make the for_each perform these operations for each command line argument in the data vector:
You already stored the values in the data vector. Now you overwrite the first element with the same value derived via a stringstream. What is the purpose of doing that?
what does function observe() do?
Would writing a functor work better? That way, I could use it directly within the for_each(), but it would not require writing a function that is used only once?
I'm still not sure what you're trying to do exactly. Clearly the problem can be solved easily using std::min_element / std::max_element but you wish to do it some other way. Is this a self-education project or one you must do for some homework. In other words, are the constraints imposed by yourself, or externally by the instructions you were given?
How could this be solved using std::max_element? For each of the input parameters, I need to read them in and then later determine the minPos and maxPos. The limitations are not mine. Instead, could I do the calls to minPos = find () from within a for_each and use it as a lambda expression?
Here is what I have come up with. I have thought of moving the minPos and maxPos positions to be included in each lambda expression for each of the for_each's. Here is what I have right now: