Let's Get Functional

Total Page:16

File Type:pdf, Size:1020Kb

Let's Get Functional

CST223 Lab #4 Let's get functional

Functional programming is a different way to solve problems. While some modern programming languages are not known as functional languages, but they have incorporated functional programming paradigm via libraries.

In this lab, we will explore some functional style of programming available in the C++ standard library. No explicit recursions! For these three functions, you will need to #include and #include . These functions are in the std namespace so you’ll want to do using appropriately.

Please review the powerpoint slides on these functions first. You are welcome to use the C++ standard library class vector and iterators for these questions if you want, but regular arrays and pointers work just as well.

Problem Set #1: Function Pointers

For the following problems, you need to create functions and pass function pointers to for_each, accumulate or transform algorithms. 1. Create an array and fill it with some numbers. Use for_each function to multiply 5 to each number in an array. 2. Use for_each function to print out all even numbers in an array of numbers. 3. Use for_each function to add 1 to each number in the first half of the array and use another for_each function to add 2 to each number in the second half of the array. 4. Use accumulate to find the maximum value in an array of numbers. 5. Use accumulate to count the number of 7’s in an array of numbers. 6. Use accumulate to add up all the ASCII values in a character array. 7. Use transform to add 5 to each number in the input array. 8. Use transform that takes two input arrays and multiplies the values together and store in the output array. 9. Use transform to find the number of 5’s in an array of number. (Hint: use bool or int array as the output)

Problem Set #2: Functions and adapters Solve the same problems above, except instead of passing function pointers, use function objects, lambda expressions, TR1::functions or function adapters.

Recommended publications