#include <iostream>
#include <vector>
usingnamespace std;
int main()
{
int sum = 0;
vector<int> numbers(5);
for (int i = 0; i < 5; i++)
{
cin >> numbers[i];
}
for (int i = 0; i < 5; i++)
{
}
system("PAUSE");
return 0;
}
std::accumulate(numbers.begin(), numbers.end(), -numbers[position]);
Where position is the index of the element you want to exclude.
This actually computes the addition so it doesn't assume that the numbers are the first N positive integers.
Are you saying you want to delete one of the numbers, and then total them up? If you wanted to do that, you could just set one of the numbers in the array to zero, so it doesn't get calculated in the total.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
int array[5], total = 0, deleteNum;
cout << "Enter five numbers: " << endl;
for (int i = 0; i < 5; i++)
cin >> array[i];
cout << "Which array element would you like to delete? (0, 1, 2, 3, or 4) " << endl;
cin >> deleteNum;
cout << "Deleting array element " << deleteNum << ", which is " << array[deleteNum] << ". " << endl;
//Doesn't really "delete" it, just sets the value to zero so it's not calculated in the sum
array[deleteNum] = 0;
for (int i = 0; i < 5; i++)
total += array[i];
cout << "The total of the numbers is: " << total << ". " << endl;
}