/*
8. Write a program that reads and stores a series of integers and then
computes the sum of the first N integers. First ask for N, then read
the values into a vector, then calculate the sum of the first
N values. For example:
“Please enter the number of values you want to sum:”
3
“Please enter some integers (press '|' to stop):”
12 23 13 24 15 |
“The sum of the first 3 numbers ( 12 23 13 ) is 48.”
Handle all inputs. For example, make sure to give an error message if
the user asks for a sum of more numbers than there are in the vector.

10. Modify the program from exercise 8 to use double instead of int. 
Also, make a vector of doubles containing the N–1 differences between 
adjacent values and write out that vector of differences.
*/



#include "std_lib_facilities.h"

double sumOfN(vector<double>, int);
vector<double> differences(vector<double>);

int main() {
	int N{ 0 };
	double n{ 0 };
	vector<double> vDoubles, vectorOfdifferences;
	do {
		vDoubles.clear();
		cout << "Enter the number of doubles to add ('\' to quit): ";
		cin >> N;
		while (cin >> n) {
			vDoubles.push_back(n);
		}
		cin.clear();
		cin.ignore(100, '\n');
		cout << "The sum of the first " << N << " doubles is " << sumOfN(vDoubles, N) << '\n';
		cout << "The vector of difference is ";
		vectorOfdifferences = differences(vDoubles);
		for (double d : vectorOfdifferences) cout << d << " ";
		cout << '\n';
	} while (1);
}

//precondition: N>=v.size();
double sumOfN(vector<double> v, int N) {
	if (!(v.size() >= N)) error("v too small for that.");
	int sum{ 0 };
	for (int i = 0; i < N; ++i)
		sum += v[i];
	return sum;
}

vector<double> differences(vector<double> v) {
	vector<double> returnV;
	if (v.size() < 2) return returnV;
	for (int i = 1; i < v.size(); ++i)
		returnV.push_back(v[i] - v[i-1]);
	return returnV;
}