How to get the average between two elements from two vector and transform it into a third vector in C++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;

double avg(double a, double b)
{
	return (a + b) / 2 ;
}

int main()
{
	vector<double> vec1{ 1, 2, 3, 4, 5 }, vec2{ 10, 20, 30, 40, 50 }, vec3(5);

	transform(vec1.begin(), vec1.end(), vec2.begin(), vec3.begin(), avg);

	for (double val : vec3)
		cout << val << " ";
	
	cout << endl;

	return 0;
}

/*
run:

5.5 11 16.5 22 27.5

*/

 



answered May 1, 2018 by avibootz
...