CISC105 Summer 2007 Lab05
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 lab05.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 lab05.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 to demostrate that when we pass an integer or double into a function from main(), changing the parameter inside the function does not change the value in main. Make sure your program adequately demonstrates this idea.
- Write a program to demonstrate that when we pass an entire array into a function from main(), changes we make to the array elements will indeed change the values in main().
Write a function that computes and prints the value of a number raised to an exponent. It should take two integer parameters and use a loop to raise the number to the correct power. Make sure to test thoroughly so that it works for all cases (including an exponent of 0, for example). You may assume that the exponent will always be greater than or equal to zero. Call the function with user input from main().
- Write a function that computes the value of a number raised to an exponent, just as in problem 1. Instead of printing it, however, the function should return the computed value. Remember that you need to specify the return type in both the prototype and the function definition, and that the value returned must match this type. The returned value should then be printed in main(). Call this function once again on user input.
- Declare an array of 10 integers in main(), and fill it either with user input or with a static initialization. Write a function that takes two parameters, an array of integers and an integer size of the array. The function should then calculate the sum of these numbers in a loop, and then return the calculated value. Call it from main with user input, and then print the returned value.
- Make a copy of your lab05.5.c. Add a function that calculates the average of the numbers in the array. In this function, instead of using a loop to calculate the sum, call the function you wrote in the previous problem and use the value it returns. Return the calculated value and then print it out in main().
- Make a copy of your lab05.4.c, which contained a function that will calculate the value of a number raised to an exponent. Add a new function that will calculate the sum of three integers passed as parameters and return the calculated value. Once you have these two functions, calculate the value of
23 + 52 + 100
Using only function calls, and print out this value. All function calls, as well as the printf, should happen on one line. Remember that functions, just like any other expression, are "evaluated" first, and the values they return may be used just like any other number. You can print the returned values, assign the value to a variable, or even use that value as a parameter for another function.
Submission
You should have a total of 7 programs, named lab05.1.c to lab05.7.c. Make a single script file (named lab05.txt) where you cat, compile, and run each program in sequence. That is, you should cat, compile and run lab05.1.c, then do the same with lab05.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.