Homework 5

Due: December 6, 2019
Points: 100


UNIX/Linux Questions

Please put the answers to these two questions in a text file called linux.txt or a PDF file called linux.pdf.

  1. (10 points) The output from the command ls -l /usr/bin/at shows:
    -rwsr-sr-x 1 daemon daemon 51464 Feb 19  2018 /usr/bin/at
    
    What do the two “s”es in the permissions mean, and what is the effect on the process created when you run this program?
  2. (10 points) Give a Linux command to print dictionary words that contain the vowels ‘a’, ‘e’, ‘i’, ‘o’, ‘u’, and ‘y’ in that order. Show your output.
    Hint: The file /usr/share/dict/words is a dictionary of English words.

C Programming

  1. (50 points) This problem asks you to calculate the atomic weight of molecules.

    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 (which you can ignore for this problem).

    We will proceed in stages, to make life easier. You should turn in only the final program, though.

    1. Write a function to load the contents of the file into an array of structures. The fields of the structure are to contain the chemical symbol and the atomic weight.

    2. Now write a function that takes a chemical compound and breaks it into elements and numbers. The basic unit of a chemical formula is an element’s symbol followed by a number (1 or more digits); the chemical compound’s formula is a sequence of one or more units. For example, the chemical formula for ethanol, C2H5OH, is 2 carbon (C) atoms, 5 hydrogen (H) atoms, an oxygen (O) atom, and another hydrogen (H) atom; and the chemical formula for water, H2O, is 2 H hydrogen (H) atoms and an oxygen (O) atom.

      A good way to check your program is to have it print out each atom’s symbol and the number that follows it, if any.

      Hint: Element symbols are either 1 or 2 letters. The first letter is always capitalized; if there is a second letter, it is always lower case. So “HO” is a hydrogen atom (H) and an oxygen atom (O), and “Ho” is the symbol for holmium. Similarly, “SN” is a sulfur atom (S) and a nitrogen atom (N), and “Sn” is the symbol for tin. Similarly, if no number follows an element’s name, treat it as 1.

    3. Using the functions you wrote in the above two parts, write a program that reads in a chemical compound and prints its atomic weight. Your program is to continue reading input until the user types an end of file.

      Your output is to look like this (input is in red).

      Chemical composition? C2H5OH↵
      The atomic weight of C2H5OH is 46.08
      Chemical composition? H2O↵
      The atomic weight of H2O is 18.02
      Chemical composition? HO↵
      The atomic weight of HO is 17.01
      Chemical composition? Ho↵
      The atomic weight of Ho is 164.93
      Chemical composition? SN3↵
      The atomic weight of SN3 is 74.1
      Chemical composition? Sn3↵
      The atomic weight of Sn3 is 356.13
      Chemical composition? control-D>
      
    To turn in: Put this program into a file called compound.c. Turn it in to Gradescope; you can do this as many times as you like, up to the due date, and we will grade the last one you submit.

Debugging

  1. (30 points) The program getbit.c (available in /home/bishop/hw5-programs/getbit.c) reads in two numbers, n and b. It returns the bth bit of integer n, where the smallest (rightmost) bit is bit number 0. Rather, it is supposed to. But it doesn’t work. Please debug it. Include in the comments a description of the bug(s) and indicate how you fixed it (them).

    To turn in: Put the debugged program into a file called “getbit-fixed.c”. Turn it in to Gradescope; you can do this as many times as you like, up to the due date, and we will grade the last one you submit.

Extra Credit

  1. (30 points) Expand the program you wrote in question 1 to print the names of the elements in the compound, which are in the third field of the file “atomic_weights.txt”. If an element occurs twice, only list it once. Print them in sorted order, using proper grammar (so if there is one element, print “The element is”, and if there are more than one, print “The elements are”). Put commas after all but the last element names, and separate the last two with “and”.

    The output for the same input as in question 1 would look like this (input is in red).

    Chemical composition? C2H5OH↵
    The atomic weight of C2H5OH is 46.08
    The elements are Carbon, Hydrogen, and Oxygen
    Chemical composition? H2O↵
    The atomic weight of H2O is 18.02
    The elements are Hydrogen and Oxygen
    Chemical composition? HO↵
    The atomic weight of HO is 17.01
    The elements are Hydrogen and Oxygen
    Chemical composition? Ho↵
    The atomic weight of Ho is 164.93
    The element is Holmium
    Chemical composition? SN3↵
    The atomic weight of SN3 is 74.1
    The elements are Nitrogen and Sulfur
    Chemical composition? Sn3↵
    The atomic weight of Sn3 is 356.13
    The element is Tin
    Chemical composition? control-D
    

    To turn in: Put this program into a file called longcompound.c and submit it to Gradescope. Turn it in to Gradescope; you can do this as many times as you like, up to the due date, and we will grade the last one you submit.


UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming and Problem Solving
Version of November 26, 2019 at 12:31AM

You can also obtain a PDF version of this.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh