CISC105 Summer 2007 Lab08
Notes before you begin:
- You will write one program for each of the following problems. You may start each program using a previous program as a base if it seems suitable.
- Be sure to save each program as its own .c file. Name them lab08.n.c, where "n" is the number in the program list below. For example, the name of the file for the first program will be lab08.1.c
- Style counts. This includes header comments, comments on any tricky lines of code, good variable naming, good spacing and indentation. These are worth points to you.
- When scripting your lab, you will compile and run each one. For each .c file, you will first cat the file, then compile it, then run a.out and fully demonstrate the capability of the program. If your program does not compile, do not attempt to run it. Since it doesn't compile, no new a.out file is created--thus if you try to run it, you'll just be running the previous file which did compile.
- Please Remember that the lab is due electronically by Sunday at 11:55p.m. A 10% penalty is assessed for each day that the lab is late, and this penalty depends upon the electronic submission time (from myCourses).
Programs
- Write a program containing a recursive function called exp(). This function should take an integer base and another integer exponent and return the value of baseexponenet.
To calculate this recursively: remember that bx is really the same thing as saying b * bx-1. So if you know the value of bx-1, you can easily get the value of bx -- just multiply it by b! How to get this value? Use your exp() function recursively.
So, if you want to calculate 57, you can just multiply 5 * exp(5, 7-1) to get that value.
Remember, for a recursive function, you need a base case. When the exponent is zero, anything raised to that exponent will have the value 1. So if you receive an exponent of 0 in the function, there's no need to call exp() again--you can just return the value of the base^exponent, which is 1.
-
Write a program that demonstrates the idea of pass-by-reference. Remember that when using pass-by-reference, instead of (for example) a function expecting an int as a parameter, the function should expect the address of an integer--so an integer pointer, the data type of which is int *.
All you need to do for this problem is write a function that accepts such a value and changes the number at the address received. Then in main(), you should show the value before the function call and afterwards, in order to show that it does actually change.
In the function, you can change the actual value by first dereferencing the integer pointer (int *) and then changing what's there. If you don't remember how to do this, see the first section of chapter 6 in your book.
- Make a copy of lab07.6.c. Add a menu that allows you to print the current student or enter a new one.
Use an integer to scan in the user's input. In the switch statement for your menu, make sure you deal with integer values that are not menu options--bad input. Also, make sure the menu continues until the user enters a sentinel value of -1 to quit.
- Make a copy of your lab08.3.c. Here, instead of scanning in an integer, you are instead going to use fgets to read in an 80 character string from the user. The user doesn't have to know this: you should still tell them just to type in a number. When you have done this, use the atoi() function to convert the string to an integer, and use that integer for your menu. Look up atoi() and fgets() in your book if you are unsure how to use them. In comments, answer the following: Why would we want to do it this way? What is the benefit?
Submission
You should have a total of 4 programs, named lab08.1.c to lab08.4.c. Make a single script file (named lab08.txt) where you cat, compile, and run each program in sequence. That is, you should cat, compile and run lab08.1.c, then do the same with lab08.2.c, etc.
Submit all program and script files on MyCourses before 11:55 Sunday night. Give the paper version of your script to your TA next Monday at the beginning of lab. Write your name and the lab number on the front of your paper submission. Also, if something doesn't work properly, please circle it in bright ink and make a note for your TA explaining the problem. You will lose less points for a problem you recognize and can explain (at least a little) than for one you don't seem to notice.