/*
 * Copyright ©2026 Soham Pardeshi. All rights reserved.
 * Permission is hereby granted to students registered for University of
 * Washington CSE 333 for use solely during Summer Quarter 2026 for
 * purposes of the course. No other use, copying, distribution, or
 * modification is permitted without prior written consent. Copyrights
 * for third-party components of this work must be honored. Instructors
 * interested in reusing these course materials should contact the author.
 */

#include <stdio.h>   // printf
#include <stdlib.h>  // EXIT_SUCCESS

#define LEN 4

int Negate(int num) {return -num;}
int Square(int num) {return num * num;}

// perform operation pointed to on each array element
void Map(int a[], int len, int (* op)(int n)) {
  for (int i = 0; i < len; i++) {
    a[i] = (*op)(a[i]);  // dereference function pointer
  }
}

int main(int argc, char** argv) {
  int arr[LEN] = {-1, 0, 1, 2};
  int (* op)(int n);  // function pointer called 'op'
  op = Square;  // assign to function
                // function name returns addr (like array)
  Map(arr, LEN, op);
  for (int i = 0; i < LEN; i++) {
    printf("%d%c", arr[i], i == (LEN-1) ? '\n' : ' ');
  }
  return EXIT_SUCCESS;
}