CS 113: Introduction to Computer Science II with Introduction to C

Steve Homer---Spring 1999

Homework 1---due Tuesday, January 26

Reading: Chapters 1,2,3 and 4, pages 1-154.


Please follow the instructions on the Homework and Documentation Guidelines.

The files you must submit are "h1p1.c", "h1p2.c" and "h1p3.c" for the first, second and third problems respectively. You must also submit script files for each program, namely "h1p1.scr", "h1p2.scr" and "h1p3.scr", which will contain your source code (display source code in the script with the "cat" program) and test runs of that code. These test runs must contains runs on our data files for the first two programs (see below), but you may also include additional tests of your own.

The purpose of this assignment is to acquaint you with the basics of using CSA, and to get you started writing simple C programs.

The terminal room is in B24 of MCS at 111 Cummington Street. Or, you can use the public terminal room down the hall in the same building.

There is a very nice on-line emacs tutorial which can be accessed by typing the UNIX command "emacs" then Ctrl-h t.

If you have a personal computer or primarily use another machine, you are welcome to work on programs on that machine. However we cannot help you with PC/Mac problems. I will use CSA to communicate with you and provide data for programs; you must be familiar with it and log on frequently to read messages and collect mail. Whatever you write must run on CSA so we can check your work.

We suggest that you try to write and run a couple of the programs from the book first just to get started. Or, if you like, you can do Part 1 below. Then, do the three programs in Part 2. They are the assignment that is to be turned in.

Part 1

This part is not to be turned in and is optional. If you have written C programs before you can skip this part.

Perform the following tasks on CSA. (You will need to run "new" to request an account and wait for the account to be activated before you can proceed.)

  1. Log on.
  2. Use emacs to create and save the C program written below.
  3. Compile the program using the gcc compiler.
  4. Execute the program, providing input as requested.
  5. Produce a listing of the program and its output on the printer.
  6. Log out.
    /*
     * File name: age.c
     * Name: Steve Homer
     * Login name: homer
     * BUID: U123456789
     * Assignment: 1
     * Problem: 0
     * Date: Jan 1, 1999
     *
     * age.c calculates your age, using the current year and
     * the year of your birth.
     *
     */
    
    /*
     * Inclusions
     */
    #include <stdio.h>
    
    
    int main(void)
    {
      int birth_year, current_year = 1999;
    
      printf("\nIn what year were you born?\n");
      scanf("%d", &birth_year);
      printf("\nSo you will have birthday number %d in %d.\n",
             current_year - birth_year, current_year);
      return 0;
    }
    

    Part 2

    This part is to be turned in using the submit program. Take a look at the UNIX Reference for some info about this program. Data files and sample outputs for some of these programs will be placed in the directory /cs/course/cs113/current/hw1.

    Problems:

    1. Write a C program which takes as input some number of seconds and converts this input to the number of hours, minutes and seconds. Of courses, the seconds and minutes should be no more than 59. The seconds are simply given as an integer.
      So, for example, if the input is 3800 seconds the output would be 1 hour, 3 minutes and 20 seconds.

      Notes: What do you do if one of these is 0 ? And how do you handle the plurals (e.g., second vs. seconds) in your output ? (See our sample outputs)

      Note: Your program should loop through all the data until a -1 is found, signaling the end of the program. You should be able to input the data from a file which you create or just type it in at a terminal. You can find the data in the file /cs/course/cs113/current/hw1/data1.

      You must submit source code file h1p1.c and script file h1p1.scr for this problem.

    2. Develop a C program that will determine the account balance of a bank customer. For each customer, the following facts are available:

      1. Account number
      2. Balance at the beginning of the month
      3. Total of all debits made by this customer this month
      4. Total of all credits applied to this customer's account this month

      The program should input each of these facts in the order shown, calculate the new balance and determine if it is negative, and print the account number and the new balance. If the new balance is negative then the program should also display the message "Your account is overdrawn."

      Your program should enter a loop reading data, and should stop when a -1 is given as an account number. All amounts are given as whole dollar amounts, and thus, are integers. You can find the data in the file /cs/course/cs113/current/hw1/data2.

      Note: Are debits and credits represented by positive or negative numbers (or both)? (See our sample inputs)

      You must submit source code file h1p2.c and script file h1p2.scr for this problem.

    3. Write a C program that reads a binary real number and outputs its decimal value. As an example:
      11001101.1011 = 1 * 2^7 + 1 * 2^6 + 0 * 2^5 + 0 * 2^4 + 1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0 + 1 * 2^-1 + 0 * 2^-2 + 1 * 2^-3 + 1 * 2^-4 = 205.6875

      Your program should read the binary number as an integer (whole number part) and a string (fractional part). Use the format "%d.%s", with a dot between the %d and the %s (the %s refers to a string variable). The fractional part is tricky and there is a set of hints [New] discussing it.

      The program only has to read in and convert one binary number. There is no data file for this one. You should run your program on the binary numbers 1011.001, 110110.0[Updated] , 11010.11111, 100.101, and 11011010.001101 and show the answers your program gets in the script file.

      Hint: Use division and modulus operations to extract the bits from the integer part of the number read.

      You must submit source code file h1p3.c and script file h1p3.scr for this problem.


    Academic Honesty and Collaboration

    It is reasonable to discuss with others possible general approaches to problems. It is unreasonable to work together on a detailed solution, to copy a solution, or to give away a solution. If your common discussion can be detected by looking at the solutions, then there is too much collaboration. Such instances of academic dishonesty may result in a course grade of F or expulsion from Boston University.

    Do not allow your work to be used by others:

    Warning: If someone cheats by using your work, you will also be penalized.