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 distance vector:
#include
#include
#include
#define MAX_NODES 100
#define INFINITY 999999
typedef struct {
int cost[MAX_NODES];
int next_hop[MAX_NODES];
} RoutingTable;
int num_nodes;
int graph[MAX_NODES][MAX_NODES];
RoutingTable rt[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 initialize_routing_table(){
for (int i =0; i < num_nodes; i++){
for (int j =0; j < num_nodes; j++){
if (i == j){
rt[i].cost[j]=0;
rt[i].next_hop[j]= i;
} else if (graph[i][j]!= INFINITY){
rt[i].cost[j]= graph[i][j];
rt[i].next_hop[j]= j;
} else {
rt[i].cost[j]= INFINITY;
rt[i].next_hop[j]=-1;
}
}
}
}
void update_routing_table(){
int updated;
do {
updated =0;
for (int i =0; i < num_nodes; i++){
for (int j =0; j < num_nodes; j++){
for (int k =0; k < num_nodes; k++){
if (rt[i].cost[j]> rt[i].cost[k]+ rt[k].cost[j]){
rt[i].cost[j]= rt[i].cost[k]+ rt[k].cost[j];
rt[i].next_hop[j]= rt[i].next_hop[k];
updated =1;
}
}
}
}
} while (updated);
}
void print_routing_table(FILE *output, int src){
for (int i =0; i < num_nodes; i++){
fprintf(output,"%d %d %d
", i, rt[src].next_hop[i], rt[src].cost[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
if (rt[src].cost[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, rt[src].cost[dst]);
int hop = src;
while (hop != dst){
fprintf(output,"%d", hop);
hop = rt[hop].next_hop[dst];
}
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: distvec topologyfile messagesfile changesfile
");
return 1;
}
read_topology(argv[1]);
FILE *output = fopen("output_dv.txt","w");
if (!output){
printf("Error: open output file.
");
return 1;
}
initialize_routing_table();
update_routing_table();
for (int i =0; i < num_nodes; i++){
print_routing_table(output, i);
}
read_messages(argv[2], output);
apply_changes(argv[3]);
initialize_routing_table();
update_routing_table();
for (int i =0; i < num_nodes; i++){
print_routing_table(output, i);
}
read_messages(argv[2], output);
fclose(output);
printf("Complete. Output file written to output_dv.txt.
");
return 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_2

Step: 3

blur-text-image_step3

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