SHARE
TWEET

Untitled

a guest Mar 24th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int* prefixAverages1(int *X, int n);
  6. int* prefixAverages2(int *X, int n);
  7.  
  8. int main(void)
  9. {
  10.     int *X, *A1, *A2;
  11.     int n, i;
  12.     clock_t start, end;
  13.  
  14.     scanf("%d", &n);
  15.  
  16.     X = (int*)malloc(sizeof(int)*n);
  17.     srand(time(NULL));
  18.     for(i=0; i<n; i++)
  19.         X[i] = (rand()%10000) + 1;
  20.    
  21.     start = clock();
  22.     A1 = prefixAverages1(X, n);
  23.     end = clock();
  24.     printf("%fms\n", (float)(end-start)/1000);
  25.    
  26.     start = clock();
  27.     A2 = prefixAverages2(X, n);
  28.     end = clock();
  29.     printf("%fms\n", (float)(end-start)/1000);
  30.  
  31.     free(X); free(A1); free(A2);
  32.     return 0;
  33. }
  34.  
  35. int* prefixAverages1(int *X, int n)
  36. {
  37.     int *A = (int*)malloc(sizeof(int)*n);
  38.     int i, j, sum;
  39.    
  40.     for(i=0; i<n; i++) {
  41.         sum = 0;
  42.         for(j=0; j<=i; j++)
  43.             sum = sum + X[j];
  44.         A[i] = (float)sum/(i+1) + 0.5;
  45.     }
  46.     return A;
  47. }
  48.  
  49. int* prefixAverages2(int *X, int n)
  50. {
  51.     int *A = (int*)malloc(sizeof(int)*n);
  52.     int i, sum = 0;
  53.  
  54.     for(i=0; i<n; i++) {
  55.         sum = sum +  X[i];
  56.         A[i] = (float)sum/(i+1) + 0.5;
  57.     }
  58.     return A;
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top