I am writing a program for class that should find the largest int from a vector of ints. The vector needs to contain 20 random int values. I have to create a function that returns the maxiumum value.
I have a temp variable that I have used to try to hold the value of the largest int while it iterates through my vector; but, I am not sure why it is not holding it. I'm guessing its a scope issue maybe? I'm not sure. Any help would be greatly appreciated.
So a simple work around is to change to an int function and have it return the max?
I'm sure I could figure that out, thanks, and I do appreciate it.
Is there anyway to have it display the max value with a void function?
I'm curious
Is there anyway to have it display the max value with a void function?
Your code already displays the max value, but only of the first 20 elements, not the entire vector.
But if you wanted to return a value without changing the return type which is void, then you could pass another parameter by reference which allows you to change the value of that parameter and it will be changed in the code from which the function was called.
e.g.
void max(const vector<int>& v, int & maxval)
Note that because a parameter passed by reference may be changed, you should declare as constant any parameter that should not be changed. Here the contents of the vector should not be modified within the function, so make that parameter const.