(Back to the main page)

Homework 1: Hello World and Other Basics (100 pts, due Monday January 26th, by 11:59 PM)

If you already know how to compile code on eniac, click here to skip to the actual assignment!

To get started, we're going to get your programming environment set up and make sure that you can compile a simple C program.

One great thing about C and C++ is that there are powerful integrated development environments (or IDEs) to use for every major platform. In the name of simplicity and fundamentals, however, we're going to use the Unix command-line tools in our instructions and in lecture. You are free to use a richly-featured IDE (such as Eclipse) instead of the command-line tools for your own work. Feel free to ask questions on Piazza about these, but the only officially supported development platform will be the Unix command-line tools.

Connecting to eniac

SEAS maintains a Linux cluster, eniac, for all students to use for academic work. Eniac is already equipped with the programs we need to write and compile C/C++ programs, so all we need to do is connect to eniac to get started!

  1. Get a SEAS account. If you are a SEAS student, then you already have an account. Otherwise, you can go to the SEAS account management page to create a new account.
  2. Download ssh software. If you have a Mac or are running Linux, then you can simply open a terminal window (via the Terminal application for the Mac folk) and use the ssh program to connect to eniac (details below). If you are running Windows, then you'll need to download PuTTy or SecureCRT. These are both SSH clients. Note that if you are using the lab machines in Levine/Towne/Moore, SecureCRT is already available to use.
  3. Connect to eniac. The full address of the eniac server is eniac.seas.upenn.edu. To connect to eniac with the ssh program in a terminal window (either Mac or Linux), enter
    ssh user@eniac.seas.upenn.edu
    
    where "user" should be replaced with your SEAS username. If you're using one of the programs for Windows, enter eniac.seas.upenn.edu as the server name/address and then connect. Once you are connected to eniac, you will be prompted to enter your username (if you haven't done so already) and your SEAS password. Along the way, you can answer yes to any prompts regarding host identity and saving eniac's host key.

    If you have difficulties connecting to eniac, see the CETS support entry for connecting to eniac for more information. If that doesn't answer your questions, post on Piazza.

Navigating the command-line interface

Once you are connected to eniac, you will be greeted with a command-line prompt. For example, this is some sample output one might get when connecting to eniac via a terminal window:
Katie@masks37:~$ ssh gibsonk@eniac.seas.upenn.edu
gibsonk@eniac.seas.upenn.edu's password: 
Last login: Thu Dec 26 18:23:35 2013 from 2607:f470:8:1050:6c0b:e027:6c13:e6e8
gibsonk@plus:~> 
At this prompt, you can enter commands to navigate your fileshare on eniac or execute programs. Since this is not the Linux skills class, we won't dwell too long on how to be a master command-line user. But here are the basic commands and some example output to get you started:

These commands should be enough to get you through the Unix command-line portion of the class. If you want more information about navigating the command line, you can start with this article from wikibooks. Also, if you're interested in learning more, I recommend taking CIS 191, Unix/Linux Skills, next time it's offered!

Editing text at the command line

In addition to these shell commands, you can also run programs from the command line. One of the programs we will need to run is a text editor to be able to write source code. And the other is a compiler to compile our source code.

X-Windows

If you've set up your computer to allow X-Windows (or you're on a Linux machine already), I recommend using xemacs. You'll need to include the -Y (capital Y) flag when you ssh in to your account. See the end of the Cygwin instructions file for information on how to turn on syntax highlighting (if it's not already on) and fix the indentation style in xemacs. Additionally, all of the commands described for emacs in the "No X-Windows" section below will work the same in xemacs. In particular, "Ctrl-X 1" (Ctrl+X followed by a 1) will change xemacs to fullscreen, instead of the split-screen mode it starts in by default.

No X-Windows

There are a plethora of text editors available on eniac. If you are new to the command line, I recommend using emacs. Emacs (rightfully) has a reputation as a complicated beast, but at its core is fairly easy to use, and also comes with syntax highlighting support for C and C++ (and a wide variety of other languages). For example, to run emacs, enter

emacs filename
where filename is the name of the file you wish to edit. While emacs offers many commands that can be used to make a programmer's life easier, the basic functionality is fairly standard.

You can navigate a file in emacs with the arrow keys or page up/down. To save the current file, press Ctrl-x Ctrl-s (i.e., first press Ctrl-x then press Ctrl-s). Note that the emacs way of writing this combination of keys is "C-x C-s". To exit emacs, press "C-x C-c". Cut is "C-w", Copy is Esc-w (or Alt-w if you have it and it works in your terminal window), written as "M-w" (M is for meta), and paste is "C-y". "C-s" is search, and "M-%" (Esc-Shift-5) is find and replace (don't ask me why). When you open emacs, it will probably be in split-screen mode. Typing "C-x 1" will switch to fullscreen. ("C-x 2" is split-screen horizontal and "C-x 3" is split-screen vertical.) If you want to learn more about the other features of emacs, you can access a built-in tutorial by typing "C-h t" or search online for one of the many emacs tutorials that are available. Here is a pretty good cheat sheet.

Note that if you are not sshing into eniac (because you are in the Linux lab or on a personal computer), then emacs will appear as a window instead of a command line editor, in which case there are clickable menus with all sort of options.

If you're completely overwhelmed by emacs, you can also try using pico, which can be run just by typing pico at the command line. Pico is simpler than emacs, but not nearly as fun!

Putting it all together: compiling hello world in C [80 points]

Now that you can log in to your eniac account and have access to a text editor, let's write and compile the canonical Hello World program to make sure everything is in order. Create a folder hw1 and navigate into that folder. (At the command line, execute the commands mkdir hw1 and then cd hw1.) Now, make a file hello_world.c that uses printf to print "Hello, world!" to the console.

Though the Hello World program is included in the notes for the first lecture, the code is repeated here for your convenience:

/* File:   hello_world.c
   Author: [your name here]
   Desc:   CIS 190, Homework 1 -- Hello World! in C
*/

#include <stdio.h>

int main()
{
   printf("Hello world!\n");
   return 0;
}

Compile your hello_world program by invoking gcc as we discussed at the end of the first lecture:

gcc -o hello_world -Wall hello_world.c
And then run your executable to make sure it does the right thing
gibsonk@plus:~/hw1> gcc -o hello_world -Wall hello_world.c 
gibsonk@plus:~/hw1> ./hello_world 
Hello world!
Congratulations! You now have a working C development environment and can move onto bigger and better things.

Introducing yourself [10 points]

The next part of this assignment is to write a short file (name it answers.txt, and place it inside the hw1 folder) with some information about you. Please answer the following questions:
  1. What CIS courses have you taken before? These include courses at Penn and a previous school (including high school).
  2. Do you have relevant experience outside of school? What kind of experience?
  3. What other programming languages are you familiar with?
  4. Why are you taking this class? What are you hoping to get out of it?
  5. What kind of computer will you be doing most of your work on?
  6. What development platform (e.g. Eclipse, Visual C++, XCode, g++, gcc, ...) do you expect to use? Or, do you not have a preference and will just use whatever we use in class?
  7. Is there anything else that we should know about you?

Other Basics [10 points]

Once you've completed and turned in hello_world.c and answers.txt, take the time to do the following:

  1. Fill out the when2meet poll here with when you would be able to attend office hours. Use your penn username (e.g., gibsonk) as your name, so that we will be able to give you credit. Filling out the poll for office hours is 10% of your Homework 1 grade.
  2. Read the C/C++ Style guide on the course website here.
  3. Sign up for the Piazza page for CIS 190 here.

Submitting Your Homework

Please submit your homework via Canvas. You should submit hello_world.c and answers.txt as a zip or tar file.

If you are unsure how to create a zip or tar of the files that you need to submit, you can follow the instructions below.

delozier@plus:~/homework> ls
hw1
delozier@plus:~/homework> ls hw1
answers.txt  hello_world.c
delozier@plus:~/homework> zip hw1.zip hw1
  adding: hw1/ (stored 0%)
delozier@plus:~/homework> ls
hw1  hw1.zip