### Subtracting values in a vector

I am just learning C++ and I want to add this piece of code to a project. I have created a vector from values entered by a user. I want to then subtract each value from each other from the beginning to the end. For example if a user enters 10,4,3 the program will subtract the values in that order 10 - 4 - 3 and output 3 as the solution. I figured how to do it with addition but can't get it to output for subtraction.
 1234567891011121314151617181920212223242526272829303132 #include #include using namespace std; //Subtraction using vectors int main() { vectorvectorValues; int Size, subtraction, temp; double values; cout << "How many values you want to subtract: "; cin >> Size; int i = Size; for(i = 0; i < Size; ++i) { cout << "Enter the values to be subtracted: "; cin >> values; vectorValues.push_back(values); } cout << "You want to subtract a total of "<< int(vectorValues.size()) << " numbers. \n"; for(int i = 0; i < vectorValues.size(); i++) { //subtraction -= vectorValues[i]; <--This is where I'm stuck cout << vectorValues[i] << ", "; } cout << "The subtraction of these values is: " << subtraction << "\n\n"; system("pause"); return 0; }
Set your subtraction value equal to vectorValues[0] before the subtraction for loop so it knows where to start. Then your for loop is basically correct (you will want to skip vectorValues[0] in the loop through either by starting at 1 or using an if statement).
Okay I think I'm almost there my problem now is its subtracting from the first entered number only. So when I put in a vector of three numbers 10, 3, 2 it give me the answer 7 and 8 instead of 5. Kind of the same problem I was having earlier before the changes. How can I get it to subtract value to next value.
 12345678910111213141516171819202122232425262728293031323334 #include #include using namespace std; //Subtraction using vectors int main() { vectorvectorValues; int Size, subtraction, temp; double values; cout << "How many values you want to subtract: "; cin >> Size; int i = Size; for(i = 0; i < Size; ++i) { cout << "Enter the values to be subtracted: "; cin >> values; vectorValues.push_back(values); } cout << "You want to subtract a total of "<< int(vectorValues.size()) << " numbers. \n"; for(int i = 1; i < vectorValues.size(); i++) { subtraction = vectorValues[0]; subtraction -= vectorValues[i]; cout << vectorValues[i] << ", "; cout << "The subtraction of this value: " << subtraction << "\n\n"; } system("pause"); return 0; }
You need to set subtraction = vectorValues[0]; before the for loop, otherwise it is just going to go back to that number every time and subtraction -= vectorValues[i]; wont do anything.

 12345678 subtraction = vectorValues[0]; // Number to start from (eg. 10) for(int i = 1; i < vectorValues.size(); i++) { subtraction -= vectorValues[i]; //Loop through and subtract each number cout << vectorValues[i] << ", "; cout << "The subtraction of this value: " << subtraction << "\n\n"; }
Man thanks so much straight forward answer to my problem unlike some other people on those other boards that just beat about the bush but don't provide any type of guide, hints, or solution. Much appreciated.
You can do your task applying standard algorithm std::accumulate to any sequence. For example

 123456789101112131415161718192021222324252627 #include #include #include #include #include #include int main() { int a[] = { 10, 4, 3 }; int x = std::accumulate( std::next( std::begin( a ) ), std::end( a ), *std::begin( a ), std::minus() ); std::cout << "x = " << x << std::endl; std::vector v( std::begin( a ), std::end( a ) ); x = std::accumulate( std::next( std::begin( v ) ), std::end( v ), *std::begin( v ), std::minus() ); std::cout << "x = " << x << std::endl; std::list l( std::begin( a ), std::end( a ) ); x = std::accumulate( std::next( std::begin( l ) ), std::end( l ), *std::begin( l ), std::minus() ); std::cout << "x = " << x << std::endl; }
Last edited on
Topic archived. No new replies allowed.