vector sum

hi. this is a function which takes a vector of ten integers as the argument
and returns the sum of the largest three numbers.I don't need all three for loops. I do need the third.how can I improve this code.
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
#include <iostream> // std::stream
#include <algorithm> // std::sort
#include <vector>    // std::vector
using namespace std;

int SumTop3(vector<int> numbers)
{
    int SUM = 0;
    sort(numbers.begin(), numbers.end()); 
    for(int i = numbers.size() - 3; i < numbers.size(); ++i)
    SUM += numbers[i];
    return SUM;
}

int main()
{
	vector<int> intvec;
	
        for(int i=0; i< number * 2; i++)

        for(int i=0; i<=10; i++)
        {
           intvec.push_back(i);
        }

        for(int i= 0; i<intvec.size(); i++)
	cout << intvec[i] << endl;

	cout<< "Sum of Top 3 is " << SumTop3(intvec) << endl;
} 

19
20
21
22
23
24
        for(int i=0; i< number * 2; i++)

        for(int i=0; i<=10; i++)
        {
           intvec.push_back(i);
        }
This is equivalent to:
19
20
21
22
23
24
25
        for(int i=0; i< number * 2; i++)
        {
                for(int i=0; i<=10; i++)
                {
                   intvec.push_back(i);
                }
        }
Which means you're declaring i twice, which isn't correct.
Topic archived. No new replies allowed.