-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.c
209 lines (186 loc) · 5.04 KB
/
array.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "array.h"
/******************************************************************************
* Array()
*
* Arguments: fp_dict - pointer to the dictionary file
* main_array - stores dictionary words, sorted by size
*
* Returns: main_array
*
* Description: Reads a .dict file and sorts the words by their
* size in a array of arrays
*
*****************************************************************************/
char*** array(FILE* fp_dict,char*** main_array,int* counter)
{
//counter_2: counter to make the array
int counter_2[46];
int length;
char buffer[48];
int i;
//initialize arrays
for (i = 0; i < 46; i++)
{
counter[i] = 0;
main_array[i] = NULL;
counter_2[i] = 0;
}
//runs through the dictionary and counts the words of each size
while ((fscanf(fp_dict, "%s", buffer)) == 1){
length = strlen(buffer);
counter[length]++;
}
//pointer returns to the beginning
rewind(fp_dict);
//if there are words of a certain size, it creates a 2D array for each size
for (i = 0; i < 46; i++)
{
if (counter[i] != 0)
{
main_array[i] = Main_list(counter[i], i);
}
}
//runs through the dictionary and fills the 2D array with words
while ((fscanf(fp_dict, "%s", buffer)) == 1)
{
length = strlen(buffer);
strcpy(main_array[length][counter_2[length]], buffer);
counter_2[length]++;
}
return main_array;
}
/******************************************************************************
* swap()
*
* Arguments: a, b - words to be swapped
*
* Returns: void
*
* Description: Swaps two words
*
*****************************************************************************/
void swap(char** a,char** b) {
char* temp = *a;
*a = *b;
*b = temp;
}
/******************************************************************************
* quicksort()
*
* Arguments: arr - array of a certain size
* length - self explanatory
*
* Returns: void
*
* Description: sort alphabetically words of the same size
*
*****************************************************************************/
void quicksort(char * arr[], int length) {
int i, piv = 0;
if (length <= 1)
return;
for (i = 0; i < length; i++) {
// if current arra[] word < pivot word, swaps both words
if (strcmp(arr[i], arr[length - 1]) < 0) //the last word is used as a pivot
swap(arr + i, arr + piv++);
}
//move pivot to the middle
swap(arr + piv, arr + length - 1);
//recursively sort
quicksort(arr, piv++);
quicksort(arr + piv, length - piv);
}
/******************************************************************************
* binarySearch()
*
* Arguments: input - word to be found
* arr - array of words of a certain sizes
* l - left subarray
* r - right subarray
*
* Returns: void
*
* Description: find a word
*
*****************************************************************************/
int binarySearch(char* input, char* arr[], int l, int r)
{
if (r >= l) {
int mid = l + (r - l) / 2;
//if the word is in the middle, that word
//is returned
if (strcmp(arr[mid], input) == 0)
return mid;
//if the word is smaller than the middle one, then
//it must be present in the left subarray
if (strcmp(arr[mid], input) > 0)
return binarySearch(input,arr, l, mid - 1);
//if not, it must be present in the right subarray
return binarySearch(input,arr, mid + 1, r);
}
//if the word doesn't exist
return -1;
}
/******************************************************************************
* free_array()
*
* Arguments: arr - array of words of a certain sizes
* n - size of arr
*
* Returns: void
*
* Description: free array of each size
*
*****************************************************************************/
void free_array(char* arr[],int n)
{
int i;
for (i = 0; i < n; i++)
{
free(arr[i]);
}
free(arr);
}
/******************************************************************************
* free_array()
*
* Arguments: main - array of arrays of dictionary words
* counter - size of each array
*
* Returns: void
*
* Description: free all arrays
*
*****************************************************************************/
void free_all(char** main[], int* counter)
{
int i;
for ( i = 0; i < 47; i++)
{
if (counter[i] != 0)
{
free_array(main[i], counter[i]);
}
}
}
/******************************************************************************
* Main_list()
*
* Arguments: words - number of words
* size_words - size of words
*
* Returns: arr
*
* Description: allocate a array of words of a certain size
*
*****************************************************************************/
char** Main_list(int words, int size_words)
{
char** arr = (char**)malloc(words * sizeof(char*));
int i;
for (i = 0; i < words; i++)
{
arr[i] = (char*)malloc(size_words * sizeof(char) + 1);
}
return arr;
}