Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <time.h>
- //amount of data in source array
- #define N 4096
- // K defenition from task
- #define K 32
- // maximum integer in source array
- #define MAX 256
- #define THREADS 4
- #define res(cond) abs(result[cond].a) * abs(result[cond].b)
- struct equation {
- int a;
- int b;
- };
- int *array;
- struct equation *result;
- int result_size=2;
- int result_in=0;
- void generate_array()
- {
- srandom(time(NULL));
- int i;
- array = (int *)malloc(sizeof(int)*N);
- for (i=0;i<N;i++)
- {
- array[i] = rand() % MAX;
- }
- }
- void print(int *data)
- {
- int i;
- for(i=0;i<N;i++)
- {
- printf("%i ", data[i]);
- }
- printf("\n");
- }
- struct equation make_equation(int a, int b)
- {
- return *(struct equation[]){{a,b}};
- }
- int main(void)
- {
- // loop index
- int i,j;
- // equation variables:
- int a,b;
- if (THREADS < 4)
- {
- fprintf(stderr, "Minimum threads are: 4. Your's: %i\n", THREADS);
- exit(1);
- }
- // allocation memory for result
- result = malloc(sizeof(struct equation)*result_size);
- generate_array();
- printf("K: %i\n\n", K);
- int max = res(0);
- struct equation tmp;
- for(i=0;i<N;++i)
- {
- for(j=0;j<N;++j)
- {
- a = array[i];
- b = array[j];
- if (b-a == K)
- {
- if (max < res(i))
- {
- max = abs(a) * abs(b);
- tmp.a = a;
- tmp.b = b;
- }
- }
- }
- }
- printf("|%i|*|%i| = %i\n", tmp.a, tmp.b, max);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement