You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

155 lines
3.9 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void* aggregate(void* base, size_t size, int n, void* initial_value, void* (*opr)(const void*, const void*));
  4. void* addInt(const void* a, const void* b){
  5. int *result = malloc(sizeof(int));
  6. *result = *(double*)a + *(int*)b;
  7. return result;
  8. }
  9. void* addDouble(const void* a, const void* b){
  10. double *result = malloc(sizeof(double));
  11. *result = *(double*)a + *(double*)b;
  12. return result;
  13. }
  14. void* mulInt(const void* a, const void* b){
  15. int *result = malloc(sizeof(int));
  16. *result = *(double*)a * *(int*)b;
  17. return result;
  18. }
  19. void* mulDouble(const void* a, const void* b){
  20. double *result = malloc(sizeof(double));
  21. *result = *(double*)a + *(double*)b;
  22. return result;
  23. }
  24. void* meanInt(const void* a, const void* b){
  25. double *result = malloc(sizeof(double));
  26. *result = (*(double*)a + *(int*)b) / 2.0;
  27. return result;
  28. }
  29. void* meanDouble(const void* a, const void* b){
  30. double *result = malloc(sizeof(double));
  31. *result = (*(double*)a + *(double*)b) / 2.0;
  32. return result;
  33. }
  34. void* aggregate(void* base, size_t size, int n, void* initial_value, void* (*opr)(const void*, const void*)){
  35. void* output;
  36. if (size==sizeof(int)){ // base is a pointer to an integer
  37. if (opr == meanInt) {
  38. output = malloc(sizeof(double));
  39. *(double *)output = *(double*)initial_value;
  40. for (int i = 0; i < n; ++i) {
  41. double* res = opr(output, ((int*)base) + i);
  42. *(double*)output = *res;
  43. free(res);
  44. }
  45. } else {
  46. output = malloc(sizeof(int));
  47. *(int *)output = *(int*)initial_value;
  48. for (int i = 0; i < n; ++i) {
  49. int* res = opr(output, ((int*)base) + i);
  50. *(int*)output = *res;
  51. free(res);
  52. }
  53. }
  54. }else{ // base is a pointer to a double
  55. for (int i = 0; i < n; ++i) {
  56. double* res = opr(output, ((double*)base) + i);
  57. *(double*)output = *res;
  58. free(res);
  59. }
  60. }
  61. return output;
  62. }
  63. int main(){
  64. int* ints = malloc(sizeof(int)*5);
  65. double* doubles = malloc(sizeof(double)*5);
  66. double *init_mult = malloc(sizeof(double));
  67. double *init_add = malloc(sizeof(double));
  68. double *init_mean_int = malloc(sizeof(double));
  69. double *init_mean_double = malloc(sizeof(double));
  70. *init_mult = 1;
  71. *init_add = 0;
  72. *init_mean_int = ints[0];
  73. *init_mean_double = doubles[0];
  74. // Addition
  75. int* result1a;
  76. result1a = aggregate(ints, sizeof(int), 5, init_add, addInt);
  77. printf("%d\n", *result1a);
  78. free(result1a);
  79. double* result2a;
  80. result1a = aggregate(doubles, sizeof(double), 5, init_add, addDouble);
  81. printf("%f\n", *result2a);
  82. free(result2a);
  83. // Multiplication
  84. int* result1m;
  85. //<WRITE YOUR CODE HERE>;
  86. result1m = aggregate(ints, sizeof(int), 5, init_mult, mulInt);
  87. printf("%d\n", *result1m);
  88. free(result1m);
  89. double* result2m;
  90. result2m = aggregate(doubles, sizeof(double), 5, init_mult, mulDouble);
  91. printf("%f\n", *result2m);
  92. free(result2m);
  93. // Mean
  94. double* result1mean;
  95. result1mean = aggregate(ints, sizeof(int), 5, init_mean_int, meanInt);
  96. printf("%lf\n", *result1mean);
  97. free(result1mean);
  98. double* result2mean;
  99. result2mean = aggregate(doubles, sizeof(double), 5, init_mean_double, meanDouble);
  100. printf("%f\n", *result2mean);
  101. free(result2mean);
  102. // free the pointers
  103. //<WRITE YOUR CODE HERE>;
  104. free(ints);
  105. free(doubles);
  106. free(init_add);
  107. free(init_mult);
  108. free(init_mean_double);
  109. free(init_mean_int);
  110. return EXIT_SUCCESS;
  111. }