Trying to understand a code.

I imagine this is a stupid question, I'm sorry, but I was trying to do this exercise,
"(...) make a vector of doubles containing the N-1 differences between adjacent values and write out that vector of differences."

I could figure it out some parts on my own, but in the end I decided to look up for a solution and cause I was really lost and I had trouble understanding what the exercise was asking, I found this it works and I understand what this code does now, but I can't figure out why you have this line:

 
diffs.push_back(val - vals[vals.size() - 2]);


specifically the '-2' whats the logic the author use to substract -2 to make the function work and write out the difference.

and this is the program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "std_lib_facilities.h"

int main()
try
{
	vector<double> vals;
	vector<double> diffs;
	double val = 0;
	int n = 0;

	//get input calculate differences.
	cout << "Please enter the number of values you want to sum: \n";
	cin >> n;
	if (n < 1) error("you have to sum at least one value!");
	cout << "Please enter some doubles (press'|' to stop)\n";
	while (cin >> val) {
		vals.push_back(val);
		if (vals.size() >= 2)
			diffs.push_back(val - vals[vals.size() - 2]);
		
	
		
	}
	//print vector of differences
	if (vals.size() < 2)
		cout << "You need at least two values to get a vector of differences\n";
	else {
		cout << "Vector of differences:\n";
		for (int i = 0; i < diffs.size(); ++i)
			cout << diffs[i] << endl;
	}

	if (vals.size() < n) error("You wanted to sum more values than you entered, namely", n);

	double sum = 0;
	cout << "The sum of the first: ";
	if (n > 1) cout << n << ' ';
	cout << " ( ";
	for (int i = 0; i < n; ++i) {
		sum += vals[i];
		cout << vals[i] << ' ';
	}
	cout << ") is " << sum <<".\n";
	
	return 0; 


}
catch (exception& e)
{
	cerr << "Error: " << e.what() << endl;
	//keep_window_open();
	return 1;
}

catch (...) {
	cerr << "Unknown exception!\n";
	return 2;
}



if someone could provide me with an explanation I would be grateful.
thanks!
Last edited on
val is the last number you've just entered which is at location index [vals.size() - 1], since the first element is at vals[0], second at vals[1] etc. Hence the number prior to that is that vals.size() - 2. Same analogy also works for diffs
Thanks! from your explanation I drew the spaces of vals.size() in a piece of paper to understand how the vector moves. I know read this somewhere but, so the last element of a vector is always 'vector.size()-1' ? The same goes for arrays?
Yes, indexation starts from 0 for both C-style array and std::array, as well as for std::deque though for C-style arrays the size() method is not defined so you'd need to do something like this to back out the size:
1
2
    int x[] = {1, 2, 3, 4, 5};
    size_t y = sizeof(x)/sizeof(x[0]);
Last edited on
thank you very much!
Topic archived. No new replies allowed.