How to square all elements of array in C++

1 Answer

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

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


void print(int arr[], int size)
{
	copy(arr, arr + size, std::ostream_iterator<int>(cout, " "));

	cout << endl;
}

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

	int size = sizeof(arr) / sizeof(int);

	std::transform(arr, arr + size, arr, arr, std::multiplies<int>());

	print(arr, size);

	return 0;
}


/*
run:

1 4 9 16 25

*/

 



answered Jan 15, 2018 by avibootz

Related questions

1 answer 188 views
1 answer 193 views
1 answer 632 views
1 answer 169 views
1 answer 149 views
1 answer 115 views
1 answer 196 views
...