Thanks that helped a lot. I think I am getting the hang of the code now but I am still having trouble getting the program to work. Whenever I run the program it just sits there. I tried putting in some printf statements to see where my program would mess up at but it doesn't wanna execute any of those lines of code. I think the program is getting hung up on oaInsert, any advice and I am trying to use the name of the student as the key??
CODE
#include <stdio.h>
#include <stdlib.h>
static int N, M;
struct studentType
{
char name[20];
int age;
int id;
float gpa;
};
typedef struct studentType student;
student theStudent;
student nullStudent;
student *oaTable;
student *currentPos;
int hash(char *v, int W);
void hashTable(int max);
int count ();
void insert(student addStudent);
student search(student searchStudent);
/*
*******************Main Function*************************************************
*/
int main(int argc, char *argv[])
{
int count, datasize;
FILE* fdata=NULL;
FILE* fsearch=NULL;
student studentArray[200];
student studentNameArray[200];
printf("Hello World");
if(argc != 3)
{
printf ("Invalid number of arguments");
printf("\n");
exit(0);
}
printf("Hello World4");
hashTable(200);
/*read file 1*/
count = 0;
fdata = fopen(argv[1], "r");
if (fdata == NULL)
{
fprintf(stderr, "Can't open input file input!\n");
printf("Error: file %s doesn't exist.\n", argv[2]);
exit(1);
}
printf("Hello World3");
while (fscanf(fdata, "%s %d %d %f", theStudent.name, &theStudent.age,&theStudent.id,&theStudent.gpa) != EOF)
{
studentArray[count]=theStudent;
printf("Hello World1");
insert(theStudent);
printf("Hello World2");
count++;
datasize = count;
}
/*
printf("%d", datasize);
printf("\n");
*/
printf("%s", studentArray[125].name);
printf("\n");
/* Opens search file.*/
fsearch=fopen(argv[2], "r");
if(!fsearch)
{
fprintf(stderr, "Can't open input file input!\n");
printf("Error: file %s doesn't exist.\n", argv[2]);
exit(1);
}
count = 0;
while (fscanf(fsearch, "%s", theStudent.name) != EOF)
{
studentNameArray[count]=theStudent;
count++;
datasize = count;
}
/* Closes both files. */
fclose(fdata);
fclose(fsearch);
return 0;
}
/*
My hash function for strings
*/
int hash(char *v, int W)
{
int h, a = 31415, b = 27183;
for( h= 0; *v != '\0'; v++, a = a*b % (W - 1))
h = (a*b + *v) % W;
return h;
}
/*
This function builds by hash table.
*/
void hashTable(int max)
{
int i;
N = 0;
M = 2*max;
oaTable = malloc(M*sizeof(student));
currentPos = oaTable;
for(i = 0; i < M; i++)
{
*currentPos = nullStudent;
currentPos++;
}
currentPos = oaTable;
}
/*
Keeps count of the number of entries I have in my table
*/
int count ()
{
return N;
}
/*
Inserts a student struct into hash table.
*/
void insert(student addStudent)
{
char *v = addStudent.name;
int i = hash(v,M);
printf("Hello World");
while(oaTable[i].name != nullStudent.name)
i = (i + 1) % M;
oaTable[i] = addStudent;
N++;
}
/*
This funciton searches for the entry.
*/
student search(student searchStudent)
{
char *v = searchStudent.name;
int i = hash(v, M);
while(oaTable[i].name != nullStudent.name)
{
if(searchStudent.name == oaTable[i].name)
return oaTable[i];
else
i = (i + 1 ) % M;
}
return nullStudent;
}