Hello I am having a segmentation fault error when trying to move my code over to mobaxterm, I have spent the last 4 to 5 days trying to talk with tutors but nothing has been helping and some think its even an error on mobaxterm after sending them my code. Below will be My code it consists of The main file (the ABC123) the hashtable header given to use by the professor himself, a Hashtable.c file that created by me as well as two data files used on the program and a make file used to compile the file within mobaxterm.
The Error in question is the following segmentation fault error "[Error given in xterm][1]", it consists of Team not found followed by the error itself (this is the gdb output). this makes me believe that this has something to do with read access but when I try and print information from key parts of the code the information comes forth from the tokens and elements used. At this point I don't know what to do so here I am as even if my professors see this i think it will be fine since I really have no Idea what to do and the tutors seem unable to help me. Bellow is the code and I hope someone can help me.
(main.c)
#define _CRT_SECURE_NO_WARNINGS
#include "HashTable.h"
#include<stdbool.h>
#include <stdio.h>
#include <string.h>
#define MAXCHAR 1000
void print(HashTable h,char string[50][50]){
size_t j;
int size=30;
for( j=0; j < size; j++)
{
Element* e=get(h,string[j]);
printf("%s :%d Wins - %d Losses\n",e->teamName,e->wins,e->losses);
}
}
int main(){
FILE* stream = fopen("nbaTeamNames.csv", "r");
if (stream == NULL) {
perror("Error opening file: ");
return 1;
}
HashTable h=newHashTable(100);
int i=0;
char myString[50][50];
char row1[MAXCHAR];
char *token1;
while (feof(stream) != true)
{
fgets(row1, MAXCHAR, stream);
token1 = strtok(row1, ",\n");
int col=1;
Element e;
while(token1 != NULL)
{
if(col==1){
strcpy(e.teamName,token1);
strcpy(myString[i],token1);
e.wins=0;
e.losses=0;
put(h,e);
i++;
}
token1 = strtok(NULL, ",\n");
col++;
}
}
char row[MAXCHAR];
char *token;
FILE *fp = fopen("nbaData2019.csv","r");
while (feof(fp) != true)
{
fgets(row, MAXCHAR, fp);
token = strtok(row, ",");
int col=1;
int team1Score;
int team2Score;
Element* e=NULL;
Element* e2=NULL;
while(token != NULL)
{
if(col==1){
e=get(h,token);
}
if(col==2){
team1Score=atoi(token);
}
if(col==3){
e2=get(h,token);
}
if(col==4){
team2Score=atoi(token);
}
token = strtok(NULL, ",\n");
col++;
}
if(e==NULL || e2==NULL){
printf("Team not found\n");
}else{
if(team1Score>team2Score){
e->wins++;
e2->losses++;
}
else{
e -> losses++;
e2 -> wins++;
}
}
}
print(h,myString);
return 0;
}
HashTable.c
#define _CRT_SECURE_NO_WARNINGS
#include "HashTable.h"
#include <stdio.h>
#include <string.h>
#include <math.h>
HashTableImp *newHashTable(int size){
HashTableImp *x=(HashTableImp*)malloc (sizeof(HashTableImp));
x->n=size;
x->hashTable=(HashTableEntry*)malloc (size*sizeof(HashTableEntry));
int i;
for(i=0; i<size; i++){
x->hashTable[i].chainIndex=-2;
}
return x;
}
void freeHashTable(HashTable h){
free(h->hashTable);
free(h);
h=NULL;
}
int stringToInt(char *stringToConvert){
int x;
sscanf(stringToConvert,"%d",&x);
return x;
}
int divisionMethod(int key,int n){
return key%n;
}
int CountTotalDigits(int a){
int count=0;
while (a != 0) {
a /= 10; // n = n/10
++count;
}
return count;
}
int po(int a,int b){
while (b>0){
a=a*10;
b--;
}
return a;
}
int midsquareMethod(int key,int n){
int temp=key*key;
int x=CountTotalDigits(temp);
int y=CountTotalDigits(n);
if(x<y){
return x;
}
int p,p1;
p=po(10,(x-ceil((x-y)/2)));
p1=po(10,(x-y)/2);
temp=temp%p;
temp=temp/p1;
return temp;
}
int count(HashTable h){
int c=0;
int i=0;
while(h->hashTable[i].chainIndex!=-2){
i+=1;
c++;
}
return c;
}
void put(HashTable h,Element e){
int key=stringToInt(e.teamName);
int idx=divisionMethod(key,h->n);
int i=0;
if (h->n == (count(h)+1)) {
// Hash Table Full
printf("Insert Error: Hash Table is full\n");
return;
}
if (strcmp(h->hashTable[idx].key.teamName, e.teamName) == 0) {
printf("Insert Error: Key already exist\n");
return;
}
while(h->hashTable[idx].chainIndex!=-2){
idx+=1;
}
h->hashTable[idx].key=e;
h->hashTable[idx].chainIndex=-1;
}
Element* get(HashTable h,char *teamName){
int key=stringToInt(teamName);
int idx=divisionMethod(key,h->n);
while(h->hashTable[idx].chainIndex!=-2){
if (strcmp(h->hashTable[idx].key.teamName, teamName) == 0){
printf("");
return &h->hashTable[idx].key;
}
idx+=1;
}
return NULL;
}
HashTable.h (provided By the professor)
/****************************
HashTable.h
Purpose:
Struct definition of a Hash Table.
Define the function protoypes used by Hash Tables.
*************************/
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
//Defining the maximum team name length.
#define MAX_NAME_LENGTH 50
//TRUE and FALSE constants
#define TRUE 1
#define FALSE 0
//Defining an Element to be the data for a team.
typedef struct
{
char teamName[MAX_NAME_LENGTH];
int wins;
int losses;
} Element;
//Defining a HashTableEntry to contain an Element and an integer to manage chaining.
typedef struct
{
Element key;
int chainIndex;
} HashTableEntry;
//Defining the a struct for a Hash Table Implementation.
typedef struct
{
int n;
//The size of the array to store all the data.
HashTableEntry *hashTable;
//Pointer that we will use ty dynamically allocate an array for our Hash Table.
} HashTableImp;
//Defining a HashTable to be a HashTableImp Pointer.
typedef HashTableImp *HashTable;
/*****FUNCTION PROTOTYPES**********/
//Malloc a new HashTableImp, malloc the hashTable to be an array of HashTableEntrys of size n,
//initialize each iChainIndex to be -2 (indicating that the spot is empty),
//and return a pointer to the HashTableImp.
HashTable newHashTable(int n);
//Free the HashTable h.
void freeHashTable(HashTable h);
//Given a string, convert it into an integer to be used in either
//the division method or the midsquare method.
int stringToInt(char *stringToConvert);
//Given a key value, use the division method to find a
//valid index for hashTable.
int divisionMethod(int key, int n);
//Given a key value, use the midsquare method to find a
//valid index for hashTable.
int midsquareMethod(int key, int n);
//Insert e into our HashTable h by using stringToInt to convert the team name
//into an integer and then passing that integer to one of the division or
//midsquare method functions. If this entry is not occupied, insert it there
//and change the corresponding iChainIndex to be -1. If there is a collision,
//use open chaining to find an open location for e, and update the iChainIndex
//values accordingly.
void put(HashTable h, Element e);
//Return a pointer to the key stuct containing the data that matches teamName
//if it exists. If it does not exist, return NULL.
Element* get(HashTable h, char *teamName);
Makefile (the make file used to run the code in the gcc)
# Define the machine object files for your program
OBJECTS = Xwj819Project5.o HashTable.o
# Define your include file
INCLUDES = HashTable.h
# make for the executable
p5: ${OBJECTS}
gcc -g -o p5 ${OBJECTS} -lm
# Simple suffix rules for the .o
%.o: %.c ${INCLUDES}
gcc -g -c -O3 $< -lm
# Clean the .o files
clean:
rm -f ${OBJECTS}
nbaTeamNames.csv
Atlanta Hawks
Boston Celtics
Brooklyn Nets
Charlotte Hornets
Chicago Bulls
Cleveland Cavaliers
Dallas Mavericks
Denver Nuggets
Detroit Pistons
Golden State Warriors
Houston Rockets
Indiana Pacers
Los Angeles Clippers
Los Angeles Lakers
Memphis Grizzlies
Miami Heat
Milwaukee Bucks
Minnesota Timberwolves
New Orleans Pelicans
New York Knicks
Oklahoma City Thunder
Orlando Magic
Philadelphia 76ers
Phoenix Suns
Portland Trail Blazers
Sacramento Kings
San Antonio Spurs
Toronto Raptors
Utah Jazz
Washington Wizards
nbaData2019.cvs (these are only the first 50 lines because the file is 1230 lines long if the full file is needed please tell me)
Philadelphia 76ers,87,Boston Celtics,105
Oklahoma City Thunder,100,Golden State Warriors,108
Milwaukee Bucks,113,Charlotte Hornets,112
Brooklyn Nets,100,Detroit Pistons,103
Memphis Grizzlies,83,Indiana Pacers,111
Miami Heat,101,Orlando Magic,104
Atlanta Hawks,107,New York Knicks,126
Cleveland Cavaliers,104,Toronto Raptors,116
New Orleans Pelicans,131,Houston Rockets,112
Minnesota Timberwolves,108,San Antonio Spurs,112
Utah Jazz,123,Sacramento Kings,117
Denver Nuggets,107,Los Angeles Clippers,98
Dallas Mavericks,100,Phoenix Suns,121
Chicago Bulls,108,Philadelphia 76ers,127
Miami Heat,113,Washington Wizards,112
Los Angeles Lakers,119,Portland Trail Blazers,128
Charlotte Hornets,120,Orlando Magic,88
New York Knicks,105,Brooklyn Nets,107
Atlanta Hawks,117,Memphis Grizzlies,131
Cleveland Cavaliers,123,Minnesota Timberwolves,131
Sacramento Kings,129,New Orleans Pelicans,149
Boston Celtics,101,Toronto Raptors,113
Indiana Pacers,101,Milwaukee Bucks,118
Oklahoma City Thunder,92,Los Angeles Clippers,108
Golden State Warriors,124,Utah Jazz,123
Brooklyn Nets,112,Indiana Pacers,132
Toronto Raptors,117,Washington Wizards,113
Boston Celtics,103,New York Knicks,101
Orlando Magic,115,Philadelphia 76ers,116
Detroit Pistons,118,Chicago Bulls,116
Charlotte Hornets,113,Miami Heat,112
Minnesota Timberwolves,136,Dallas Mavericks,140
Phoenix Suns,91,Denver Nuggets,119
San Antonio Spurs,108,Portland Trail Blazers,121
Houston Rockets,124,Los Angeles Lakers,115
Atlanta Hawks,133,Cleveland Cavaliers,111
Sacramento Kings,131,Oklahoma City Thunder,120
Golden State Warriors,98,Denver Nuggets,100
Houston Rockets,112,Los Angeles Clippers,115
Orlando Magic,93,Boston Celtics,90
Charlotte Hornets,106,Toronto Raptors,127
New York Knicks,113,Milwaukee Bucks,124
Indiana Pacers,91,Minnesota Timberwolves,101
Chicago Bulls,109,Dallas Mavericks,115
Memphis Grizzlies,92,Utah Jazz,84
Washington Wizards,125,Portland Trail Blazers,124
Phoenix Suns,103,Golden State Warriors,123
San Antonio Spurs,143,Los Angeles Lakers,142
Philadelphia 76ers,132,Detroit Pistons,133
Los Angeles Clippers,109,New Orleans Pelicans,116
Again any help with this would be greatly appreciated I usually try and find and work these problems out in person or by myself but my project is due today and im at a loss as where to go from here. I thank you for any help you can give me. [1]: https://i.stack.imgur.com/qU9kB.png