This assignment should be turned in for evaluation. To turn in the assignment, you will use the turnin command:
turnin -c 010 <file.c>
where you substitute the name of your file for <file.c>. It is ok to turn in the same file more than once. This will overwrite the earlier version. You may want to do this if you discover a mistake or make an improvement after turning in your assignment.
C provides a sizeof function. This function takes a single parameter, which is the name of a type. It returns the size of the type in bytes. For this question, use the sizeof function to determine the size of the four integer types in C on FreeBSD as well as the size of pointers and print out the result. (Hint: the universal pointer type in C is called "void *". So, to find the size of pointers, use "void *" as the type name.)Place this program in a file called sizeof.c.
Enter the string to search through: catnip Enter the string to search for: cat cat was found in catnip! Enter the string to search through: catnip Enter the string to search for: mouse mouse was not found in catnip! Enter the string to search through: catnip Enter the string to search for: nip nip was found in catnip!
Note that the values following the : characters are input by the user. You should allow blanks in the strings. Place this program in a file called match.c. When writing this program, please write your own function to search the strings, rather than strstr from C's string library.
Take this problem one step at a time. First, define the array that you will use to hold your chessboard and remember where the queens are. In C, you create a 2-d array exactly as you would expect. For example, double arr; declares and allocates space for a 10x10 array of doubles. Then write a function that draws the chessboard (using normal chracters, like '-' and '|' to represent the boundaries between squares on the chessboard. Output an x or q or something to indicate the location of queen, and a space to indicate an empty cell.)
Next, write your code such that it ensures each queen is in a separate row, but don't check the columns or diagonals.
When that works, add a check for columns.
When that works, add a check for one of the diagonals.
Finally, add the check for the second diagonal.
At some point, you may discover that you ran through your algorithm, did all the checks, and fewer than 8 queens were placed. If you examine your displayed chessboard, you should discover that, in fact, given the locations of the first few queens, it is not possible to place the remaining queens. To solve this problem, your algorithm needs to recognize that no more pieces can be added and to backtrack - move an already placed piece to a new location. You need to use recursion as described below to solve this problem.
Your recursive function should take an integer parameter, where the parameter specifies the row in which the next queen should be placed. It should attempt to find a legal position for the queen and place it given the already placed queens. This function should return true if it has placed a queen in the last row. The function should return false if it could not place a queen in the row. This is an indication that some already-placed queen should be moved.
If it placed a queen, but not in the last row, it should call itself recursively, passing in the next row. If this recursive call returns true, then this call should also return true. Returning true indicates that we placed a queen in the current row, and because of the recursion, queens have also been placed in all subsequent rows.
If the recursive call returns false, we should move the queen that we placed in this row to the next legal column and try the recursion again. If there are no more legal columns, we should just return false.
When all the queens have been placed, display the board.
Put your program in a file called queens.c.
Return to CS 010 Home Page