Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <time.h>
- #include <stdlib.h>
- #include <string.h>
- struct vector3 {
- int x, y ,z;
- };
- //__attribute__((annotate("sensitive"))) struct vector3 asdfadsfasd;
- void add(struct vector3* v, struct vector3* w) {
- v->x += w->x;
- v->y += w->y;
- v->z += w->z;
- }
- void copy(struct vector3* dst, struct vector3* src) {
- memcpy(dst, src, sizeof(struct vector3));
- }
- void swap(struct vector3* a, struct vector3* b){
- int x = a->x;
- int y = a->y;
- int z = a->z;
- a->x = b->x;
- a->y = b->y;
- a->z = b->z;
- b->x = x;
- b->y = y;
- b->z = z;
- }
- int get_smallest(struct vector3* vs, int start, int N);
- void selection_sort(struct vector3* vs, int N) {
- for (int i = 0; i < N; i++) {
- int idx = get_smallest(vs, i, N);
- swap(&vs[idx], &vs[i]);
- }
- }
- int get_smallest(struct vector3* vs, int start, int N) {
- int smallest = vs[start].x;
- int idx = start;
- for (int i = start; i < N; ++i) {
- if (vs[i].x < smallest) {
- idx = i;
- smallest = vs[i].x;
- }
- }
- return idx;
- }
- struct vector3* make_random_arr(int N) {
- struct vector3* arr = malloc(sizeof(struct vector3)*N);
- for (int i = 0; i < N; ++i) {
- arr[i].x = rand() % 100;
- arr[i].y = rand() % 100;
- arr[i].z = rand() % 100;
- }
- return arr;
- }
- int main(int argc, char** argv) {
- printf("argc = %i\n", argc);
- printf("argv[0] = %s\n", argv[0]);
- int x;
- printf("&x = %p\n", &x);
- srand(time(NULL));
- struct vector3* arr = make_random_arr(3);
- struct vector3* arr2 = make_random_arr(10);
- struct vector3* a = &arr[0];
- struct vector3* b = &arr[1];
- struct vector3* c = &arr[2];
- if (argc == 1) {
- add(c, b);
- } else {
- add(c, b);
- add(c, b);
- }
- selection_sort(arr, 3);
- selection_sort(arr2, 10);
- printf("should be random: %i %i %i\n", a->x, a->y, a->z);
- printf("should be sorted: %i %i %i\n", arr2[0].x, arr2[1].x, arr2[2].x);
- return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement