CS 16, Winter 2018

Lab06:
Arrays


Goals for this lab

By the time you have completed this lab, you should be able to

Step by Step Instructions

Step 1: Get together with your lab partner, and create a lab06 directory

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.

Step 2: Practice using an array from the ch prompt

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:

  1. Unlike standard C++, ch has filled the array with zeros. Go ahead and verify that both the first and last element do equal zero, by typing a[0] and then a[9] on successive lines.
  2. Another feature of ch that is not like standard C++ is that a warning will be issued if you try to access an element outside the bounds of the array. Try it now, by typing a[10] and then a[-1], and read the different messages.
  3. Now set the value of the first array element, a[0], to 1. And then verify its new value by typing a[0] again at the prompt.
  4. Use a for loop to set the other nine array elements to successive powers of 2. That is, a[0] already stores 20, and so we want to store 21 in a[1], 22 in a[2], and so on up to a[9]. Declare the loop control variable, i, and type the for loop header exactly as shown here - including the '\' continuation character at the end:
    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
  5. Now print a table of these powers of 2, by looping through the array and printing the index and value of each array element on a separate line. Of course you should use a for loop to accomplish this task. Match this output display:
    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.

Step 3: Write and test a function to sum elements of an array

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.

Step 4: Write and test a function to find an average value from an array

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

Step 5: Complete a program to find differences from average in user-entered data

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:

  1. Append your two functions from Steps 3 and 4 to the end of diffs.cpp. You can use an editor to copy/paste, but you can also use a combination of the Linux cat command and output redirection to get the job done. First observe what happens when you type the following command at the system prompt:
    -bash-4.3$ cat isum.chf avg.chf
    The 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.cpp
    Now 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.
  2. Type your name(s) in the comment at the top.
  3. In the appropriate place, use your avg function to set the value of the variable named average.
  4. In the appropriate place, write a loop to calculate and print the differences between each of the n values and the average value. Match the results shown below.

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.

Step 6: Submit your completed program

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.cpp
Otherwise 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.


Evaluation and Grading

Each student must accomplish the following to earn full credit [50 total points] for this lab:

If you finish before the end of your lab period, work on the optional challenges below.


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.

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.