0

How would I find the sum of the elements in a vector that was inputted by a user? I tried searching for a method to do so everywhere online but couldn't really find one online that explained it really well, nor was it explained in class too much unfortunately.

So I basically have the vectors inputted by a user here, but I have no idea how to use it to take the sum of it? (printvector is only there because I have to present what the user put in to the user before telling the user the sum)

#include <iostream>
#include <vector>
using namespace std;

void fillVector(vector<int>&);

void printVector(const vector<int>&);

int main()
{

vector<int> VectorQuantities;
fillVector(VectorQuantities);
printVector(VectorQuantities);

return 0;
}

void fillVector(vector<int>& newVectorQuantities)
{
cout << "Type in a list of numbers, and type in -1 as the last number when       you are finished: ";
int input;
cin >> input;

while (input != -1) {
    newVectorQuantities.push_back(input);
    cin >> input;
}
cout << endl;
}

void printVector(const vector<int>& newVectorQuantities) {

cout << "Vector: ";
for (unsigned int i=0; i < newVectorQuantities.size(); i++) {
    cout << newVectorQuantities[i] << " ";
}

cout << endl;
}
Sal
  • 25
  • 1
  • 4

2 Answers2

5

You can use std::accumulate().

#include <algorithm>
std::vector<int> vec = ...;
int vecSum = std::accumulate(std::begin(vec), std::end(vec), 0);

The accumulate() function is really just a left fold, and by default it uses the + function to combine elements.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
-1

Try this:

void printSum(const vector<int>& newVectorQuantities) {

    cout << "Sum: ";
    int sum = 0;
    for (unsigned int i=0; i < newVectorQuantities.size(); i++) {
        sum = sum + newVectorQuantities[i];
    }
     cout << sum << " ";
}

(Using your style for the function, not modern C++.)

Rob L
  • 2,351
  • 13
  • 23
  • The compiler isn't being very fast so I won't be able to confirm for sure it works but I get the idea, and thank you for using namespace std, we aren't really allowed to use string that much. Would void printAverage work the same way if it was defined correctly? – Sal Mar 16 '16 at 03:31
  • You probably need to use a double precision sum (or at least division) for printAverage. Otherwise, your average will be an integer, truncated. For robustness, you probably should use a double-precision sum for printSum, too, otherwise it could overflow if you have a large list. – Rob L Mar 16 '16 at 03:34
  • Gotcha. I'm supposed to limit the list to 10, but the best I could do was set it to where -1 would mark the end of the list. Once I figure out how to limit it to 10 I will, although I think it should be fine as is, because the script's general purpose is to use 10 inputs. Thank you very much for the help! – Sal Mar 16 '16 at 03:44
  • Change your while loop: while (input != -1 && newVectorQuantities.size() < 10). That will terminate either on -1 or after 10 entries. – Rob L Mar 16 '16 at 03:46