-
Notifications
You must be signed in to change notification settings - Fork 2
/
perc.c
52 lines (45 loc) · 852 Bytes
/
perc.c
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
#include <stdio.h>
#include <stdlib.h>
#define SITE(A,X,Y) (A[(X)*Ly+(Y)])
void print_array(int *a, int Lx, int Ly) {
int i,j;
for(i=0; i<Lx; ++i) {
for(j=0; j<Ly; ++j) {
SITE(a,i,j) = SITE(a,i,j) - 1;
printf("%d\t", SITE(a,i,j));
}
printf("\n");
}
printf("\n");
}
void set_array(int *a, int N) {
int i;
for(i=0; i<N; ++i)
if (drand48() < 0.5)
a[i] = 1;
else a[i] = 0;
}
void do_array() {
int Lx = 3;
int Ly = 4;
int N = Lx * Ly;
int *a = (int *) malloc(sizeof(int)*N);
set_array(a,N);
print_array(a,Lx,Ly);
free(a);
}
/* Just to show it is compatible with C arrays */
#define LX 3
#define LY 4
void do_array2() {
int Lx = 3;
int Ly = 4;
int N = Lx * Ly;
int a[LX][LY];
set_array((int *)a,N);
print_array((int *)a,Lx,Ly);
}
int main () {
do_array();
do_array2();
}