Advertisement
BlueBear

michael.c

Jan 12th, 2022
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.88 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int add_up (int *a, int** b, int num_elements);
  5.  
  6. int main()
  7. {
  8.     int orders[5] = {100, 220, 37, 16, 98};
  9.     int *orders_sum = (int *)malloc(sizeof(int) * 5);
  10.    
  11.     if (orders_sum == NULL) {
  12.         printf("Memory not allocated.\n");
  13.         exit(0);
  14.     }
  15.  
  16.     printf("Total orders is %d\n", add_up(orders, &orders_sum, 5));
  17.    
  18.     for (int i = 0; i < 5; ++i)
  19.     {
  20.         printf("sum of the elements at %d is %d\n", i, orders_sum[i]);
  21.     }
  22.    
  23.  
  24.     return 0;
  25. }
  26.  
  27. int add_up (int *a, int** b, int num_elements)
  28. {
  29.    int total = 0;
  30.  
  31.    for (int k = 0; k < num_elements; ++k)
  32.    {
  33.         total += a[k];
  34.    }
  35.    
  36.    for (int i = 0; i < num_elements; ++i)
  37.    {
  38.         for (int k = num_elements - 1; i <= k; --k)
  39.         {
  40.            
  41.             (*b)[i] += a[k];
  42.         }
  43.     }
  44.  
  45.     return (total);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement