Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


8,090 questions

11,268 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 by avibootz
...