COMP 411: Lab 5: More Recursion in C

Due Wednesday, February 14, 2018, 11:59 PM


Exercise 1: Solving a Maze in C

This exercise involves solving a maze, i.e., finding a path from start to finish without going through walls. This lab is an exercise in recursion as well as 2-D matrices whose maximum size is known.

You will implement a simple recursive method for solving a maze, a java version of which is available here (adapted from here, see under "Recursive algorithm"). We will modify/adapt it for a C implementation. A starter file for your C program is available (ex1.c). Instead of calling the method generateMaze(), the C program reads in the maze from the standard input. Our main() function reads in the maze, initializes the three matrices maze, wasHere and correctPath, then calls recursiveSolve(), and finally prints the maze with the path identified.

Assume the width and height of the maze are provided at run time, but the size of the maze will not exceed 100x100, so storage for the 2-D maze is provided by a fixed-size 100x100 array. At runtime, however, only part of the array is actually used, for a total of height * width entries.

The maze itself will be read from standard input. First its width and height will be present in the input (in that order), followed by the maze, one row at a time. A '*' character represents a wall, and the space character ' ' represents open space through which one can move (i.e., a corridor). One point in the maze will have an 'S' to indicate "start", and one point will have an 'F' to indicate "finish".

Your task is to use the recursive algorithm to find the path, and indicate the path on the maze using the '.' character, and print the maze with the path on the standard output.

For example, for the following 10-column X 5-row maze as the input:


    10
    5
    ****S  ***
    *   ** ***
    ***    **F
    *** ***** 
    ***       

The expected output is:


    ****S..***
    *   **.***
    ***....**F
    ***.*****.
    ***.......

You can assume that the maze is solvable, i.e., a path exists from start to finish. Also, assume that if multiple paths exist, you only need to display the one path found by the algorithm implemented by java program linked to above (it stops after finding one path).

Use the template provided (ex1.c). Complete the code, compile and run on inputs of your choice, and also make sure it runs correctly on the sample inputs provided.

Here are some explanations and useful tips:


Exercise 2: A child couldn't sleep...

In this exercise, you are to write a recursive implementation of a function bedtimestory() that takes a sequence of words and spins them into a story, as follows. If the input to the function bedtimestory() is this sequence of strings—"child", "frog", "bear", "weasel"—then it prints the following output:

A child couldn't sleep, so her mother told a story about a little frog,
  who couldn't sleep, so the frog's mother told a story about a little bear,
    who couldn't sleep, so the bear's mother told a story about a little weasel,
      ... who fell asleep.
    ... and then the little bear fell asleep;
  ... and then the little frog fell asleep;
... and then the child fell asleep.    

Note carefully the subtle differences in the outputs for the different strings above, especially the first and the last strings:

The number of strings that bedtimestory() will be called with will be determined at runtime, not to exceed 20. You will write a main() procedure that will read these strings from the input, put them into an array of strings (up to 20 strings in the array, each string no longer than 15 characters including NULL), and pass this array to bedtimestory(). Use the following code template:


void bedtimestory(char words[20][15], int current, int number) {
  ...                   // Print something before
  ...                   // Recursive call to bedtimestory()
  ...                   // Print something after
}

main() {
  char names[20][15];   // Up to 20 names, each up to 15 letters long (incl. NULL)
  int num;

  ...                   // Read the names from the input
                        // until you read "END"

  bedtimestory(names, 0, num);
}

The main() procedure should read the strings from the input, each string on a line by itself. The last line of the input will have the string "END" on it, indicating that there are no more strings to be read. Use a loop with fgets() in main() to read the input, and store the strings in the array names, and keep track of the total number read. Then call bedtimestory().

For the above example, the input given to the program is:

child
frog
bear
weasel
END

Your implementation of bedtimestory() must be recursive. A non-recursive implementation of the function will receive zero credit. Moreover, since this is an exercise in generating text, any mismatches, including white space errors, will be penalized. Name the file with your C code ex2.c. Test it using your own inputs, and also the sample inputs provided.


Test Inputs, Due Date and Submission Procedure

Reminder: Before beginning work, it is generally a good idea to run the following command so your terminal environment is set up properly:

 % /home/porter/comp411/bin/comp411start

Sample inputs and corresponding outputs are provided under /home/porter/comp411/samples/lab5. You should try running your program on the sample input files provided, and make sure the program's output is identical to that in the sample output files.

Your assignment (i.e., the files ex1.c and ex2.c) must be submitted electronically by 11:59pm on Friday, February 14.

How to submit: Navigate to the folder where these files are stored (~/comp411lab/lab5), and run the following command for running a self-checking script (which does not submit the files):

 % cd ~/comp411lab/lab5
 % /home/porter/comp411/bin/selfchecklab5

If the self-check did not report any errors, you can submit your work as follows:

 % cd ~/comp411lab/lab5
 % /home/porter/comp411/bin/submitlab5

When you invoke the submit script, it submits your files, and once again performs a quick check on your source files (including compiling, running and comparing the output of your program with the expected output, for all sample input/output files), and provides a quick summary of any errors. Your work is submitted regardless of whether errors were found or not, but you are free to fix any errors and submit again before the due date.

In case of any problems, please contact the instructor or the TAs.


29 September 2017, Montek Singh, montek@cs.unc.edu

Last updated: 2018-02-17 11:48:56 -0500 [validate xhtml]