In: Computer Science
This is the homework given by the teacher. I don't have more information
Complete the below code so that your program generates a random walk on the given graph. The length of your walk should also be random.
/******************************************************************/
#include
#include
#include
typedef struct NODE_s *NODE;
typedef struct NODE_s{
char name;
NODE link[10];
}NODE_t[1];
#define nodeA 0
#define nodeB 1
#define nodeC 2
#define nodeD 3
#define nodeE 4
#define nodeF 5
int main() {
srandom(time(NULL));
//printf("%d\n", random());
NODE_t node[6];
node[nodeA]->name = 'A';
node[nodeB]->name = 'B';
node[nodeC]->name = 'C';
node[nodeD]->name = 'D';
node[nodeE]->name = 'E';
node[nodeF]->name = 'F';
int i, j;
for (i = 0; i < 6; i++) {
for (j = 0; j < 10; j++) {
node[i]->link[j] = NULL;
}
}
//a -> c,d,e,f.
node[nodeA]->link[0] = node[nodeC];
node[nodeA]->link[1] = node[nodeD];
node[nodeA]->link[2] = node[nodeE];
node[nodeA]->link[3] = node[nodeF];
//b -> c,f.
node[nodeB]->link[0] = node[nodeC];
node[nodeB]->link[1] = node[nodeF];
//c -> a,b,e.
node[nodeC]->link[0] = node[nodeA];
node[nodeC]->link[1] = node[nodeB];
node[nodeC]->link[2] = node[nodeE];
//d -> a,e,f.
node[nodeD]->link[0] = node[nodeA];
node[nodeD]->link[1] = node[nodeE];
node[nodeD]->link[2] = node[nodeF];
//e -> a,c,d.
node[nodeE]->link[0] = node[nodeA];
node[nodeE]->link[1] = node[nodeC];
node[nodeE]->link[2] = node[nodeD];
//f -> a,b,d.
node[nodeF]->link[0] = node[nodeA];
node[nodeF]->link[1] = node[nodeB];
node[nodeF]->link[2] = node[nodeD];
//Random walk.
int choice = random() % 6;
printf("%c\n", node[choice]->name);
return 0;
}
/******************************************************************/
May be the code is like this:
include <stdio.h>
#include<math.h>
#include<time.h>
typedef struct NODE_s *NODE;
typedef struct NODE_s{
char name;
NODE link[10];
}NODE_t[1];
#define nodeA 0
#define nodeB 1
#define nodeC 2
#define nodeD 3
#define nodeE 4
#define nodeF 5
int main() {
srandom(time(NULL));
//printf("%d\n", random());
NODE_t node[6];
node[nodeA]->name = 'A';
node[nodeB]->name = 'B';
node[nodeC]->name = 'C';
node[nodeD]->name = 'D';
node[nodeE]->name = 'E';
node[nodeF]->name = 'F';
int i, j;
for (i = 0; i < 6; i++) {
for (j = 0; j < 10; j++) {
node[i]->link[j] = NULL;
}
}
//a -> c,d,e,f.
node[nodeA]->link[0] = node[nodeC];
node[nodeA]->link[1] = node[nodeD];
node[nodeA]->link[2] = node[nodeE];
node[nodeA]->link[3] = node[nodeF];
//b -> c,f.
node[nodeB]->link[0] = node[nodeC];
node[nodeB]->link[1] = node[nodeF];
//c -> a,b,e.
node[nodeC]->link[0] = node[nodeA];
node[nodeC]->link[1] = node[nodeB];
node[nodeC]->link[2] = node[nodeE];
//d -> a,e,f.
node[nodeD]->link[0] = node[nodeA];
node[nodeD]->link[1] = node[nodeE];
node[nodeD]->link[2] = node[nodeF];
//e -> a,c,d.
node[nodeE]->link[0] = node[nodeA];
node[nodeE]->link[1] = node[nodeC];
node[nodeE]->link[2] = node[nodeD];
//f -> a,b,d.
node[nodeF]->link[0] = node[nodeA];
node[nodeF]->link[1] = node[nodeB];
node[nodeF]->link[2] = node[nodeD];
//Random walk.
int choice = random() % 6;
printf("%c\n", node[choice]->name);
Int lenthofwalk = random%6;
Printf("%c",choice.lenth);
return 0;
}