Hey guys, I am trying to create a program that reads my file filled with random words, it then compares the words after they are put into a 2d array and sees if there is any matching words.. unfortunately the count is not working for me (in function2 and function3) and I am not sure why.. could anyone please help?
Code:
#include<stdio.h>
#include<string.h>
char function1(char words_array[][17]);
int function2(char words_array[][17]);
void function3(int pairs, char words_array[][17]);
int main( void )
{
char words_array[20][17];
int x = 0;
int y = 0;
int pairs = 0;
system("clear");
function1(words_array);
function2(words_array);
function3(pairs, words_array);
return ( 0 ) ;
}
char function1(char words_array[][17])
{
FILE *words;
int x = 0;
int y = 0;
words = fopen("words.dat","r");
if(words==NULL)
printf("\n\nwords.dat was not properly opened.\n");
else
{
for(x = 0 ; x < 20 ; x++ )
{
fscanf(words,"%s",&words_array[x][y]);
}
fclose(words);
}
}
int function2(char words_array[][17])
{
int x = 0;
int y = 0;
int pairs = 0;
for(x=0; x<20; x++)
{
for(y=x+1; y<20; y++)
{
if (strcmp (words_array[x], words_array[y])==0)
{
pairs++;
}
}
}
return(pairs);
}
void function3(int pairs, char words_array[][17])
{
int x = 0;
//int y = 0;
for(x = 0 ; x < 20 ; x++ )
{
printf("Word #%d is %s \n", x + 1, words_array[x]);
}
printf("%d \n",pairs);
}