Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Change my code, so that it doesn t bring any error when I compile it with g + + compiler. Also, I want my code

Change my code, so that it doesnt bring any error when I compile it with g++ compiler. Also, I want my code to be in a C programming language format as much as possible. Here is my code about link state:
#include
#include
#include
#define MAX_NODES 100
#define INFINITY 999999
int num_nodes;
int graph[MAX_NODES][MAX_NODES];
int dist[MAX_NODES];
int prev[MAX_NODES];
int visited[MAX_NODES];
void read_topology(const char *filename){
FILE *file = fopen(filename,"r");
if (!file){
printf("Error: open input file.
");
exit(1);
}
fscanf(file,"%d", &num_nodes);
for (int i =0; i < num_nodes; i++){
for (int j =0; j < num_nodes; j++){
if (i == j){
graph[i][j]=0;
} else {
graph[i][j]= INFINITY;
}
}
}
int node1, node2, cost;
while (fscanf(file,"%d %d %d", &node1, &node2, &cost)!= EOF){
graph[node1][node2]= cost;
graph[node2][node1]= cost;
}
fclose(file);
}
void dijkstra(int src){
for (int i =0; i < num_nodes; i++){
dist[i]= INFINITY;
prev[i]=-1;
visited[i]=0;
}
dist[src]=0;
for (int i =0; i < num_nodes; i++){
int min_dist = INFINITY;
int u =-1;
for (int j =0; j < num_nodes; j++){
if (!visited[j] && dist[j]< min_dist){
min_dist = dist[j];
u = j;
}
}
if (u ==-1) break;
visited[u]=1;
for (int v =0; v < num_nodes; v++){
if (!visited[v] && graph[u][v]!= INFINITY){
int new_dist = dist[u]+ graph[u][v];
if (new_dist < dist[v]){
dist[v]= new_dist;
prev[v]= u;
}
}
}
}
}
void print_routing_table(FILE *output, int src){
for (int i =0; i < num_nodes; i++){
fprintf(output,"%d %d %d
", i, prev[i], dist[i]);
}
fprintf(output,"
");
}
void read_messages(const char *filename, FILE *output){
FILE *file = fopen(filename,"r");
if (!file){
printf("Error: open input file.
");
exit(1);
}
int src, dst;
char message[1000];
while (fscanf(file,"%d %d", &src, &dst)!= EOF){
fgets(message, sizeof(message), file);
message[strlen(message)-1]='\0'; // Remove newline character
dijkstra(src);
if (dist[dst]== INFINITY){
fprintf(output, "from %d to %d cost infinite hops unreachable message %s
", src, dst, message);
} else {
fprintf(output, "from %d to %d cost %d hops", src, dst, dist[dst]);
int hop = dst;
while (prev[hop]!=-1){
fprintf(output,"%d", hop);
hop = prev[hop];
}
fprintf(output," message %s
", message);
}
}
fclose(file);
}
void apply_changes(const char *filename){
FILE *file = fopen(filename,"r");
if (!file){
printf("Error: open input file.
");
exit(1);
}
int node1, node2, cost;
while (fscanf(file,"%d %d %d", &node1, &node2, &cost)!= EOF){
if (cost ==-999){
graph[node1][node2]= INFINITY;
graph[node2][node1]= INFINITY;
} else {
graph[node1][node2]= cost;
graph[node2][node1]= cost;
}
}
fclose(file);
}
int main(int argc, char *argv[]){
if (argc !=4){
printf("usage: linkstate topologyfile messagesfile changesfile
");
return 1;
}
read_topology(argv[1]);
FILE *output = fopen("output_ls.txt","w");
if (!output){
printf("Error: open output file.
");
return 1;
}
for (int i =0; i < num_nodes; i++){
dijkstra(i);
print_routing_table(output, i);
}
read_messages(argv[2], output);
apply_changes(argv[3]);
for (int i =0; i < num_nodes; i++){
dijkstra(i);
print_routing_table(output, i);
}
read_messages(argv[2], output);
fclose(output);
printf("Complete. Output file written to output_ls.txt.
");
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions