CS010 Assignment 1

Basic C, Pointers, Strings, Arrays

Due: January 14, 5 PM

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.

  1. In Java, the sizes of the predefined types, like char and int, are all well-defined. This means that they are guaranteed to use the same range of values in all implementations. In C, these sizes are not well-defined. Different implementations can use different sizes for different types. This makes it difficult to write a program that will behave the same way on all machines. C provides 4 different integer types: char, short, int, and long. One might expect each of these to have a different size, but all the language definiion requires is that long is at least as long as int, int is at least as long as short and short is at least as long as char and char can hold the ASCII character set (anything you can type on a standard keyboard, including using shift and control modifiers).

    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.

  2. Write a program that prompts the user for two strings: a string to search through and a string to search for. The program should report if the 2nd string occurs within the first string or not. Sample output might be the following:
    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.

  3. It is possible to place eight queens on a chessboard (an 8 x 8 grid) such that none of the queens can capture any of the other queens. For this to be true, it must be the case that there is exactly 1 queen in each row, each column, and each diagonal. Write a program that finds one solution to the eight queens problem.

    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[10][10]; 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.

  4. For a bonus, modify your eightqueens program to display all solutions to the problem along with a count of the number of solutions found.


Return to CS 010 Home Page