aboutsummaryrefslogtreecommitdiff
path: root/semestr-4/ask/lista12/rozw.c
blob: 84d66c68f0eda31b6e9c58f88784f6c64f880a93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>

/* Zadanie 1 */
 
void swap(long *xp, long *yp) {
  *xp = *xp + *yp; /* x+y */
  *yp = *xp - *yp; /* x+y-y = x */
  *xp = *xp - *yp; /* x+y-x = y */
}

void swap2(long *xp, long *yp) {
  long x = *xp, y = *yp;
  x = x + y, y = x - y, x = x - y;
  *xp = x, *yp = y;
}

/* Zadanie 2 */

__attribute__((noinline))
size_t my_strlen(const char *s) {
  size_t i = 0;
  while (*s++)
  i++;
  return i;
}

const char *my_index(const char *s, char v) {
  for (size_t i = 0; i < my_strlen(s); i++)
    if (s[i] == v)
      return &s[i];
  return 0;
}

/* Zadanie 3 */
void foobar(long a[], size_t n, long y, long z) {
  for (int i = 0; i < n; i++) {
    long x = y - z;
    long j = 7 * i;
    a[i] = j + x * x; 
  }
}

void foobar_decomp(long a[], size_t n, long y, long z) {
  z -= y;
  z *= z;
  for (int i = 0; i < n; i++) {
    a[i] = z;
    z += 7;
  }
}

/* Zadanie 4 */

long neigh(long a[], long n, long i, long j) {
  long ul = a[(i-1)*n + (j-1)];
  long ur = a[(i-1)*n + (j+1)];
  long dl = a[(i+1)*n - (j-1)];
  long dr = a[(i+1)*n - (j+1)];
  return ul + ur + dl + dr;
}

long neigh_better(long a[], long n, long i, long j) {
  long idx = (i-1) * n + (j-1);
  // n *= 2;
  long ul = a[idx];
  long dl = a[idx + n*2];
  idx += 2;
  long ur = a[idx];
  long dr = a[idx + n*2];
  return ul + dl + ur + dr;
}