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.3$ ch Ch (64-bit) Professional edition, version 7.5.3.15401 Copyright (C) SoftIntegration, Inc. 2001-2016 http://www.softintegration.com /cs/class/cs16/lab06> _prompt = "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: a statement that 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 7: 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 find the sum and then use that result to calculate the average (do remember to "cast" either the isum result or n to double before calculating).
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. Download and save a copy of this skeleton program in your (first pilot's) ~/cs16/lab06 directory.
Study the parts of the program that are complete: (1) after include and using directives, there are prototypes for the two functions you wrote in Steps 3 and 4; (2) main defines a constant that sets a maximum number of input values, because the values will be stored in an array whose size must be specified; (3) then main creates 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.3$ mkdir safe -bash-4.3$ cp *.chf safe/ -bash-4.3$ ls safe avg.chf isum.chf
Stay in the lab06 directory, and accomplish the following steps in this order:
-bash-4.3$ 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.cpp:
-bash-4.3$ cat isum.chf avg.chf >> diffs.cppNow start your editor to edit diffs.cpp, 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.cpp if you made mistakes that are difficult to undo, and remember you saved copies of the chf files in directory safe too.
Make an executable version of the program. Then test it:
-bash-4.3$ make diffs g++ diffs.cpp -o diffs -bash-4.3$ ./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.3$
Fix anything that causes your results to differ from the example results. Then test it again once or twice with different inputs to be sure it works properly before submitting.
If you are still logged on, and your current directory is still the same as your two programs, then the quickest way to submit is by entering the following command (suggest you copy/paste):
~submit/submit -p 960 diffs.cppOtherwise you can use the submit.cs interface at https://submit.cs.ucsb.edu/ from your web browser - submit diffs.cpp for Lab06. A perfect score is 50/50 points.
If you are working with a partner, be sure that both partners' names are in a comment at the top of the source code file, and be sure to properly form a group for this project in the submit.cs system.
Each student must accomplish the following to earn full credit [50 total points] for this lab:
The deadline for submitting your work for credit is tonight by 11:59 pm.
Optional Extra Challenge
Work with a copy of diffs.cpp to accomplish these challenges. Do not change the version that you submitted, and do not submit again with these changes incorporated.
- Revise the output from diffs.cpp 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.cpp to record both the maximum positive and maximum negative differences, and print these values neatly with labels at the end.
- Enhance diffs.cpp 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.cpp named something like zscores.cpp, 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
Past quarters included a lab about two-dimensional arrays. Although no lab is devoted to that topic this quarter, we still think you should be skilled at using two-dimensional arrays. Please work on this practice lab on your own to gain those skills. Thanks ... and we hope you have fun with it!
Prepared by Michael Costanzo.