Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please correct my code where is it wrong: #include #include #include #include #include #define MAX _ NODES 1 0 0 #define NAME _ MAX 1

Please correct my code where is it wrong:
#include
#include
#include
#include
#include
#define MAX_NODES 100
#define NAME_MAX 100
typedef struct {
char Node1[NAME_MAX];
char Node2[NAME_MAX];
int load;
} Links;
typedef struct {
Links links[MAX_NODES];
int num_links;
} Diagram;
void InsertNewDiagram(Diagram *diagram);
void saveNew(Diagram *diagram);
void SelectDiagram();
void ShortPath(Diagram *diagram);
int main(){
int options;
Diagram diagram;
diagram.num_links =0;
do {
printf("
Menu Options:
");
printf("Option 1: Insert a new graph.
");
printf("Option 2: Choose a graph from the CSV files to activate.
");
printf("Option 3: Find the shortest path of the active graph.
");
printf("Option 4: Exit the program.
");
printf("
Provide the option you want to choose: ");
scanf("%d", &options);
switch (options){
case 1:
InsertNewDiagram(&diagram);
break;
case 2:
SelectDiagram();
break;
case 3:
if (diagram.num_links ==0){
printf("No Graph selected. Firstly select a Graph.
");
} else {
ShortPath(&diagram);
}
break;
case 4:
printf("Leave the program.
");
break;
default:
printf("Incorrect option chosen. Retry.
");
}
} while (options !=4);
return 0;
}
//Option 1
void InsertNewDiagram(Diagram *diagram){
char Node1[NAME_MAX], Node2[NAME_MAX];
int load, num_links;
printf("Provide the amount of Edges the graph has: ");
scanf("%d", &num_links);
diagram->num_links = num_links;
printf("Provide the vertices in capital letters and weight:
");
printf("(Vertex1,Vertex2,Weight)
");
for (int i =0; i < num_links; i++){
scanf("%s,%s,%d", Node1, Node2, &load);
strcpy(diagram->links[i].Node1, Node1);
strcpy(diagram->links[i].Node2, Node2);
diagram->links[i].load = load;
}
saveNew(diagram);
}
void saveNew(Diagram *diagram){
char Name[NAME_MAX];
FILE *fp;
printf("Provide the name you want to save the graph to: ");
scanf("%s", Name);
fp = fopen(Name,"w");
if (fp == NULL){
printf("The file cannot be opened
");
return;
}
for (int i =0; i < diagram->num_links; i++){
fprintf(fp,"%s,%s,%d
", diagram->links[i].Node1, diagram->links[i].Node2, diagram->links[i].load);
}
fclose(fp);
printf("The Graph is saved under %s
", Name);
}
//Option 2
void SelectDiagram(){
DIR *dir;
struct dirent *entry;
char rentName[NAME_MAX];
printf("Provide the path to the location of the CSV files: ");
scanf("%s", rentName);
if ((dir = opendir(rentName))!= NULL){
int compute =0;
char files[100][NAME_MAX];
while ((entry = readdir(dir))!= NULL){
if (strstr(entry->d_name, ".csv")!= NULL){
strcpy(files[compute], entry->d_name);
compute++;
}
}
closedir(dir);
if (compute ==0){
printf("There are no CSV files available to use.
");
return;
}
printf("The available CSV files:
");
for (int i =0; i < compute; i++){
printf("%d.%s
", i +1, files[i]);
}
int choose;
printf("Provide the number of the file to be added: ");
scanf("%d", &choose);
if (choose <1|| choose > compute){
printf("Incorrect option chosen.
");
return;
}
printf("The option you chose is: %s.
", files[choose -1]);
} else {
perror("Error in openening directory.");
}
}
//finding index of vertices
int discoverIND(char *apex, Diagram *diagram){
for (int j =0; j < diagram->num_links; j++){
if (strcmp(diagram->links[j].Node1, apex)==0|| strcmp(diagram->links[j].Node2, apex)==0){
return j;
}
}
return -1;
}
//Dijkstra
void Dijkstra(Diagram *diagram, char *begin, char *fin){
int c = diagram->num_links;
int span[MAX_NODES];
int before[MAX_NODES];
int marked[MAX_NODES];
for (int k =0; k < c; k++){
span[k]= INT_MAX;
before[k]=-1;
marked[k]=0; // Initialize marked array with zeroes
}
int beginIND = discoverIND(begin, diagram);
int finIND = discoverIND(fin, diagram);
if (beginIND ==-1|| finIND ==-1){
printf("Unable to find the startpoint or endpoint of the vertex given in Graph.
");
return;
}
span[beginIND]=0;
//The algorithm itself
for (int sum =0; sum < c -1; sum++){
int shortestSpan = INT_MAX;
int shortestIND =-1;

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

More Books

Students also viewed these Databases questions