Variable not holding value of largest int in vector

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

void max(vector<int>& v);

int main(int argc, char** argv) {

    srand(time(0));
    vector<int> v(20);
    for(int i = 0; i < 20; i++)
    {
        int num = rand();
        v.push_back(num);
        cout << num << " ";
    }
    max(v);
    
    return 0;
}

void max(vector<int>& v)
{
    int temp = 0;
    for(int i = 0; i < 20; i++)
    {
        if(v[i] > temp)
        {
            temp = v[i];
        }
    }
    cout << "The largest value is " << temp << endl;
}
Last edited on
One of the good features of the vector is that it keeps track of its own size, so there's no need to supply a number in the for loop.
Example:
1
2
3
4
5
void display(const vector<int>& v)
{
    for (unsigned i = 0; i< v.size(); ++i)
        cout << i << ' ' << v[i] << '\n';
}

Call it from main() like this:
1
2
    cout << "\n\n";
    display(v);

typical output:
10152 20045 1590 15777 4576 7634 14173 27718 23523 26930 22335 3038 28572 1471 27826 13178 17709 4897 16526 20766 The largest value
is 0


0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
10 0
11 0
12 0
13 0
14 0
15 0
16 0
17 0
18 0
19 0
20 10152
21 20045
22 1590
23 15777
24 4576
25 7634
26 14173
27 27718
28 23523
29 26930
30 22335
31 3038
32 28572
33 1471
34 27826
35 13178
36 17709
37 4897
38 16526
39 20766
Last edited on
> I have to create a function that returns the maxiumum value.
void max(vector<int>& v);
You are returning nothing.
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.
Topic archived. No new replies allowed.