Computer Science 324
Computer Architecture

Mount Holyoke College
Fall 2007


Lab 1: Bit Manipulations in C
Due: 11:59 PM, Monday, September 17, 2007


Unix and C practice

If you are not familiar with the CSLab Linux systems, spend some time getting familiar with those systems. We will be using them during the semester. There is nothing to turn in for this part of the lab. I understand that this is the first experience with Unix for many of you, so I expect a lot of questions.

  1. Go through the Unix tutorial handout. This will teach you how to log in and out of the machines, use basic Unix commands, and edit files with Emacs.
  2. Log into a Linux system in Clapp 202. You should be able to log in with the same username and password you may have used for Windows in that lab in the past.
  3. Identify the function of and experiment with these Unix Commands:
     
    ls         cd         cp         mv         rm         mkdir      pwd
    man        chmod      cat        more       grep       head       tail
    ln         find       rmdir      wc         diff       tar
    
    Identify the function of and experiment with these Emacs Commands:
     
    C-x C-s    C-x C-c    C-x C-f    C-x C-w    C-g        C-a        C-e        
    C-d        C-_        C-v        M-v        C-s        C-r        M-%
    
    Learn these commands - you will use them often. Hints can be found in the Unix and Emacs web pages on the course website.
  4. Make a directory in your account for CS 324 work (perhaps "324" or "cs324" might be reasonable). Use the chmod command to restrict access to this directory so only you can read the files.
  5. Write a "Hello, world!" program in C and run it on a Linux system. The C compiler "gcc" should be available.
C Programming Assignments

The main part of the lab is to write two C programs. Approach these problems as best you can as a computer scientist. If you're having trouble, ask questions! Make sure that your code is well written and documented.

  1. Write a C program prime.c that reads an integer from the input or command line (your choice, do both for a point of extra credit) and prints out whether the number itself is prime, then prints the primes that are closest to it (next smaller and next larger). You might use this in a data structures course if you're looking to size a hash table. Note that there no primes less than 2. To earn full credit, you should not check for unnecessary factors and you should avoid making expensive calls to the math library (such as sqrt(3)).
  2. A certain security system includes a keypad where access codes are entered as 5 digit numbers. On this particular system, 1 and 2 are on the same button, as are 3 and 4, etc. 9 and 0 are on the same button as well.

    Write a C program access.c that reads in a five digit access code as a decimal number that writes out each of the five digit access codes that are equivalent (there are 32). Make sure that you print any leading zeros. Again, your program may prompt for the input value or accept it on the command line. You may assume that the value on the input is an integer.

Submit your programs via electronic mail.

Your programs will be graded based on correctness, documentation, structure, and performance.

Just For Fun

Just for fun and more practice, try this one:

Write a C program to find the three four-digit numbers that have this special property: when you add the first two digits to the last two digits and square the sum, you get the original number back.