CS157

PmWiki

edit SideBar

Homework 2: File I/O, Two-Dimensional Arrays, Structures

Description

For this assignment, you will complete a mostly-written program. The program will take two arguments:

  • The name of a file that contains a word puzzle
  • A word to search for

The program will then search for the word in the puzzle, and indicate the position of the word in the puzzle, if found.

Data File

Here’s an example of a puzzle file:

    r j o e s v u t
    a b a r a c k b
    h t x g r a p i
    m o h n a y a d
    a b j q h f l e
    n a a p p l i n
    u m c c a i n z
    w a k e j o h n

This file contains the word “joe” in the first line, going across, and the word “man” in the first column, going down. Words can only go across or down, to keep it simple.

Here's another example:

    bjflqhea
    aalippnn
    mcincazu
    akohejnw
    jovuestr
    backraba
    txapgrih
    ohyanadm

The puzzle file will contain only lower-case letters, spaces, and newlines. You should ignore all spaces in the puzzle files. When you print out the puzzle, you must print a space before each letter.

Sample Runs

    % gcc -std=c99 word.c -o word

    % ./word puzzle mccain
    The puzzle:
     r j o e s v u t
     a b a r a c k b
     h t x g r a p i
     m o h n a y a d
     a b j q h f l e
     n a a p p l i n
     u m c c a i n z
     w a k e j o h n

     * * * * * * * *
     * * * * * * * *
     * * * * * * * *
     * * * * * * * *
     * * * * * * * *
     * * * * * * * *
     * m c c a i n *
     * * * * * * * *

    % ./word puzzle obama
    The puzzle:
     r j o e s v u t
     a b a r a c k b
     h t x g r a p i
     m o h n a y a d
     a b j q h f l e
     n a a p p l i n
     u m c c a i n z
     w a k e j o h n

     * * * * * * * *
     * * * * * * * *
     * * * * * * * *
     * o * * * * * *
     * b * * * * * *
     * a * * * * * *
     * m * * * * * *
     * a * * * * * *

    % ./word puzzle bush
    The puzzle:
     r j o e s v u t
     a b a r a c k b
     h t x g r a p i
     m o h n a y a d
     a b j q h f l e
     n a a p p l i n
     u m c c a i n z
     w a k e j o h n

    "bush" was not found.

Skeleton

Most of the program has already been written—you just have to fill in a few functions. Look for “YOUR CODE GOES HERE”, and add your code there. Don’t change anything else in the program. Here it is:

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>              // for isalpha

    #define SIZE 8                  // Width and height of puzzle

    // Possible values for location.length:
    #define NOT_FOUND 1
    #define DIRECTION_HORIZ 2
    #define DIRECTION_VERT 3

    struct location {
            int x;          // Starting location of the match
            int y;          // Starting location of the match
            int direction;  // NOT_FOUND or DIRECTION_HORIZ or DIRECTION_VERT
            int length;     // The length of the match
    };

    int read_puzzle(char fname[], char puz[SIZE][SIZE]);
    void display_puzzle(char puz[SIZE][SIZE]);
    struct location find_word(char puz[SIZE][SIZE], char word[]);
    void display_word(char puz[SIZE][SIZE], struct location answer);


    int main(int argc, char *argv[])
    {
            char puzzle[SIZE][SIZE];
            struct location where;

            // There must be exactly two arguments.
            if (argc != 3) {
                    printf("Usage: %s puzzle word\n", argv[0]);
                    return 1;
            }

            // Read the puzzle from the file, exit if we can't.
            if (read_puzzle(argv[1], puzzle) != 0)
                    return 2;

            printf("The puzzle:\n");
            display_puzzle(puzzle);
            printf("\n");

            // Look for the given word in the puzzle.
            where = find_word(puzzle, argv[2]);

            // Display the word in the puzzle, or complain.
            if (where.direction == NOT_FOUND)
                    printf("\"%s\" was not found.\n", argv[2]);
            else
                    display_word(puzzle, where);

            return 0;
    }


    // Read the puzzle from the given filename.
    //
    // The first letter you read should go into puz[0][0],
    // the second letter into puz[1][0],
    // the third letter into puz[2][0], and so on.
    //
    // Return value: 0 for success, 1 for failure.
    // If the file couldn't be opened, print an error message.

    int read_puzzle(char fname[], char puz[SIZE][SIZE])
    {
            // YOUR CODE GOES HERE
    }


    // Display the puzzle.
    //
    // When you print out the puzzle,
    // you must print a space before each letter.
    //
    // Return value: none

    void display_puzzle(char puz[SIZE][SIZE])
    {
            // YOUR CODE GOES HERE
    }

    // Is the given word in the puzzle at this location?
    //
    // Return value: boolean: 1 for success, 0 for failure.

    int is_word_at(char puz[SIZE][SIZE],
                   char word[],
                   int x, int y,
                   int direction)
    {
            for (int i=0; word[i] != '\0'; i++) {
                    if (x < 0 || x>=SIZE || y<0 || y>=SIZE)
                            return 0;               // Outside the puzzle, fail
                    if (puz[x][y] != word[i])
                            return 0;               // No match, fail
                    if (direction == DIRECTION_HORIZ)
                            x++;
                    else
                            y++;
            }
            return 1;                               // It must have matched!
    }


    // Find the given word in the puzzle.
    //
    // Return value: a struct location.  It will either contain:
    //      length=NOT_FOUND if we couldn't find the word
    //      length=DIRECTION_HORIZ if it's in the puzzle going across
    //      length=DIRECTION_VERT if it's in the puzzle going down

    struct location find_word(char puz[SIZE][SIZE], char word[])
    {
            struct location result;

            result.length = strlen(word);

            // Look for the word starting at every location in the puzzle.
            // It might go across, and it might go down.

            for (int y=0; y<SIZE; y++) {
                    for (int x=0; x<SIZE; x++) {
                            result.x = x;
                            result.y = y;
                            if (is_word_at(puz, word, x, y, DIRECTION_HORIZ)) {
                                    result.direction = DIRECTION_HORIZ;
                                    return result;
                            }
                            if (is_word_at(puz, word, x, y, DIRECTION_VERT)) {
                                    result.direction = DIRECTION_VERT;
                                    return result;
                            }
                    }
            }

            result.direction = NOT_FOUND;
            return result;
    }

    // Display the puzzle, but only display the word that we found.
    // All other letters will be replaced by asterisks.
    //
    // When you print out the puzzle,
    // you must print a space before each letter.
    //
    // Return value: none

    void display_word(char puz[SIZE][SIZE], struct location answer) {
            // YOUR CODE GOES HERE
    }

Grading

Homework 2 is worth 10 points. You’ll lose points if you submit a file that:

  • changes any part other than that marked “YOUR CODE GOES HERE
  • doesn’t compile
  • doesn’t work
  • isn’t named word.c
  • uses language features that we have not yet covered
  • changes any of the prompts or messages
  • contains poor code:
    • cryptic variable names
    • missing/useless comments
    • extra { } for no reason
  • and many, many, more!

How to submit your homework:

Go to the same directory as the word.c file, and do this:

    ~cs157/bin/checkin HW2 word.c

How to check your answer after you turn it in:

    ~cs157/bin/peek HW2 word.c

How to receive negative points:

Turn in someone else’s work.
Edit - History - Print - Recent Changes - Search
Page last modified on November 21, 2008, at 10:06 PM