By the time you have completed this lab, you should be able to
Choose the first partner wisely (ask us if you don't know the goal). If your assigned partner is more than 5 minutes late, ask the TA to pair you with someone else for this week.
This lab's first pilot should log in, create ~/cs16/lab06/ and make that your current directory.
Start ch, and simplify the ch prompt if you want:
-bash-4.2$ ch Ch Professional edition, version 6.1.0.13751 (C) Copyright 2001-2009 SoftIntegration, Inc. http://www.softintegration.com /cs/class/cs16/lab06> _prompt = "ch> " ch> ch>
Create an array, a, that can store 10 int values:
ch> int a[10];
Now accomplish each of the following steps in order:
ch> int i; ch> for (i=1; i<10; i++)\The next line you type should be indented, because it is "inside" the loop structure. This line must be an assignment statement that sets the value of the current array element, a[i], to twice the value of the preceding element, a[i-1]. Before going on to part e, verify that 29 is now stored in a[9].
ch> CENSORED: SETS a[i] ch> a[9] 512
0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512
By the way, see what ch displays when you just type the array name, a, by itself (your result probably will be a different value, but that's not what you should think about):
ch> a 0x9e3bef8
Surprised? The result is a memory address (in hexadecimal). It is the address of the first byte in the array. Keep this concept in mind, and learn more about it in chapter 6: an array name points to a location in memory.
Do NOT exit ch (to keep the array from Step 2 in memory), but open emacs or another editor
to create a file named isum.chf (a ch "function file"). Type a comment
such as /* returns sum of the first n values in x */
and define the function
as specified here:
Write a function named isum that takes two arguments - an int array named x, followed by an int named n - and returns an int value that is the sum of the first n elements in x.Here is what the function's header should be:
int isum(int x[], int n)
Save the file as isum.chf
in your lab06 directory.
Then test it from the ch prompt. If something doesn't work correctly, fix the
problem before going to Step 4. Here are some correct answers to expect:
ch> isum(a, 10) 1023 ch> isum(a, 4) 15 ch> int b[] = {17, -4, 11, 9}; ch> isum(b, 4) 33
The first two examples sum all and then just the first four elements of the array from Step 2. The third example demonstrates another way to create an array - a way that automatically sizes it and initializes its elements. The size is determined by the number of values listed between the curly braces, and the elements are initialized with these values in the order they are listed.
First: switch roles between pilot and navigator if you did not already do that.
Create a file named avg.chf with your editor (still no need to exit ch). Type an appropriate comment and then function avg as follows:
Write a function named avg that takes two arguments - an int array named x, followed by an int named n - and returns a double value equal to the average (arithmetic mean) of the first n elements in x.This function can and should be very short, because you should use function isum to calculate the average, as in:
double result = (double)isum(x, n) / n;Save the file as
avg.chf
in your lab06 directory.
Then test it from the ch prompt. If something doesn't work correctly, fix the
problem before going to Step 5. Here are some correct answers to expect:
ch> avg(b, 4) 8.2500 ch> avg(a, 10) 102.3000
You can exit ch now. Make a copy of this skeleton program in your (first pilot's) ~/cs16/lab06 directory. You can cp a copy from the instructor's account:
-bash-4.2$ cp ~kyledewey/cs16/lab06/diffs.c ~/cs16/lab06/
Study the parts of the program that are complete: (1) after including the standard input-output library, there are prototypes for the two functions you wrote in Steps 3 and 4; (2) a symbolic constant is defined to set a maximum number of input values, because the values will be stored in an array whose size must be specified; (3) main begins by creating the array, a, and four other variables to be used later; (4) the user is prompted to enter data, and these data are read into the array - at the end of this for loop, the value of n is equal to the number of data values entered and stored in the array; and (5) between the two uppercase instructions to you, the values of n and average are printed along with a message that differences will print next.
First, just to be safe, make a new directory named safe, inside your lab06 directory, and copy your two function files into it:
-bash-4.2$ mkdir safe -bash-4.2$ cp *.chf safe/ -bash-4.2$ ls safe avg.chf isum.chf
Stay in the lab06 directory, and accomplish the following steps in this order:
-bash-4.2$ cat isum.chf avg.chfThe command's purpose is to concatenate the files listed as arguments and print the result to standard output. So now type that command again, but use the double output redirection arrow, >>, to redirect the output to the end of diff.c:
-bash-4.2$ cat isum.chf avg.chf >> diffs.cNow start your editor to edit diffs.c, and verify that your two functions are appended to the end of it - just once. You might have to add newlines between parts of the file to clean it up. And don't be afraid to start over completely with a new copy of diffs.c if you made mistakes that are difficult to undo, and remember you saved copies of the chf files in directory safe too.
Save the file, and exit the editor. You can use ch to test your program, but also make an executable version of it:
-bash-4.2$ make diffs cc diffs.c -o diffs -bash-4.2$ ./diffs enter up to 100 integers - end by non-integer: 15 -23 4 38 done average of 4 values: 8.5 differences from average: 6.5 -31.5 -4.5 29.5 -bash-4.2$
Before proceeding to Step 6, be sure your version works exactly like the instructor's solution. You can run that solution yourself by typing the following at the system prompt:
~kyledewey/cs16/lab06/diffs
Don't leave early though ... see challenge problems below.
Submit the lab with the turnin program. You MUST have both your name and your partner's name in the file in order to receive credit. Remember that the original pilot needs to do this step, since that is whose account you have been using in Cooper Lab.
Bring up a terminal window and cd into the original pilot's cs16 directory, and cd again into the lab06 directory. Then type the following to turn in just the completed file from Step 5 above:
turnin lab06@cs16 diffs.cRespond "yes" when the program asks if you want to turn in (be sure to read the list of files you are turning in), and then wait for the message indicating success.
Optional Extra Challenge
- Revise the output from diffs.c to print the differences much more neatly, and along with the original values. Start by printing a row of titles: "Value" and "Difference" and then print the values themselves in a first column and the differences in the second column. Line both columns up neatly (and right justified) beneath the titles, as in:
Value Difference 5 -2.50 10 2.50- Enhance diffs.c to record both the maximum positive and maximum negative differences, and print these values neatly with labels at the end.
- Enhance diffs.c again, this time to count the number of values above average, the number of values below average and the number of values that equal the average, and print all of these counts neatly with labels at the end.
- If you still have some time before lab ends, then why not add a new function to calculate the standard deviation of the first n values in an array? The easiest formula for a sample's standard deviation, s, is:
where the x with a bar on top is the average value in the array x. You can incorporate this function into a new version of diffs.c named something like zscores.c, to print z-scores instead of just differences from average. The z-score of an array value is equal to that value's difference from the average, divided by the standard deviation.
Practice Lab: Two-dimensional arrays
Most quarters, the next lab would be about two-dimensional arrays. This quarter, because of the holiday on May 28, the lab about Pointers will be next. We still think you should practice using two-dimensional arrays though, and so you should work on the practice lab when you have time to do so. Here is the practice lab link.
Adapted by Kyle Dewey from a lab prepared by Michael Costanzo.