CS010 Practice 3

Pointers and Arrays

Unix tips 

Command Histories

Here are some more useful Unix tips. If you want to repeat a command that you did earlier, you can use C-p or the uparrow key to retrieve the previous command. You can repeat these to go further back in your command history. If you go too far, you can use C-n or the down arrow key to go forward in the command history.

If the command that you retrieve is not exactly what you want, you can edit it before hitting carriage return. Many of the basic Emacs editing commands can be used, such as C-b (back one character), C-f (forward one character), C-a (beginning of line), and C-e (end of line).

Core Files

If you run a C program and it crashes, it leaves behind a core file. A core file is useful if you wrote the program that crashed and you want to debug the program in a debugger. Otherwise, core files are very big and very useless. If you get a segmentation fault or a bus error, there will probably be a core file laying around. The core file is named with the name of your executable file appended with .core. So, if your program named charcount crashes it will leave behind a file called charcount.core.

C Mode in Emacs

When you edit C files in Emacs, Emacs automatically enters C mode. It knows that you are editing a C file when you visit a file with .c for its suffix. As a result, if you create a buffer and start typing a C program, it won't go into C mode until you save the buffer to a file with a .c suffix. C mode provides you with help in formatting your C programs. For example, if you use C-j to end a line instead of the return key, Emacs will create a new line and indent it to the appropriate level. When you type a }, it will find the matching { and briefly highlight it for you. This will help you make sure that your brackets are balanced.

You will also notice that Emacs uses colors to help you tell at a glance what different pieces of the program are. Comments will appear in one color, keywords in another, identifiers in a third etc. Having different colors for comments and for code is very helpful because it will help you find situations where you have accidentally commented out code or failed to close a comment. Other than that, well, it looks cool.

There are also two additional "minor modes" that you can turn on and off as you prefer. (By default they are off.) The first is auto-newline insertion mode. When this is on, Emacs will automatically insert newlines when you type certain characters, like {. It's pretty handy. The second mode is hungry-delete. When this mode is on, the backspace character will delete all the consecutive whitespace characters preceding the cursor, not just the immediately preceding one.

Here are some useful commands:

C-j

Insert a newline and indent the next line.

C-c C-q

Fix indentation of current function

C-c C-a

Toggle the auto-newline-insertion mode. (If it was off, it will now be on and vice versa.)

C-c C-d

Toggle the hungry delete mode

Also note that there is an Emacs info page about C mode so you can learn more.

Exercises 

  1. Write a program to compute the prime numbers between 1 and 1000. A good algorithm to compute the prime numbers with is the Sieve of Eratosthenes. First create an array that can hold 1000 booleans. (Be sure to define the size of the array using #DEFINE so that it is easy to change. You'll be asked to change it in later questions.)

    Initially, set all the elements of the array to TRUE. On completion of the algorithm, if an array's value is TRUE, it indicates that the array's index is a prime number. For this algorithm, the 0th entry of the array is essentially not used. For example, when you are done the 1st, 2nd, 3rd, 5th, and 7th entries should be true since 1, 2, 3, 5, and 7 are all prime numbers. 4, 6, 8, and 9 are not prime numbers and so the values indexed by those numbers should be FALSE.

    Begin with the number 2. Since 2 is a prime number, leave the array indexed by 2 to be false. Every multiple of 2 is not prime, so go through the array and set every entry indexed by a multiple of 2 (greater than 2) to FALSE.

    When you're done with 2, repeat the process using multiples of 3.

    When you get to 4, you should check and discover the array value indexed by 4 is already FALSE. Thus, 4 is not prime and all multiples of 4 will also already be marked as not prime.

    Continue until you have eliminated all non-prime numbers. Think about what the last value is you need to check. If you want to use standard math functions, include the file math.h, just like stdlib.h, etc. The function names are sin, cos, exp, log, pow, sqrt, and so on. They all take doubles arguments. As in most languages, passing an integer value to these functions implicitely "promotes" the value to a double. To compile a program using these functions, provide "-lm" as another command line argument to gcc.

    When done, make one more pass through the array, printing out all the prime numbers you have found.

  2. Rewrite this program, but modify the function that actually updates the array so that it uses pointers to walk the array setting values to FALSE instead of using subscripts to walk the array.
  3. Now, modify each version of the program by increasing the size by a factor of 10. Repeat this several times.
    1. Proponents of pointer arithmetic claim that pointer arithmetic is faster than subscripting. For this application is one approach faster than the other?
    2. What appears to be the slowest aspect of either of these algorithms?
  4. Repeatedly increase the size by a factor of 10. After each increase, compile and run each program again. What eventually happens?
  5. The file /home/faculty/freund/shared/cs010/httpd-access.log contains access log information for the CS web server for the last few days. Use grep, wc, "|", and ">", to (1) create a file that contains all references to web pages having cs010 in their name, and (2) find out how many accesses to cs010 web pages appear in the log.


Return to CS 010 Home Page