Advertisement
Bkmz

Untitled

Apr 24th, 2012
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <malloc.h>
  5. #include <time.h>
  6.  
  7. //amount of data in source array
  8. #define N 4096
  9. // K defenition from task
  10. #define K 32
  11.  
  12. // maximum integer in source array
  13. #define MAX 256
  14. #define THREADS 4
  15.  
  16. #define res(cond) abs(result[cond].a) * abs(result[cond].b)
  17.  
  18. struct equation {
  19.     int a;
  20.     int b;
  21. };
  22.  
  23. int *array;
  24.  
  25. struct equation *result;
  26. int result_size=2;
  27. int result_in=0;
  28.  
  29. void generate_array()
  30. {
  31.     srandom(time(NULL));
  32.     int i;
  33.     array = (int *)malloc(sizeof(int)*N);
  34.     for (i=0;i<N;i++)
  35.     {
  36.         array[i] = rand() % MAX;
  37.     }
  38. }
  39.  
  40. void print(int *data)
  41. {
  42.     int i;
  43.     for(i=0;i<N;i++)
  44.     {
  45.         printf("%i ", data[i]);
  46.     }
  47.     printf("\n");
  48. }
  49.  
  50. struct equation make_equation(int a, int b)
  51. {
  52.     return *(struct equation[]){{a,b}};
  53. }
  54.  
  55.  
  56. int main(void)
  57. {
  58.     // loop index
  59.     int i,j;
  60.     // equation variables:
  61.     int a,b;
  62.  
  63.     if (THREADS < 4)
  64.     {
  65.         fprintf(stderr, "Minimum threads are: 4. Your's: %i\n", THREADS);
  66.         exit(1);
  67.     }
  68.  
  69.     // allocation memory for result
  70.     result = malloc(sizeof(struct equation)*result_size);
  71.  
  72.     generate_array();
  73.  
  74.     printf("K: %i\n\n", K);
  75.  
  76.     int max = res(0);
  77.     struct equation tmp;
  78.  
  79.  
  80.     for(i=0;i<N;++i)
  81.     {
  82.         for(j=0;j<N;++j)
  83.         {
  84.             a = array[i];
  85.             b = array[j];
  86.  
  87.             if (b-a == K)
  88.             {
  89.                 if (max < res(i))
  90.                 {
  91.                     max = abs(a) * abs(b);
  92.                     tmp.a = a;
  93.                     tmp.b = b;
  94.                 }
  95.             }
  96.         }
  97.     }
  98.  
  99.  
  100.     printf("|%i|*|%i| = %i\n", tmp.a, tmp.b, max);
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement