Homework 3 Revision 1

Revision 1, May 14, 2025: The due date changed from May 14 to May 16.

Date: May 16, 2025
Points: 100

All programs are to be submitted through Gradescope. Gradescope will compile and execute your program, sometimes with varying inputs or invocations. You can see your score once executions are done. You can submit your program multiple times, up to the deadline — at that point, the score you have will usually be the grade for that problem. I say “usually” because we reserve the right to look at your submission and deduct points if you do not use proper programming style.

You have to name your program as given in the problem. Otherwise you will get an odd error message indicating there is a problem with Gradescope.

Also, your output must match Gradescope’s exactly, including blanks and tabs — so pay attention to the example output!

  1. (25 points) Write a program that prints a tic-tac-toe board. Each square is to be 7 × 7, with “#” marks. The “X”s and “O”s are to be 5 × 5, composed of “X”s, and “O”s respectively, and placed in a square the coordinates of which are given as input.

    Your program first prompts for, and reads, two integers separated by blanks or tabs between 1 and 3 inclusive; if either is not, print an error message giving both numbers and stating it is not a valid square and prompt for two numbers again. If you encounter an end-of-file, end the program. For example:

    > 2 4
    2,4 is not a valid square; the numbers must be between 1 and 3 inclusive
    > 2,4
    Illegal character in input ","
    >
    
    When an error in input occurs, print the error message on the standard error (stderrr) and ask for input again.

    The upper left square has co-ordinates (1, 1) and the lower right corner has coordinates (3,3).

    The two numbers are co-ordinates of the move. The first move puts an “X” in the identified square. Your program then prompts again, and this time it puts an “O in the identified square. This continues, alternating between “X”s and “O”, until either the board is full or an EOF is encountered. When either of these cases occur, exit the program with an exit status code of 0.

    Your prompts reflect whether the turn is for “X” or for “O”. When it is “X”s turn, the prompt is

    X's turn >␣
    
    (note the space after “>”), and when it is “O”s turn, the prompt is
    O's turn >␣
    
    (again, note the space after “>”).

    Here is an example input and output; the input is in \textcolor{red}{red} and the output in black.

    X's turn > 1 1
           #       #
     X   X #       #
      X X  #       #
       X   #       #
      X X  #       #
     X   X #       #
           #       #
    #######################
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
    #######################
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
    O's turn > 2 2
           #       #
     X   X #       #
      X X  #       #
       X   #       #
      X X  #       #
     X   X #       #
           #       #
    #######################
           #       #
           # OOOOO #
           # O   O #
           # O   O #
           # O   O #
           # OOOOO #
           #       #
    #######################
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
           #       #
    X's turn > 4 2
    4,2 is not a valid square; the numbers must be between 1 and 3 inclusive
    X's turn > 
    
    Call your program toe.c. You can find an executable for it in the directory /home/bishop/hw3.

  2. (35 points) The file atomic_weights.txt contains lines with three fields separated by tabs. The first field is the atomic weight, the second field is the symbol for the element, and the third field is the name of the element .

    Write a program to load the contents of the file into an array, and then print out the array.The program is to take 1 argument on the command line, which is the name of the file containing the input lines.

    When you print them, print the weight as a floating point number and the symbol and name as strings. The three fields are separated by tabs. You can use this string as the format for printing: “%6.2f\t%s\t%s\n”.

    If you encounter one of the following errors, here’s what you do:

    1. There is more than 1 argument (that is, argc does not equal 2): print the following, where %s is replaced by the program’s name (argv[0]); “Usage: %s file\n”. Then return an exit status code of 1.
    2. The file cannot be opened: use perror with the file name as an argument. Then return an exit status code of 1.
    3. The atomic weight is out of range (that is, larger than the maximum integer or smaller than the minimum number), print the following, with “%d” replaced by the line number: “Line %d: bad atomic weigh\n” and skip the rest of the line.
      Hint:C heck the manual page for strtod(3).
    4. The line does not begin with an atomic weight: print the following, with “%d” replaced by the line number: “Line %d: missing atomic weight\n” and skip the rest of the line.
    5. If the line ends with only an atomic weight, and there is no symbol or name, print the following, with “%d” replaced by the line number: “Line %d: no element symbol or name\n” and skip the rest of the line.
    6. If the atomic symbol is one character, it must be capitalized; if it is 2, the first letter must be capitalized and the second lower case. If this is not so, print the following, with “%d” replaced by the line number and “%s” with the symbol on the line: “Line %d: bad atomic symbol: %s\n” and skip the rest of the line.
    7. If there is no name for the element, print the following, with “%d” replaced by the line number: “Line %d: missing element name\n” and skip the rest of the line.

    Call your program atomic.c You can find an executable for it, and the file atomic_weights.txt, in the directory /home/bishop/hw3.

  3. (40 points) Now modify atomic.c to sort the elements. The user is to be able to sort them in 3 ways.
    1. It the option -n is given, sort the elements by name and print them in that order.
    2. It the option -s is given, sort the elements by symbol and print them in that order.
    3. It the option -w is given, or no option is given, sort the elements by atomic weight and print them in that order.
    So your program must take 1 (the file name) or 2 (the option and the file name) arguments.

    Call your program atomicsort.c. You can find an executable for it, and the file atomic_weights.txt, in the directory /home/bishop/hw3.