Computer Science 2500
Computer Organization

Rensselaer Polytechnic Institute
Spring 2009


Lab 1: Bit Manipulations in C
Due: 11:59 PM, Monday, January 26, 2009


Unix and C practice

If you are not familiar with the CSLab Unix 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. Log into a CSLab system.
  2. 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.
  3. Make a directory in your account for CS 2500 work (perhaps "2500" or "cs2500" might be reasonable). Use the chmod command to restrict access to this directory so only you can read the files.
  4. Write a "Hello, world!" program in C and run it on a CSLab 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 find an appropriate size for a hash table. Note that there no primes less than 2. To earn full credit, you should maximize efficiency by not checking for unnecessary factors and by avoiding the use of expensive calls to the math library (such as sqrt(3)). When you are finished, you should submit your program on a CSLab Unix system by issuing the command
    submit comporg lab1prime prime.c
    
    at your shell prompt, from the directory that contains the file prime.c.
  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. Submission for this part of the assignment is to be done by the command:
    submit comporg lab1access access.c
    

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.