CS35: Assignment 3

CS35: Programming and Problem Solving
Ray Ontko
Department of Computer Science
Earlham College

Due Wednesday, 1999/09/01

Note: Some of the following will be completed in Lab on Monday, 8/30.

Reading Assignment

Read section 2.3 of Deitel and Deitel. Optionally, read section 1.2 of Kernighan and Ritchie.

Lab Assignment

  1. Use the "telnet" program to connect to one of the machines in the Athena cluster ("athenax.pvm.cs.earlham.edu", where x is a digit 0..9 or a number a..f.) Enter your CS username and its initial password.

  2. Use the "yppasswd" command to change your password to something you can remember.
    1. Enter the command "yppasswd" (followed by the Enter key). You will be prompted for your old password.
    2. Enter your old password. You will be prompted for your new password.
    3. Enter your new password. You will be prompted to re-enter your new password. If things don't seem to work correctly, you may wish try again by re-entering the "yppasswd" command.

  3. Use the "pico" editor to create a new C program.
    1. At the command prompt, type "pico pounds.c". You should see a bar across the top line of the terminal window, and a menu of options occupying the bottom few lines.
    2. Type in the following program, letter for letter, typing the Enter key at the end of each line, or the delete key to erase any typing errors. Use the arrow keys to move the cursor around as needed. For now, just enter exactly what you see. We'll worry about what it means later. The purpose of this exercise is to get you familiar with some of the program-creating tools.
      /* 
      This program displays a conversion table
      from dollars to pounds.  It prompts the 
      user for the initial and final value in 
      dollars, the amount to increment by, and
      the exchange rate. 
      */
      
      #include <stdio.h>
      
      main()
      {
        float initial_dollars , increment_dollars, final_dollars , exchange_rate ;
        float dollars , pounds ;
      
        printf( "pounds -- prints a conversion table from dollars to pounds\n\n" ) ;
      
        printf( "Enter the initial dollar amount\n" ) ;
        scanf( "%f" , &initial_dollars ) ;
        printf( "Enter the number of dollars by which to increment\n" ) ;
        scanf( "%f" , &increment_dollars ) ;
        printf( "Enter the final dollar amount\n" ) ;
        scanf( "%f" , &final_dollars ) ;
        printf( "Enter the exchange rate\n" ) ;
        scanf( "%f" , &exchange_rate ) ;
      
        printf( "\n" ) ;
        printf( "   Dollars      Pounds\n" ) ;
        
        dollars = initial_dollars ;
        while( dollars <= final_dollars )
        {
          pounds = dollars * exchange_rate ;
          printf( "%10.2f  %10.2f\n" , dollars , pounds ) ;
          dollars += increment_dollars ;
        }
      }
      
    3. When you're done typing, hold the CTRL key down (like you would a shift key) and type the X key. (This key combination corresponds to the ^X you see at the bottom of the Pico screen). You will be prompted for whether you wish to "Save modified buffer".
    4. Type the "Y" key to indicate that you wish to save your changes. You will then be prompted for the "Filename to write".
    5. Type the Enter key to indicate that you wish to write your changes to the file called "pounds.c". You should now be back at the "$" command prompt.

  4. Use the "cc" command to compile your program. During this step, the file you created will be translated from the "C" language into machine language, that is, into a language that can be executed directly by the computer hardware. Simply enter the command
    $ cc -o pounds pounds.c
    

    This command instructs the C compiler ("cc") to create an output file ("-o") named "pounds" by reading the program file "pounds.c".

    If all goes well, you should simply get another "$" command prompt. If something isn't exactly right with the program, you may get a number of error messages. If you get error messages, read them carefully, and use "pico pounds.c" to go back and see if you can change your program to correct them.

  5. Now, you're ready to run your program. You can do this by typing "./" in front of the name of your compiled program. (Later we'll learn how to do this without the "./".)
    $ ./pounds
    
    The program will prompt you for a series of values. If you enter 1, 1, 10, and 0.6543, you should see a screen that looks like this:

    pounds -- prints a conversion table from dollars to pounds
    
    Enter the initial dollar amount
    1
    Enter the number of dollars by which to increment
    1
    Enter the final dollar amount
    10
    Enter the exchange rate
    0.6543
    
       Dollars      Pounds
          1.00        0.65
          2.00        1.31
          3.00        1.96
          4.00        2.62
          5.00        3.27
          6.00        3.93
          7.00        4.58
          8.00        5.23
          9.00        5.89
         10.00        6.54
    

  6. Try running your program several different times using different input values, even some nonsensical ones. Try to explain the behaviour you see.

  7. Try making changes to your program, and recompile and re-run it. See what kind of errors you can get. What changes can you make and still have a working program?

  8. Here is a summary of commands you already know or may be interested in.

    CommandDescription
    ls List the files in my directory.
    ls -l List the files in my directory in "long" format, showing file protections, ownerships, sizes, and dates.
    mv file1 file2 Rename (move) a file from the name file1 to the new name file2.
    rm file Delete (remove) a file.
    pico file Use the pico editor to create or modify a file.
    cc -o executable-file source-file.c Use the C compiler to read a C source-file.c and produce an executable-file.
    ./executable-file Invoke an executable-file produced by a compiler.

    As an exercise, try renaming your "pounds" executable file to "sterling", and then execute "sterling". Also, try deleting your "hello" executable file.

  9. When you're done, be sure to logout.
    1. Use the "logout" command to log off the athenax machine, and
    2. close the terminal window.

Copyright © 1999, Ray Ontko (rayo@ontko.com).