'Can't sum a vector of integers using for loop c++
I am writing a function that takes a vector of integers as input, and iterates through the vector to obtain the maximum and minimum values in the vector, as well as the sum. The maximum possible integer in the argument vector is 1000000000.
void miniMaxSum(vector<long> arr) {
long sum = 0;
long min = 1000000000;
long max = 0;
for (int i = 0; i < arr.size(); i++) {
cout << arr[i] << endl;
sum += arr[i];
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
cout << sum << endl;
}
Using the following input vector as argument:
vector<long> x = {793810624,895642170,685903712,623789054,468592370 };
The sum is a negative number: -827229366
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
