Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <time.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. struct vector3 {
  7.   int x, y ,z;
  8. };
  9.  
  10. //__attribute__((annotate("sensitive"))) struct vector3 asdfadsfasd;
  11.  
  12. void add(struct vector3* v, struct vector3* w) {
  13.   v->x += w->x;
  14.   v->y += w->y;
  15.   v->z += w->z;
  16. }
  17.  
  18. void copy(struct vector3* dst, struct vector3* src) {
  19.   memcpy(dst, src, sizeof(struct vector3));
  20. }
  21.  
  22. void swap(struct vector3* a, struct vector3* b){
  23.   int x = a->x;
  24.   int y = a->y;
  25.   int z = a->z;
  26.  
  27.   a->x = b->x;
  28.   a->y = b->y;
  29.   a->z = b->z;
  30.  
  31.   b->x = x;
  32.   b->y = y;
  33.   b->z = z;
  34. }
  35.  
  36. int get_smallest(struct vector3* vs, int start, int N);
  37.  
  38. void selection_sort(struct vector3* vs, int N) {
  39.   for (int i = 0; i < N; i++) {
  40.     int idx = get_smallest(vs, i, N);
  41.     swap(&vs[idx], &vs[i]);
  42.   }
  43. }
  44.  
  45. int get_smallest(struct vector3* vs, int start, int N) {
  46.   int smallest = vs[start].x;
  47.   int idx = start;
  48.   for (int i = start; i < N; ++i) {
  49.     if (vs[i].x < smallest) {
  50.       idx = i;
  51.       smallest = vs[i].x;
  52.     }
  53.   }
  54.   return idx;
  55. }
  56.  
  57. struct vector3* make_random_arr(int N) {
  58.   struct vector3* arr = malloc(sizeof(struct vector3)*N);
  59.   for (int i = 0; i < N; ++i) {
  60.     arr[i].x = rand() % 100;
  61.     arr[i].y = rand() % 100;
  62.     arr[i].z = rand() % 100;
  63.   }
  64.   return arr;
  65. }
  66.  
  67. int main(int argc, char** argv) {
  68.   printf("argc = %i\n", argc);
  69.   printf("argv[0] = %s\n", argv[0]);
  70.  
  71.   int x;
  72.   printf("&x = %p\n", &x);
  73.  
  74.   srand(time(NULL));
  75.   struct vector3* arr = make_random_arr(3);
  76.   struct vector3* arr2 = make_random_arr(10);
  77.   struct vector3* a = &arr[0];
  78.   struct vector3* b = &arr[1];
  79.   struct vector3* c = &arr[2];
  80.  
  81.   if (argc == 1) {
  82.     add(c, b);
  83.   } else {
  84.     add(c, b);
  85.     add(c, b);
  86.   }
  87.  
  88.   selection_sort(arr, 3);
  89.   selection_sort(arr2, 10);
  90.  
  91.   printf("should be random: %i %i %i\n", a->x, a->y, a->z);
  92.   printf("should be sorted: %i %i %i\n", arr2[0].x, arr2[1].x, arr2[2].x);
  93.  
  94.  
  95.   return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement