Final 2
Final 2
Final Exam
Term: Fall 2018 (Sep4-Dec4)
Student ID Information
Grade Table
Course ID: CSCI 2132 Question Score
4 /6
Time Period: Start: 12:00
End: 15:00 5 /7
8 /8
Number of Exam Pages: 21 pages
(including this cover sheet) 9 /10
Additional
11 /10
c) (2 points) In the C programming language, the following two pairs of scanf format
strings are equivalent:
"%c,%c"
"%c ,%c"
d) (2 points) In the C programming language, the following two pairs of scanf format
strings are equivalent:
"%d,%d"
"%d ,%d"
e) (2 points) If there is a text file named log.txt in the current working directory, the
command date >> log.txt will append the output of the date command to the end of
log.txt.
f) (2 points) In a shell script, whenever we used the command exit to terminate the
script, we are always required to provide an exit code explicitly as the argument of the
exit command.
CSCI 2132, 9 Dec 2017, Final Exam Page 3 of 21
A. 10
B. -5
C. 14
D. 19
d) (3 points) A file names contains people names. Each line contains a first name and a
last name of a person (e.g. John Smith), and the file is not sorted. Our task is to list
only first names, sorted alphabetically, and no name should be repeated in more than
one line.
Which one of the following four command will complete this task correctly?
A. cut -d " " -f 1 < names | uniq | sort
B. cat names | cut -d " " -f 1 | sort | uniq
C. sort | uniq | cut -d " " -f 1 names
D. sort < names | uniq | cut -d " " -f 1
CSCI 2132, 9 Dec 2017, Final Exam Page 4 of 21
#include <stdio.h>
#include <string.h>
int main() {
char s[90] = "test";
char *p = "program";
return 0;
}
CSCI 2132, 9 Dec 2017, Final Exam Page 7 of 21
6. (9 points) Single command line. For each of the following questions, write a single
Unix command line to perform the task required.
a) (3 points) Print a list of files in the directory /usr/bin whose names end with the
English word “make”. The file named make is considered one of these files.
b) (3 points) Print out the number of six-character words in the Linux dictionary file
/usr/share/dict/linux.words starting with a or c and ending with h.
Recall that this dictionary file contains one English word per line.
c) (3 points) Print out a list of regular files in the directory /usr/bin whose file owner
has read permission but does not have execute permission.
CSCI 2132, 9 Dec 2017, Final Exam Page 8 of 21
b) (4 points) Suppose that you are working in a directory that is a working copy of
a directory in SVN. You created a new file, named file.c in your directory. Which
command lines are required to save this file in the SVN repository?
CSCI 2132, 9 Dec 2017, Final Exam Page 9 of 21
return s2;
}
CSCI 2132, 9 Dec 2017, Final Exam Page 10 of 21
b) (6 points) Write a makefile to compile the program into the executable named llist
when we run the command ‘make’ or ‘make all’. The make should do separate compi-
lation of files main.c and llist.c.
CSCI 2132, 9 Dec 2017, Final Exam Page 11 of 21
b) (5 points) Briefly describe the mergesort algorithm, its advantages and disadvantages,
and discuss running time. If you want to use code, use pseudo code.
CSCI 2132, 9 Dec 2017, Final Exam Page 12 of 21
b) (6 points) Briefly explain the lines (1), (2) and (3) in the shell script below:
if [ ! -d $1 ]; then
echo Error (1)
exit 1
fi
struct node {
char prod[50]; /* prod is product name */
double price; /* price is product price */
struct node *next; /* next is pointer to next node */
};
(a) (4 marks) Write a C function printprod that takes a pointer to the above node
structure and prints a line with the following: the product name, a comma (,), the
product price, another comma, and a newline character to the standard output. An
output example is: office desk, 356.78,
The function prototype must be: void printprod(struct node *n);
CSCI 2132, 9 Dec 2017, Final Exam Page 16 of 21
(b) (10 marks) Write the C function readprod which allocates a node structure, and
fills it with a product name, price, and sets member next to NULL. The product name
and price are read from the standard input. The input format is as follows:
orange juice, 5.95, ....
so, the product name consists of some characters that are not comma, followed by a
comma, followed by product price, followed by comma, and some other optional charac-
ters in a line. You can assume that product name is not longer than 49, and the function
should also read all characters to the end of line but not store them anywhere. You do
not need to do much error checking except that if you cannot read input in required for-
mat the function should return NULL. The function returns a pointer to the structure
created like this. The function prototype is: struct node* readprod();
CSCI 2132, 9 Dec 2017, Final Exam Page 17 of 21
(c) (10 marks) Write the C function insert which inserts a node into a linked list. You
can assume that list has products sorted by price from high to low, and after inserting
the node, the list should remain sorted. The first argument head is the pointer to the
first element of the list, or NULL if the list is empty. The second argument newnode is
the new node to be inserted. The function returns the new head of the list. The function
prototype is: struct node* insert(struct node *head, struct node *newnode);
CSCI 2132, 9 Dec 2017, Final Exam Page 18 of 21
(d) (8 marks) Write a C program that reads a sequence of products and prints them out
sorted by their price from high to low. The program must read the the products using
the function readprod defined in (b), insert them into a linked list using the function
insert defined in (c). The products must be printed using the function printprod
defined in (a). Write a complete program with includes and the function main, but you
can assume that the functions printprod, readprod, and insert are already defined
and their prototypes included, and struct node already defined.
CSCI 2132, 9 Dec 2017, Final Exam Page 19 of 21