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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,939 questions

51,876 answers

573 users

How to use partial_sum() and external function with int array in C++

1 Answer

0 votes
#include <iostream> 
#include <numeric>

using std::partial_sum;
using std::cout;  
using std::endl;

int calc(int x, int y) { 
	return (x * y) + 1;
}

int main()
{
	int arr[] = { 1, 2, 3, 4, 5 };
	int r[5];

	/*
	r[0] = v1[0] = 1
	r[1] = v1[0] + v1[1] = (1 * 2) + 1 = 3
	r[2] = v1[0] + v1[1] + v1[2] = (1 * 2) + 1 = (3 * 3) + 1 = 10
	r[3] = v1[0] + v1[1] + v1[2] + v1[3] = (1 * 2) + 1 = (3 * 3) + 1 = 10 = (10 * 4) + 1 = 41;
	r[4] = v1[0] + v1[1] + v1[2] + v1[3] + v1[4] = (1 * 2) + 1 = (3 * 3) + 1 = 10 = 
	                                               (10 * 4) + 1 = (41 * 5) + 1 = 206;
	*/

	partial_sum(arr, arr + 5, r, std::multiplies<int>());
	partial_sum(arr, arr + 5, r, calc);
	
	for (int i = 0; i < 5; i++) 
		cout << r[i] << ' ';
	
	cout << endl;

	return 0;
}

/*
run:

1 3 10 41 206

*/

 



answered Apr 26, 2018 by avibootz

Related questions

1 answer 160 views
1 answer 162 views
1 answer 105 views
1 answer 374 views
1 answer 177 views
...