Question

In: Computer Science

Complete the below code so that your program generates a random walk on the given graph....

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 <stdio.h>
#include <stdlib.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);

return 0;
}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include <stdio.h>

#include <stdlib.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()

{

    srand(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 = rand() % 6;

    printf("%c\n", node[choice]->name);

    //storing current node in a variable

    NODE current = node[choice];

    //generating a random length between 5 and 15

    int length = (rand() % 11) + 5;

    //looping for length times

    for (int i = 0; i < length; i++) {

        //generating a random index between 0 and 9

        int next = rand() % 10;

        //looping as long as the link at this next is NULL

        //assuming each node has at least one valid link

        while (current->link[next] == NULL) {

            //generating another index

            next = rand() % 10;

        }

        //moving current to the link pointed by next

        current = current->link[next];

        //printing new location

        printf("%c\n", current->name);

    }

    return 0;

}

/*OUTPUT*/

B

F

D

E

D

F

B

C

B

C

B

F

A

E


Related Solutions

The code below is giving an arithmetic overflow. Correct the below given the below code, so...
The code below is giving an arithmetic overflow. Correct the below given the below code, so that the value of s0 is printed before calling the function fun1, inside the function Fun1 and after returning from Fun1 in main. Upload the corrected .asm or .s file in the drop box. .text main: addi $s0,$zero,2 jal Disp jal Fun1 jal Disp j Exit Fun1: addi $sp,$sp,-4 sw $s0,0($sp) addi $s0,$s0,15 jal Disp lw $s0,0($sp) addi $sp,$sp,4 jr $ra Disp: li $v0,1...
Please code C# 6. Write a program that generates a random number between 0 and 10...
Please code C# 6. Write a program that generates a random number between 0 and 10 and asks the user to guess that number. The program then informs the user if they guessed correctly or incorrectly then displays the correct answer.
// (A) Implement the code below. For a given graph, return a Set of type T...
// (A) Implement the code below. For a given graph, return a Set of type T of all of the strongly connected nodes of the given key. public Set<T> stronglyConnectedComponent(T key) { return null; } // (B) Implement the code below. Optimally find the shortest path between the given start and end node. public List<T> shortestPath(T startLabel, T endLabel) {        return null; }
[50%] Code Snippet Given snippet code below that you are required to complete. You are not...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not allowed to make a new function or change any given code. Please complete each section that are marked with the notation “INSERT YOUR CODE HERE”. Once you complete the snippet below, your output should have the same result with the given output below. Descriptions: [15%] isValid() This function is for checking that there is no duplicated employee data in the linked list. This function...
Given the following program below answer the following questions. 1.Draw a program flow graph for the...
Given the following program below answer the following questions. 1.Draw a program flow graph for the binsearch() function 2. Find the Define and Usage node, du-paths and dc-paths for all the variables int binsearch(int x,int v[],int n) { int low,high,mid; low=0; high=n-1; while(low<high) { mid = ( low + high ) / 2; if( x < v[mid]) high = mid - 1; else if ( x > v[mid]) low = mid + 1; else return mid; } return -1; }
In this program, you are modifying given code so that the class is object-oriented. 2. Write...
In this program, you are modifying given code so that the class is object-oriented. 2. Write a Java class called CityDistancesOO in a class file called CityDistancesOO.java.    3. Your class will still make use of two text files. a. The first text file contains the names of cities with the first line of the file specifying how many city names are contained within the file.    b. The second text file contains the distances between the cities in the...
. Determine whether K4 (the complete graph on 4 vertices contains the following: i) A walk...
. Determine whether K4 (the complete graph on 4 vertices contains the following: i) A walk that is not a trail. ii) A trail that is not closed and is not a path. iii) A closed trail that is not a cycle.
Design c++ program so that it correctly meets the program specifications given below.   Specifications: Create a...
Design c++ program so that it correctly meets the program specifications given below.   Specifications: Create a menu-driven program that finds and displays areas of 3 different objects. The menu should have the following 4 choices: 1 -- square 2 -- circle 3 -- right triangle 4 -- quit If the user selects choice 1, the program should find the area of a square. If the user selects choice 2, the program should find the area of a circle. If the...
Given a list of items, write a program that generates a list of lists of the...
Given a list of items, write a program that generates a list of lists of the following form: [a,b,c,...,z]⇒[[z], [y,z], [x,y,z], ... , [a,b, ... ,y,z]] Hint: Slicing is your friend. please write a python program
Write the python code that generates a normal sample with given μ and σ, and the...
Write the python code that generates a normal sample with given μ and σ, and the code that calculates m (sample mean) and s (sample standard deviation) from the sample.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT