Function for matrix allocation

This commit is contained in:
nanometer5088
2023-04-25 10:39:05 -03:00
parent 48c9a36af2
commit c507b13ef5
3 changed files with 12 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "dicionario.h"
@@ -54,4 +55,12 @@ void to_lowercase(char *word) {
word[i] = tolower(word[i]);
i++;
}
}
char** alocarMatriz(int linhas, int colunas) {
char** matriz = (char**) malloc(linhas * sizeof(char*));
for (int i = 0; i < linhas; i++) {
matriz[i] = (char*) malloc(colunas * sizeof(char));
}
return matriz;
}

View File

@@ -7,6 +7,7 @@ void insert_word(char **dict, int *size, char *word);
int compare_strings(const void *a, const void *b);
int binary_search(char **dict, int left, int right, char *word);
void to_lowercase(char *word);
char** alocarMatriz(int linhas, int colunas);
// Fim do cabeçalho de inclusão condicional.
#endif

8
main.c
View File

@@ -7,7 +7,6 @@
int main() {
FILE *input_file, *output_file;
char word[50];
char **dict;
int dict_size = 0;
// Abre o arquivo de entrada
@@ -18,10 +17,7 @@ int main() {
}
// Aloca memória para o dicionário de palavras
dict = (char**) malloc(100 * sizeof(char*));
for (int i = 0; i < 100; i++) {
dict[i] = (char*) malloc(50 * sizeof(char));
}
char** dict = alocarMatriz(100, 50);
// Lê as palavras do arquivo de entrada e insere no dicionário
while (fscanf(input_file, "%s", word) != EOF) {
@@ -29,12 +25,12 @@ int main() {
if (strcmp(word, ".") == 0) {
break;
}
qsort(dict, dict_size, sizeof(char*), compare_strings);
if (binary_search(dict, 0, dict_size-1, word) == -1) {
insert_word(dict, &dict_size, word);
}
}
qsort(dict, dict_size, sizeof(char*), compare_strings);
// Fecha o arquivo de entrada
fclose(input_file);