Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i keep getting a seg fault. Assume all functions being called are working #include cs2123p5.h #include #include #include #include //#define_CRT_SECURE_NO_WARNINGS 1 #include prints.c #include insertion.c

i keep getting a seg fault. Assume all functions being called are working

#include "cs2123p5.h" #include #include #include #include //#define_CRT_SECURE_NO_WARNINGS 1 #include "prints.c" #include "insertion.c" FILE *pFile;

int main(int argc, char *argv[]) { Graph graph = malloc(sizeof(GraphImp)); getCourses(graph);

}//end main

Graph newGraph()//doesnt work { Graph graph = malloc(sizeof(GraphImp)); if(graph = NULL) ErrExit(ERR_ALGORITHM, "No memory available"); graph->iNumVertices = -1; return graph; }

getCourses(Graph graph) { FILE *inputfile; inputfile = fopen("p5Input.txt", "r"); char szInputBuffer[MAX_LINE_SIZE + 1]; char szRecordType[11]; int iScanfCnt; char *pszRemainingTxt; char szCourseId[10]; char newCourseName[15]; char szPreviousCourse[15]; int iPrereqVertex = -1; int iCourseVertex = -1; int iVertex = 0; while(fgets(szInputBuffer, MAX_LINE_SIZE, inputfile) != NULL) { if(szInputBuffer[0] == ' ') continue; printf("%s", szInputBuffer); pszRemainingTxt = getToken(szInputBuffer , szRecordType, sizeof(szRecordType)-1); if(strcmp(szRecordType, "COURSE") == 0) { sscanf(pszRemainingTxt, "%7s %[^ ]", szCourseId, newCourseName); insert(graph, szCourseId, newCourseName); } if(strcmp(szRecordType, "PREREQ") == 0) { sscanf(pszRemainingTxt, "%s", szCourseId); iPrereqVertex = findCourse(graph, szCourseId); // printf("prereqvertex# %d ", iPrereqVertex); iCourseVertex = findCourse(graph, szPreviousCourse); insertPrereq(graph, iPrereqVertex, iCourseVertex); // printf("end prereq "); } if(strcmp(szRecordType, "PRTALL") == 0) { printAllInList(graph); } if(strcmp(szRecordType, "PRTSUCC") == 0) { sscanf(pszRemainingTxt, "%s", szCourseId); //iCourseVertex = findCourse(graph, szCourseId); printOne(graph, iVertex); }

}

}

void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo) { switch (iArg) { case USAGE_ERR: fprintf(stderr, "Error: %s %s " , pszMessage , pszDiagnosticInfo); break; case USAGE_ONLY: break; default: fprintf(stderr, "Error: bad argument #%d. %s %s " , iArg , pszMessage , pszDiagnosticInfo); }//end switch/case

//print the usage information for any type of command line error fprintf(stderr, "p2 -c fileName "); if (iArg == USAGE_ONLY) exit(USAGE_ONLY); else exit(ERR_COMMAND_LINE); }//end exitUsage

char * getToken(char *pszInputTxt, char szToken[], int iTokenSize) { int iDelimPos; //found position of delim int iCopy; //number of characters to copy char szDelims[20] = " "; //delimiters szToken[0] = '\0';

//check for NULL pointer if (pszInputTxt == NULL) ErrExit(ERR_ALGORITHM , "getToken passed a NULL pointer");

//check for no token if at zero byte if (*pszInputTxt == '\0') return NULL;

//get the position of the first delim iDelimPos = strcspn(pszInputTxt, szDelims);

//if the delim position is a the fist character, return no token if (iDelimPos == 0) return NULL;

//see if we have more charcters that target token, if so, trunc if (iDelimPos > iTokenSize) iCopy = iTokenSize; //truncated size else iCopy = iDelimPos;

//copy the token into the target token variable memcpy(szToken, pszInputTxt, iCopy); szToken[iCopy] = '\0'; //null terminate

//advance the position pszInputTxt += iDelimPos; if (*pszInputTxt == '\0') return pszInputTxt; else return pszInputTxt + 1;

}//end getToken

void ErrExit(int iexitRC, char szFmt[], ...) { va_list args; //this is the standard Cvariable argument list type va_start(args, szFmt); //this tells the compiler where the variable agrument //begins. They begin after szFmt. printf("ERROR: "); vprintf(szFmt, args); //vprintf recieves a printf format string and a //va_list argument va_end(args); //let the C environment know we are finished with th //va_list argument printf(" \tEncountered in file %s ", __FILE__); //this 2nd arg is filled in by //the pre-compiler exit(iexitRC); }//end ErrExit

void processCommandSwitches(int argc, char *argv[], char **ppszCourseFileName) { int i; // Examine each of the command arguments other than the name of the program. for (i = 1; i < argc; i++) { // check for a switch if (argv[i][0] != '-') exitUsage(i, ERR_EXPECTED_SWITCH, argv[i]); // determine which switch it is switch (argv[i][1]) { case 'c': // Course File Name if (++i >= argc) exitUsage(i, ERR_MISSING_ARGUMENT, argv[i - 1]); // check for too long of a file anme else *ppszCourseFileName = argv[i]; break; case 'q': // Query File Name if (++i >= argc) exitUsage(i, ERR_MISSING_ARGUMENT, argv[i - 1]); break; case '?': exitUsage(USAGE_ONLY, "", ""); break; default: exitUsage(i, ERR_EXPECTED_SWITCH, argv[i]); } } }

/********************************************************************** cs2123p5.h Purpose: Defines constants: max constants error constants warning constants boolean constants Defines typedef for EdgeNode - graph edge containing preq and successor vertex Vertex - contains course information (course number, name), existence boolean, successor list first node pointer, predecessor list first node pointer GraphImp - array of vertices and a count of them Graph - pointer to an allocated GraphImp PlanImp - tbd Defines function prototypes for functions used in pgm5 (recursive and non-recursive) Defines function prototypes for functions used in pgm6 Defines WARNING macro Notes: **********************************************************************/ /*** constants ***/ #define MAX_TOKEN 50 // Maximum number of actual characters for a token #define MAX_LINE_SIZE 100 // Maximum number of character per input line #define MAX_VERTICES 60 #define OVERFLOW_BEGIN 29 // begin of overflow area // Error constants (program exit values) #define ERR_COMMAND_LINE 900 // invalid command line argument #define ERR_ALGORITHM 903 // Unexpected error in algorithm #define ERR_TOO_MANY_COURSE 1 // Too many courses #define ERR_BAD_COURSE 3 // Bad Course Data #define ERR_BAD_PREREQ 4 // Bad Prereq Data #define ERR_MISSING_SWITCH "missing switch" #define ERR_EXPECTED_SWITCH "expected switch, found" #define ERR_MISSING_ARGUMENT "missing argument for" // exitUsage control #define USAGE_ONLY 0 // user only requested usage information #define USAGE_ERR -1 // usage error, show message and usage information // boolean constants #define FALSE 0 #define TRUE 1 // EdgeNode represents one edge in a graph typedef struct EdgeNode { int iPrereqVertex; // prereq int iSuccVertex; // successor struct EdgeNode *pNextEdge; // points to next edge } EdgeNode; typedef struct Vertex { char szCourseId[8]; // Course Identifier char szCourseName[21]; // Course Full Name char szDept[4]; // Department (e.g., CS, MAT) int bExists; // pgm6 DELETE command causes this to be set to TRUE // TRUE - this vertex exists, FALSE - deleted EdgeNode * prereqList; EdgeNode * successorList; int iSemesterLevel; // Which semester should this be taken int iHashChainNext; // pgm 6 extra credit int iDistSource; // Distance from a source } Vertex; // GraphImp of a double adjacency list graph typedef struct { int iNumVertices; // Number of vertices in the vertexM array Vertex vertexM[MAX_VERTICES]; // Array of vertices int iOverflowBegin; // The subscript of the first overflow entry // in the array of vertices. // Any subscript less than this value is in // the primary area. int iFreeHead; // Subscript of a free list of entries // in the overflow portion of the // graph's vertexM array } GraphImp; typedef GraphImp *Graph; // Degree Plan typedef struct { int semesterM[5][MAX_VERTICES]; // Array which has five rows for each semester. int bIncludeM[MAX_VERTICES]; // TRUE if this course is to be included in the plan } PlanImp; typedef PlanImp * Plan; // Prototypes // Recursive functions for program 5 int maxChain(Graph graph, int iVertex); void printTraversal(Graph graph, int iCourseVertex, int indent); void printLongChains(Graph graph, int iVertex, int pathM[], int iLevel, int iLongLength); int causesCycle(Graph graph, int iPrereqVertex, int iVertex); // Non-recursive for program 5 int findCourse(Graph graph, char szCourseId[]); void insertPrereq(Graph graph, int iPrereqVertex, int iCourseVertex); void printAllInList(Graph graph); void printOne(Graph graph, int iVertex); void printSources(Graph graph); void printSinks(Graph graph); Graph newGraph(); int insertCourse(Graph graph, char szCourseId[]); // Program 6 function for delete void deleteCourse (Graph graph, int iVertex); // Program 6 functions for Plan void doPlan(Graph graph, Plan plan); void setLevel(Graph g, Plan plan, int iVertex, int iLev); Plan newPlan(); // hash function for extra credit int hash(Graph g, char SzCourseId[]); void printHash(Graph g); void printChain(Graph g, int iVertex); // functions in most programs, but require modifications void processCommandSwitches(int argc, char *argv[], char **ppszCommandFileName); void exitUsage(int iArg, char *pszMessage, char *pszDiagnosticInfo); // Utility routines provided by Larry void ErrExit(int iexitRC, char szFmt[], ...); char * getToken(char *pszInputTxt, char szToken[], int iTokenSize); /* WARNING macro Parameters: I szFmt - a printf format I ... - a variable number of parameters corresponding to szFmt's format codes. Results: Prints "WARNING" and the value(s) specified by the szFmt. Notes: Since this generates multiple C statements, we surround them with a dummy do while(0) which only executes once. Notice that the dummy do while isn't ended with a ";" since the user of the macro naturally specifies a ";". Example: if (x != 0) WARNING("X must be blah blah"); else { // normal processing .... } If we used {} in the macro definition instead of the dummy do while(0), the generated code would have a bad ";": if (x != 0) { printf("\tWARNING: "); printf("X must be blah blah"); printf(" "); } ; // yuck, bad ";" causing the compiler to not understand the else else { // normal processing .... } */ #define WARNING(szFmt, ...) do { \ printf("\tWARNING: "); \ printf(szFmt, __VA_ARGS__); \ printf(" "); \ } while (0) 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions

Question

=+What is your birthday? (often on application forms) Disability

Answered: 1 week ago

Question

Why do HCMSs exist? Do they change over time?

Answered: 1 week ago