Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 answers

573 users

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
...