This is a warmup exercise to get you familiar with using Linux.

The assignment

Write a program that reads an integer and shows its factorial. Here is a sample session with the program. Parts shown in black are written by the program. Parts in red are written by the user.

  Show the factorial of: 5
  5! = 120

How to do the assignment

You will find the following useful. Cinnameg expression readInteger( ) reads one integer from the user and yields it as the expression's value. Typically you write a statement such as

  Let n = readInteger().
to read an integer and call it n.

Follow these steps.

  1. Log into Linux. You can do that in Austin 208 by giving your pirate id and password. You should be logged in immediately after that. If you get a complaint that the system cannot install something, just click Ok to make it go away.

    You can also log in from another computer with an internet connection by using the NX client. Download NX and run it. (Do an internet search for "NX Client" to find it.) In the box that pops up, select configure. Set the host to login.cs.ecu.edu, set the host operating system to Unix and the desktop manager to Gnome. Select Ok and login as in the lab.

  2. Double-click on your home folder. Select menu item File/New Folder and create a folder for your CSCI 2310 work. Then open that folder. Right-click it and select Open in terminal. A console will open. You can type commands in the console. End each command with the Enter key.

  3. In the console window type command

      gedit&
    
    A text editor will pop up. (The ampersand at the end says for the terminal not to wait for gedit to finish. That way, you can keep using the terminal without closing the editor.)

  4. Type your program into the text editor. See below for the program requirements. Make the program have the following form.

      Package factorial
    
      Import "collect/string".
    
      ...Your material here...
    
      %Package
    
    The import line lets you use the readString function.

    Save the program when it is ready. Call it factorial.cmg. Do not close the text editor. You will want to keep it open so that you can make modifications to your program.

  5. When you are ready, go to the console window and type command

      ls
    
    You will see a list of the files in this folder. You should see factorial.cmg. If not, either you are in the wrong folder or you have not saved your program.

    Compile your program using command

      cmgc factorial
    
    If the compiler says nothing it means there are no errors. If there are errors, fix them and try again. To fix an error, just go to the text editor, make the change, and save the changed file.

  6. After the program compiles, run it using the following command.

      cmgr factorial
    
    The program should run. If there is a problem, fix it and try it again. Be sure to compile it again after every modification.

  7. After you believe that your program is correct, come back to this web page. Copy your program (factorial.cmg) to the clipboard, then paste it into the box below. Submit it.