Answered step by step
Verified Expert Solution
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 MAXNODES #define INFINITY int numnodes; int graphMAXNODESMAXNODES; int distMAXNODES; int prevMAXNODES; int visitedMAXNODES; void readtopologyconst char filename FILE file fopenfilenamer; if file printfError: open input file. ; exit; fscanffiled &numnodes; for int i ; i numnodes; i for int j ; j numnodes; j if i j graphij; else graphij INFINITY; int node node cost; while fscanffiled d d &node &node &cost EOF graphnodenode cost; graphnodenode cost; fclosefile; void dijkstraint src for int i ; i numnodes; i disti INFINITY; previ; visitedi; distsrc; for int i ; i numnodes; i int mindist INFINITY; int u ; for int j ; j numnodes; j if visitedj && distj mindist mindist distj; u j; if u break; visitedu; for int v ; v numnodes; v if visitedv && graphuv INFINITY int newdist distu graphuv; if newdist distv distv newdist; prevv u; void printroutingtableFILE output int src for int i ; i numnodes; i fprintfoutputd d d i previ disti; fprintfoutput ; void readmessagesconst char filename FILE output FILE file fopenfilenamer; if file printfError: open input file. ; exit; int src dst; char message; while fscanffiled d &src &dst EOF fgetsmessage sizeofmessage file; messagestrlenmessage; Remove newline character dijkstrasrc; if distdst INFINITY fprintfoutput "from d to d cost infinite hops unreachable message s src dst message; else fprintfoutput "from d to d cost d hops", src dst distdst; int hop dst; while prevhop fprintfoutputd hop; hop prevhop; fprintfoutput message s message; fclosefile; void applychangesconst char filename FILE file fopenfilenamer; if file printfError: open input file. ; exit; int node node cost; while fscanffiled d d &node &node &cost EOF if cost graphnodenode INFINITY; graphnodenode INFINITY; else graphnodenode cost; graphnodenode cost; fclosefile; int mainint argc, char argv if argc printfusage: linkstate topologyfile messagesfile changesfile ; return ; readtopologyargv; FILE output fopenoutputlstxtw; if output printfError: open output file. ; return ; for int i ; i numnodes; i dijkstrai; printroutingtableoutput i; readmessagesargv output; applychangesargv; for int i ; i numnodes; i dijkstrai; printroutingtableoutput i; readmessagesargv output; fcloseoutput; printfComplete Output file written to outputlstxt ; return ;
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 MAXNODES
#define INFINITY
int numnodes;
int graphMAXNODESMAXNODES;
int distMAXNODES;
int prevMAXNODES;
int visitedMAXNODES;
void readtopologyconst char filename
FILE file fopenfilenamer;
if file
printfError: open input file.
;
exit;
fscanffiled &numnodes;
for int i ; i numnodes; i
for int j ; j numnodes; j
if i j
graphij;
else
graphij INFINITY;
int node node cost;
while fscanffiled d d &node &node &cost EOF
graphnodenode cost;
graphnodenode cost;
fclosefile;
void dijkstraint src
for int i ; i numnodes; i
disti INFINITY;
previ;
visitedi;
distsrc;
for int i ; i numnodes; i
int mindist INFINITY;
int u ;
for int j ; j numnodes; j
if visitedj && distj mindist
mindist distj;
u j;
if u break;
visitedu;
for int v ; v numnodes; v
if visitedv && graphuv INFINITY
int newdist distu graphuv;
if newdist distv
distv newdist;
prevv u;
void printroutingtableFILE output int src
for int i ; i numnodes; i
fprintfoutputd d d
i previ disti;
fprintfoutput
;
void readmessagesconst char filename FILE output
FILE file fopenfilenamer;
if file
printfError: open input file.
;
exit;
int src dst;
char message;
while fscanffiled d &src &dst EOF
fgetsmessage sizeofmessage file;
messagestrlenmessage; Remove newline character
dijkstrasrc;
if distdst INFINITY
fprintfoutput "from d to d cost infinite hops unreachable message s
src dst message;
else
fprintfoutput "from d to d cost d hops", src dst distdst;
int hop dst;
while prevhop
fprintfoutputd hop;
hop prevhop;
fprintfoutput message s
message;
fclosefile;
void applychangesconst char filename
FILE file fopenfilenamer;
if file
printfError: open input file.
;
exit;
int node node cost;
while fscanffiled d d &node &node &cost EOF
if cost
graphnodenode INFINITY;
graphnodenode INFINITY;
else
graphnodenode cost;
graphnodenode cost;
fclosefile;
int mainint argc, char argv
if argc
printfusage: linkstate topologyfile messagesfile changesfile
;
return ;
readtopologyargv;
FILE output fopenoutputlstxtw;
if output
printfError: open output file.
;
return ;
for int i ; i numnodes; i
dijkstrai;
printroutingtableoutput i;
readmessagesargv output;
applychangesargv;
for int i ; i numnodes; i
dijkstrai;
printroutingtableoutput i;
readmessagesargv output;
fcloseoutput;
printfComplete Output file written to outputlstxt
;
return ;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started