Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int (*functions[4])(int) = {
  6. [](int x) -> int {
  7. return x + 10;
  8. },
  9. [](int x) -> int {
  10. return x - 10;
  11. },
  12. [](int x) -> int {
  13. return x * 10;
  14. },
  15. [](int x) -> int {
  16. return x / 10;
  17. }};
  18.  
  19. int *map(int *arr, int n, int (*f)(int))
  20. {
  21. int *new_arr = new int[n];
  22. for (int i = 0; i < n; i++)
  23. {
  24. new_arr[i] = f(arr[i]);
  25. }
  26. return new_arr;
  27. }
  28.  
  29. int main()
  30. {
  31. int fun;
  32. int n = 10;
  33. int offset;
  34. cout << "Select function: " << endl;
  35. cin >> fun;
  36. cout << "Select offset: " << endl;
  37. cin >> offset;
  38. int arr[n];
  39. cout << "Original array: " << endl;
  40. for (int i = 0; i < n; i++)
  41. {
  42. arr[i] = i + offset;
  43. cout << arr[i] << endl;
  44. }
  45. cout << "Mapped array: " << endl;
  46. int *new_arr = map(arr, n, functions[fun]);
  47. for (int i = 0; i < n; i++)
  48. {
  49. cout << new_arr[i] << endl;
  50. }
  51.  
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement