Adding first and last vector

Hey guys i need to write program which will sumarize pairs of integers i mean that it will store those numbers in vector and sumarize pairs of them.First vector and last . 2nd and 6th etc.. Sry for my english.My code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>
using namespace std;

int main()
{
int liczba,suma = 0;
vector<int> l;
cout << "Enter integers : ";
while(cin >> liczba)
l.push_back(liczba);
int l_size = l.size();
for(int i = 0; i < l_size;i++)
{
suma = l[i] + l[l_size--];
cout << "Sum of " l[i] << " + " << l[l_size--] << " = " << suma << endl;
}
}


And it's not working . Only first pair is working and after that with 2nd pair l[i] is adding some random integers not those which i entered. Any help why?
Last edited on
Changes:
1- Pre-decrement l_size in line 15
2- Do not increment l_size in line 16.
Last edited on
1
2
3
for(int i = 0; i < l_size;i++)
suma = l[i] + l[l_size--];
cout << "Sum of "  << l[i] << " + " << l[l_size] << " = " << suma << endl;

Now it's properly showing numbers but when i example enter 2 8 6 4 then i get 2 + 4 = 2 . Idk why .
Last edited on
You did not do Changes 1.
Change l_size-- to --l_size
Last edited on
Now i got unlimited loop with numbers
0 + 0 = 0
0 + 0 = 0
....

I think that i shouldn't increment size of vector...
Very sorry mate. I meant pre-dercrement.
1
2
3
4
5
	for(int i = 0; i < l_size;i++)
	{
		suma = l[i] + l[--l_size];
		cout << "Sum of " << l[i] << " + " << l[l_size] << " = " << suma << endl;
	}
Last edited on
Still not working ...
When i enter : 2 4 6 8
a
then i got :
Sum of 8 + 8 = 16 ...

And where is 2 , 4 ,6 ?
Last edited on
Before tackling the matter at hand, I would like to note some things:

I would advice against including the entirety of the standard namespace since the names inside it are bound to eventually clash with any identifiers declared in your code. Instead, provide using declarations for any names inside std that you need, for instance

using std::vector; using std::cout; using std::cin; // So on, and so forth.

When you defined liczba and suma, you initialized only suma to 0. It may be tempting to expect both objects to be initialized to 0 but that is not the case.

I'd like to comment on the missing << (bitwise left shit operator) between "Sum of " and l[i] in the final output stream.

Finally, the type of l_size should not be int, but rather std::vector<int>::size_type. The size_type member is guaranteed to hold the size of any vector in memory. It is considered to be tedious to write by some, so why not let auto deduce the type from the initializer for you?

auto l_size = l.size(); // l_size is a std::vector<int>::size_type

Alright, to the matter of hand:

The problem is in the for loop of your code. You're decrementing l_size twice each iteration.

You also happen to be using a postfix decrement operator. It decrements your objects value by 1 but it is the unchanged value that forms the subexpressions in each iteration.

Consider this example:

1
2
    int i = 5;
    std::cout << i-- << std::endl; // Prints 5 but the value of i is 4! 


Now consider this example:

1
2
int i = 5;
    std::cout << --i << std::endl; // Prints 4 and the value of i is 4. 


The latter example used the prefix operator which decrements the value of its operand and returns the modified value.

The output of the first iteration looks perfectly valid because it decrements the value of l_size twice but does so with the postfix decrement operator and therefore makes it look like the value is only changed once. That is not the case since the value of l_size is l_size - 2 after each iteration.

You should be able to solve it yourself now if you followed along. I'll respond with the answer if you can't. I also suggest looking into iterators, they simplify this kind of operations greatly.

This is probably just a throw-away account, but in the future, consider naming your objects in English when posting code to forums.
I dont know why it does not work for you.
This is the code i'm running.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
using namespace std;

int main()
{
	int liczba,suma = 0;
	vector<int> l;
	cout << "Enter integers : ";
	
	while(cin >> liczba)
		l.push_back(liczba);

	int l_size = l.size();
	for(int i = 0; i < l_size;i++)
	{
		suma = l[i] + l[--l_size];
		cout << "Sum of " << l[i] << " + " << l[l_size] << " = " << suma << endl;
	}
}


Enter integers : 2 4 6 8 c
Sum of 2 + 8 = 10
Sum of 4 + 6 = 10
"I'd like to comment on the missing << (bitwise left shit operator) between "Sum of " and l[i] in the final output stream."
It's missing because i wrote this code in my telephone since i can't copy text in this phone ...

"This is probably just a throw-away account, but in the future, consider naming your objects in English when posting code to forums." We will see.

Anyway thanks for your advices i will remember them ;) . I'm not best in c++ because i'm still learning it ;).

@ShadowCode thanks now it's working when i copied your code . I had same code but idk why it was not working .

Last edited on
Topic archived. No new replies allowed.