Ok, I have another challenge. I have a question that asks me to “Read a set of integers into a vector. Print the sum of each pair of adjacent elements . Change your program so that it prints out the sum of the first and last elements, followed by the sum of the second and second-to-last, and so on.”
Currently I have managed to get it to add consecutive numbers, not exactly in pairs, but as close as I can get it.
The only operators I have learnt thus far for vectors, and what the question wants me to use are: v.empty(); v.size(); v.push_back(t); v[n]; v1 = v2; v1 = {a,b,c...}; v1 == v2; v1 != v2; <, <=, >, >=
Here is my code as it stands now:
#include <iostream>
#include <string>
#include <vector>
using std::string; using std::vector; using std::cout; using std::cin; using std::endl;
int main ()
{
vector<unsigned> numbers {5, 6, 7, 8, 9, 10, 11};
unsigned sum;
decltype(numbers.size()) count;
for (count = 0; count < numbers.size(); ++count){
sum = (numbers[count]++) + (numbers[count]);
cout << sum << " ";
}
}
So I can get it to add 5+6; 6+7; 7+8 etc… but as for each pair and all the rest, not 100% sure where to start. Any guidance will be greatly appreciated!
Below two loops that print (1) sum of adjacent pairs and (2) the sum of opposite elements.
Output on LiveWorkSpace