AmidamaruZXC

Untitled

Feb 4th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int mul(int a, int b)
  5. {
  6.     return a * b;
  7. }
  8.  
  9. int fold(int* array, int n, int (*mul)(int, int))
  10. {
  11.     int result = mul(array[0], array[1]);
  12.     for (int i = 2; i < n; i++)
  13.         result = mul(result, array[i]);
  14.     return result;
  15. }
  16.  
  17. int main()
  18. {
  19.     int* array;
  20.     int n;
  21.     printf("Enter the size of the array: ");
  22.     scanf("%d", &n);
  23.     array = (int*)malloc(n * sizeof(int*));
  24.     for (int i = 0; i < n; i++)
  25.     {
  26.         printf("Enter array[%d]: ", i);
  27.         scanf("%d", &array[i]);
  28.     }
  29.     printf("Original array: ");
  30.     for (int i = 0; i < n; i++)
  31.         printf("%d ", array[i]);
  32.     printf("\nThe product of all array numbers: %d\n", fold(array, n, mul));
  33.     free(array);
  34.     return 0;
Advertisement
Add Comment
Please, Sign In to add comment