COMP2521 18s2 COMP2521 (18s2) Week-12 Lab Exam (sample_exam) Data Structures and Algorithms
[Instructions] [C language]
[Q1] [Q2]

Question 2 (10 marks)

In the q2 directory, you will find the following files/directories:

Your task for this question is to implement the function int hasPath(Graph g, int s, int d) in the file hasPath.c. The function hasPath takes three arguments: a graph g, source node s and destination node d. The function returns 1 if there a path from s to d, returns 0 otherwise.

int hasPath(Graph g, int s, int d);

You can compile and test your function using the following commands:

$ make    # builds the required program testHasPath
$ ./autotest  # apply all the tests in the tests directory to the program testHasPath
$ ./autotest <test-number>   # apply only one given test case from the tests directory to the program testHasPath

You can find out more about the behaviour of the testHasPath program by looking at the files testHasPath.c, autotest and the files in the tests directory. The files named tX.in are input files for the test program testHasPath and the corresponding expected output are in tX.exp.

If you want to run say test case 2, use the following command:

$ ./autotest 2  

If you want to run all the sample test cases provided, use the following command:

$ ./autotest   

After you run the tests, additional files will appear in the tests directory: tX.out contains the output (from your function) of running test tX.

IMPORTANT: You can add debugging code to hasPath.c or testHasPath.c if you want, however, make sure that you remove it before testing and submitting, otherwise your output won't match the expected output and you'll fail all the tests. You can also add any auxiliary functions to hasPath.c that you think are necessary.

Once you are satisfied with your program, submit it using the command:

$ submit q2

This will make a copy of the hasPath.c file from the q2 directory as your answer for this question. You can run the submit command as many times as you like, but make sure that your final submission compiles without any errors or warnings.

Testing:

Test your program thoroughly, possibly using test cases additional to those supplied. Your program will be tested using test cases different to the example test cases provided in the tests directory.

If you guess a possible answer/output and pass a test, you will not be awarded marks. Your program must properly solve the task discussed above.


If, at some stage, you need to "re-install" the files (although you should not need to), you can copy all of the original files for a question.

Beware: this will overwrite all of your exsting files for the given question, so first backup your current files/directory and only do it if you seriously mess things up.

$ re-start <question-dir>
For example, say for q2, you can copy original files into the q2 directory by running the command:

$ re-start q2