Repository navigation
Expand file tree
/
Copy pathhamming.c
More file actions
142 lines (121 loc) · 2.57 KB
/
Copy pathhamming.c
File metadata and controls
142 lines (121 loc) · 2.57 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <omp.h>
int* hamming_distance( char ** a1,char ** a2, int m, int n, int l);
double gettime(void);
int main(int argc, char **argv)
{
if(argc < 4)
{
fprintf(stderr, "\n~Arguments missing!!\nTry 'make M=100 N=100 L=100 T=4'\n\n");
exit(-1);
}
int m = atoi(argv[1]);
int n = atoi(argv[2]);
int l = atoi(argv[3]);
//initalizing rand()
srand(time(NULL)+1);
char ** arr1 = (char**)malloc(m*sizeof(*arr1));
char ** arr2 = (char**)malloc(n*sizeof(*arr2));
int i,j;
//filling first array
for(j = 0; j < m; j++)
{
if((arr1[j] = (char *)malloc(l * sizeof(char))) == NULL)
printf("malloc error\n");
//random generated character starting with ASCI 'space' (32-126)
for(i = 0; i < l; i++)
arr1[j][i] = ' ' + rand() % 94;
}
//filling second array
for(j = 0; j < n; j++)
{
if((arr2[j] = (char *)malloc(l * sizeof(char))) == NULL)
printf("malloc error\n");
for(i = 0; i < l; i++)
arr2[j][i] = ' ' + rand() % 94;
}
//Printing total time and distance
printf("\n----- Hamming Sequential");
int *distance;
double t1 = gettime();
distance = hamming_distance(arr1,arr2,m,n,l);
double t2 = gettime();
printf("\ntime: %f\n",(t2-t1));
long long dist = 0;
for(int h = 0; h<m*n;h++)
dist += distance[h];
printf("hamming: %lld\n", dist);
//Memory freeing
for(j = 0; j < m; j++){
free(arr1[j]);
arr1[j]=NULL;
}
for(j = 0; j < n; j++){
free(arr2[j]);
arr2[j]=NULL;
}
free(arr1);
free(arr2);
free(distance);
return 0;
}
/**
args: 2 arrays of char pointers, sizes of arrays
ret.val : calculated hamming distance
**/
int* hamming_distance(char ** a1,char ** a2, int m, int n, int l)
{
int i,j,k = 0;
int count = 0;
int offset = -1;
int min,max = 0;
int small_index = 0;
int * distance = (int*)malloc(m*n*sizeof(int));
for(int g=0;g<m*n;g++)distance[g]=0;
//Setting the correct array limits
if(m < n)
{
small_index = 1;
min = m;
max = n;
}
else
{
min = n;
max = m;
}
//Iterating through Arrays A and B
for(i = 0; i < min; i++)
{
for(j = 0; j < max; j++)
{
//Comparing strings
count = 0;
for(k = 0; k < l; k++)
{
if(small_index)
{
if(a1[i][k] != a2[j][k])
count++;
}
else
{
if(a2[i][k] != a1[j][k])
count++;
}
}
//Writing partial distance to array "distance[MxN]"
distance[++offset] = count;
}
}
return distance;
}
double gettime(void)
{
struct timeval ttime;
gettimeofday(&ttime , NULL);
return ttime.tv_sec + ttime.tv_usec * 0.000001;
}