Lab 3
Passing arrays to functions; Using a debugger; Recursive functions

Objectives

Note: don't worry if you can't finish the entire lab exercise. Use turnin (see step 8) to turn in as much as you've completed before you leave the lab. Make sure you finish the rest of the lab on your own time.

What you should do...

  1. Sign the attendance sheet.

  2. Write a program which asks the user to enter an integer in the range
     0 < n <= 20
    The program then reads in n integers and saves them in an array. You may assume that the number of elements in the array will not exceed 20. Name your program lab3.c.

  3. Compile and run your program. Your program doesn't produce any output, so it's hard to tell if it works correctly! Use a debugger to convince yourself that the program works. The program gdb (gnu debugger) lets you run your program in a controlled way, one command at a time, so you can see exactly what's going on. Here are some useful gdb commands:
            help                    help
            list                    display ten more lines
            list  <line number>     display ten lines centered around 
                                    <line number>
            list -                  display ten lines previous to the lines
                                    just printed 
            print <variable name>   print the value of a variable
            break <function name>   set a breakpoint at the start of a function
            break <line number>     set a breakpoint at the specified line
            cont                    continue execution after a breakpoint
            clear <function name>   remove all breakpoints from function
            run                     run program with optional arguments
            step                    resume execution for just a single statement
            quit                    exit the debugger
            backtrace               shows layers of functions you are into
    
    A breakpoint is just what the name implies - a point at which you break out of the currently-executing program and are brought into the debugger. You can then check out the state of various variables to make sure they contain the values they should contain at that point. If you reach a breakpoint and you find something amiss, you know that there is a bug in your program in one of the instructions that executed prior to the breakpoint. If, on the other hand, everything looks OK, you can narrow down your search for bugs in the instructions that execute after the breakpoint. Loops are usually good places to put breakpoints; start by placing a breakpoint immediately before a loop and immediately after. If it looks like there may be a problem inside a loop, placing a breakpoint within the loop allows you to stop after each iteration and make sure the loop control variables and other variables contain the correct values.

  4. When you want to use the debugger, you must first tell the compiler to produce debugging information. So recompile your program with the command gcc -Wall -g lab3.c. Start the debugger by typing gdb a.out. You'll see a welcoming message followed by the debugger's prompt:
    (gdb)
    
  5. Use the following commands to set breakpoints and check the values of your variables: list (to get the line numbers), break <linenumber> (to set a breakpoint), run (to start the program), print <variable name> (to check the current value of a variable), cont (to run up to the next breakpoint).

  6. Once you're sure your program runs correctly, add a function that returns the largest number stored in the array. Be sure to pass the array as a parameter to the function. You should also pass the current size of the array as a parameter. Compile your program. Use gdb to verify that your program works (demonstrate your program running under gdb for the TA). If you are running out of time, go to step 8. If you have at least 15 minutes left, proceed to step 7.

  7. Now write a second version of the same function, but this time write the function recursively. What is the "size" of this problem? How can you reduce the size with each successive recursive call? What would the base case be for this function, and what would it return? Compile your program. Use gdb to verify that your program works. How efficient is the recursive version of the function as compared to the iterative version?

  8. Turn in your file using the Unix turnin program. The Unix command you should use to submit your file is
    /cs/bin/turnin submit cs2301 lab3 lab3.c 
    

  9. Refer to this lab page as you work on the remaining assignments this term. The program you examined with the debugger today was fairly simple. You may be thinking that you could more easily debug this program just by looking at it. As your programs become more complicated and harder to debug, you will appreciate the value of a tool like gdb. If you plan to take additional courses in Computer Science, debugging is a skill you must have. Type man gdb at the Unix prompt if you would like more information about gdb.

See you next week!