#include <iostream>
#include <vector>
using namespace std;
int main()
{
int typedNos;
if (cin >> typedNos)
{
vector <int> inputNos{ typedNos };
while (cin >> typedNos)
{
inputNos.push_back(typedNos);
}
for (decltype (inputNos.size()) n = 1; n < inputNos.size(); ++n)
{
cout << inputNos[0] + inputNos[1] << '\t' << inputNos[(2 * n) - 1] + inputNos[(2 * n)] << endl;
return 0;
}
}
else
{
cerr << " Wrong input type or no input was typed!" << endl;
//return -1;
}
}
Blockquote
Everything works fine till the output statement in the for loop is reached. The first two pairs of the vector's elements are manually added to account for zero. The rest are to be added automatically. But this only works for the first pair. So, for example, an input of: 1 2 3 4 5. Will give you an output of: 3 5. Instead of 3 5 7 9. This is where I have an issue. I have seen other methods of solving this problem but my question is why the sequence 2n (even positions) and 2n-1 (odd positions) do not work for the entire vector? Remember this question does not allow me to use iterators. Thanks.
Aucun commentaire:
Enregistrer un commentaire