COMP 411: Lab 3: Arrays and Functions in C

Due 11:59 PM, Friday, February 2, 2018


Reading

Prior to beginning this tutorial, carefully review the following material from the Perry and Miller textbook: Chapters 21-23 (arrays) and 30-32 (functions).


Exercise 1

Compile and run the program analyse.c. Modify the program to replace the loop containing repeated getchar() calls with a single call to fgets(). Refer to P&M (pp. 235-236) for details on the fgets() function, but here is a quick tip on its usage:

fgets(text, MAX, stdin);

fgets stands for "get string from file", and it stores the string read into text, reading up to a maximum number of MAX characters, from the file specified in the third argument, which in our case is the "standard input" or stdin (meaning keyboard, or a file if input is redirected using "<").

Once you make this change, however, you will no longer have the length of the string in length, so you will have to use the C library function strlen() (see P&M pp. 176-177 for details) to compute the length of the string.

Note that the string read using fgets has a newline character at the end, and strlen includes this newline character in the length of the string, which is not what we want. So, adjust the string length accordingly.

Note that while there are several others ways to accomplish this exercise, you must do exactly as explained above, in particular: (i) use fgets() to read the line of input; and (ii) use strlen() to compute the length of the input line. Call your program ex1.c. Compile it and run it on a few inputs of your choice. Also, run it on the sample inputs provided and check that the output is correct.


Exercise 2

Make a copy of the program of Exercise 1, and name it ex2.c. Modify the program so that it outputs the line entered by the user in the reversed order of characters. Then, if the line is a palindrome, i.e., it reads the same forward and reverse, then your program should also indicate that.

Specifically, your program should display, "Your input in reverse is:" (with newline), and the reversed input on the next line. And if the line were a palindrome, it should also print, "Found a palindrome!" (with newline).

NOTE: For this exercise, we will use a very strict definition of palindrome: A string is a palindrome if and only if it reads exactly the same backward as forward, including spaces and punctuation. Thus, for this exercise, "madam" is a palindrome, but "Madam" is not. Also, "nurses, run" is not a palindrome, but "nursesrun" is. We will use this highly restrictive definition for now just to make coding easier.

Compile and run the program on a few inputs of your choice. Also, run it on the sample inputs provided and check that the output is correct (i.e., identical to the output provided, no extra white space anywhere).


Exercise 3

The following program computes the mean, standard deviation and correlation of two sets of data held in a 2-dimensional array: correlation.c. Read through the program and understand how it calculates these values using array accesses. If you are unfamiliar with correlation, you can learn about the math behind it here.

Rewrite the program to read 5 pairs of floats from input instead of being defined in the program. The program should first print the string "Enter 5 pairs of floats (one per line):\n". Then it should read the 5 pairs of floats, one pair per line, with the values in a pair separated by a space. The first value on each line belongs to the 'X' dataset and the second value belongs to the 'Y' dataset. You can also assume the user will not try to input invalid values.

Specifically, use a for loop to read the floats from stdin, not a long series of printf() / scanf() calls. You do not need to modify anything else in the program.

Name the file ex3.c. Compile and run your program on inputs of your choice. Make sure it runs correctly on the sample inputs provided. The numbers in the input are assumed to be in decimal floating-point format.

To compile, you will need to slightly modify the compile command in order to include the basic C math library:

% gcc -o ex3 ex3.c -lm

The flag "-lm" tells the compiler to include the library of math functions.

Reminder: As explained in Lab 2, when reading integers separated by whitespace, the preferred method is not to put a space within the scanf format string: i.e., scanf("%f%f", ...) is preferable to scanf("%f %f", ...). This is because scanf() automatically skips over whitespace when reading numbers, so the first version will work whether there is one or more spaces, tabs, or even newlines between the numbers. The latter version, however, will expect a space between the numbers, not a tab or newline, and is thus unnecessarily over-specified.


Exercise 4

Write a program to read in a series of text strings. When you get an empty line, the program should return the count of how many times each of the letters (A...Z) and (a...z) appear in the text and then exit, as illustrated below. For this assignment, you may ignore any characters that are not letters, such as numbers or whitespace. You should also count capital and lower-case letters together; for instance, the string "Aaa" should increment the count of 'A' by 3.

Note that char's here are ASCII characters, which are also encoded as numbers from 0..255. Take a quick look at the table of ASCII values here. For instance, 'A' is value 65 and 'Z' is 90; it is valid to write code like the following:

char buf[MAX_BUF];
if (buf[0] >= 'A' && buf[0] <= 'Z') /* ... */

in order to figure out if a character in the char array is a capital letter or not. Also note that upper and lower case ASCII values all differ by the fixed amount of 32.

We provided you with some starter code in ex4.c. You may assume that no input line will be larger than MAX_BUF. As with exercise 1, note that fgets still returns one character for the newline, so take this into account when terminating the do loop in the example code.

Some example outputs follow:

%  ./ex4
aaaaaaa

Distribution of letters in corpus:
A: 7
B: 0
C: 0
D: 0
E: 0
F: 0
G: 0
H: 0
I: 0
J: 0
K: 0
L: 0
M: 0
N: 0
O: 0
P: 0
Q: 0
R: 0
S: 0
T: 0
U: 0
V: 0
W: 0
X: 0
Y: 0
Z: 0
%  ./ex4
asdf;lkj
sdkeiflan
lsweinvk

Distribution of letters in corpus:
A: 2
B: 0
C: 0
D: 2
E: 2
F: 2
G: 0
H: 0
I: 2
J: 1
K: 3
L: 3
M: 0
N: 2
O: 0
P: 0
Q: 0
R: 0
S: 3
T: 0
U: 0
V: 1
W: 1
X: 0
Y: 0
Z: 0
%  ./ex4
the quick brown fox jumped over the lazy dog

Distribution of letters in corpus:
A: 1
B: 1
C: 1
D: 2
E: 4
F: 1
G: 1
H: 2
I: 1
J: 1
K: 1
L: 1
M: 1
N: 1
O: 4
P: 1
Q: 1
R: 2
S: 0
T: 2
U: 2
V: 1
W: 1
X: 1
Y: 1
Z: 1

Exercise 5

Have a look at the program bubble.c. Do you understand how it works? Compile and run the program.

The number of swaps is not a very accurate measure of performance, as much time is inevitably spent comparing elements that do not require swapping. Modify the program to count the number of iterations of the do...while loop and display this number together with the number of swaps. Refer to the sample inputs and outputs provided on exactly how to format your I/O.

Name the file with your program ex5.c. Compile and run your program on inputs of your choice, and also make sure it runs correctly on the sample inputs provided.


Exercise 6

Write a function to compute n choose k, i.e., the value of the binomial coefficient nCk. Call your function NchooseK(). (For more on binominal coefficients, see Wikipedia article.)

Specifically, we will implement this function using the recursive definition:

        NchooseK(n, 0) = 1
        NchooseK(n, n) = 1
        NchooseK(n, k) = NchooseK(n-1, k-1) + NchooseK(n-1, k)

You can assume that k will never be greater than n.

Note: There is another, more common, definition of NchooseK() that is not recursive, and uses factorials. You are not supposed to implement that definition. Implement the recursive definition provided above, which only uses additions.

Now write the main() function of your program so it does the following, repeatedly: (i) reads two integers from the terminal, n and k, separated by space; (ii) calls Nchoosek() to compute the binomial coefficient; and (iii) displays the result. The program should do so repeatedly until the user enters "0 0", in which case it prints the result ("1") and terminates.

Refer to the sample inputs and outputs provided on exactly how to format your I/O. The numbers in the input are assumed to be in decimal format. Name the file with your program ex6.c. Compile and run your program on inputs of your choice, and also make sure it runs correctly on the sample inputs provided.


Testing Inputs

Reminder: Before beginning work, it is generally a good idea to run the following command so your terminal environment is set up properly:

% /home/porter/comp411/bin/comp411start

Sample inputs and corresponding outputs are provided under /home/porter/comp411/samples/lab3. You should try running your program on the sample input files provided, and make sure the program's output is identical to that in the sample output files.

The sequence of steps to run your program and verify its output is as follows. First, copy all the sample input and output files to your lab3 folder:

 % cd ~/comp411lab/lab3
 % cp /home/porter/comp411/samples/lab3/* .

Then do the following to compile and run your programs:

 % gcc ex1.c -o ex1 
 % ./ex1 < ex1in1 > ex1result1 
 % diff -qs ex1result1 ex1out1 

... and so on. If diff reports the files are different, carefully inspect them to identify the differences. You can run diff with some of the other options, as explained in Lab 2, to highlight the differences, and also use pico or hexdump -c to inspect the files.

Before submitting your work, be sure that each of your compiled programs runs correctly on all of the sample inputs provided exactly. You may receive zero credit if your program's output does not exactly match the sample outputs provided.


Due Date and Submission Procedure

Your assignment must be submitted electronically by 11:59pm on Friday, February 2.

You will submit your work on Exercises 1-6. Specifically, the files ex1.c, ex2.c, ex3.c, ex4.c, ex5.c and ex6.c will be collected.

How to submit: Navigate to the folder where these files are stored (~/comp411lab/lab3), and run the following command for running a self-checking script (which does not submit the files):

 % cd ~/comp411lab/lab3
 % /home/porter/comp411/bin/selfchecklab3

If the self-check did not report any errors, you can submit your work as follows:

 % cd ~/comp411lab/lab3
 % /home/porter/comp411/bin/submitlab3

When you invoke the submit script, it submits your files, and once again performs a quick check on your source files (including compiling, running and comparing the output of your program with the expected output, for all sample input/output files), and provides a quick summary of any errors. Your work is submitted regardless of whether errors were found or not, but you are free to fix any errors and submit again before the due date.

In case of any problems, please contact the instructor or the TAs.


Revised: 25 September 2005, f.mokhtarian@surrey.ac.uk
Revised: 13 September 2017, Montek Singh, montek@cs.unc.edu
Revised: 24 January 2018, Don Porter, porter@cs.unc.edu

Last updated: 2018-02-19 19:04:27 -0500 [validate xhtml]