Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a Makefile that builds a C program phonebook.c and produces the executable object file phonebook. Your Makefile should use the compiler flags discussed in

Write a Makefile that builds a C program phonebook.c and produces the executable object
file phonebook. Your Makefile should use the compiler flags discussed in class, and have the
all and clean targets as shown previously. You should also include the flag -Wextra as
discussed in the lab.
Question 2[80 points]
In this question, you will write a program phonebook.c implementing a dictionary data
structure that keeps key-value pairs using an array of structs. Each struct will have two
elements: a key(a name) and a value(the phone number). The key is a string of at
most 63 characters (not including the null-terminator). The value is an unsigned long
int type that corresponds to the key. If the key is an empty string (the first character in the
character array is \0), this means the struct at this array position is not currently used.
You can assume a maximum of 128 key-value pairs will be needed.
(a)(20 points) Write a function called phonebook add that takes 3 arguments:
(1) the array of structs representing the dictionary
(2) the size of the array (i.e.,128)
(3) the struct to add
The function finds an unused position in the array and sets the key-value pair. If the
key-value pair was added successfully, the function should return 0. If no free position
was found, the function returns -1. The return type is int.
(b)(20 points) Write a function called phonebook find that takes 3 arguments:
(1) the array of structs representing the dictionary
(2) the size of the array
(3) a key (a name) to find
If an entry matching the key was found, the function returns the array index at which
the entry is found. If no such entry exists dictionary, the function should return -1.
The return type is also int.
(c)(20 points) Write a function called phonebook remove that takes 3 arguments:
(1) the array of structs representing the dictionary
(2) the size of the array
(3) a key (a name) to remove
2
If the key is found, the key is set to an empty string and the function returns 0. If the
key is not found, the function returns -1. The return type is int. Try not to have a
loop in this function and instead use the previous function(s).
(d)(10 points) Write a function called phonebook print that takes 2 arguments:
(1) the array of structs representing the dictionary
(2) the size of the array
The function then prints all the valid (used) key-value pairs in the dictionary, each on
a separate line. The output should be similar to the following:
"Jon" ->11234567890
"Kyle" ->13220000001
Note it is not required that you format phone numbers, nor that you check they are of
a valid length
(e)(10 points) Write a main() function that initializes the phonebook then runs a loop
which accepts from stdin a line containing a single character that corresponds to the
desired operation, followed by more lines containing the arguments for the operation.
The operations are as follows:
a for add; e.g.,
a
Jon
11234567890
This should add a key-value pair using the given key and value.
f for find; e.g.,
f
Jon
In this example, the output should be the value corresponding to Jon if the key
exists in the dictionary, or Key not found if the key is not in the dictionary.
r for remove; e.g.,
r
Jon
3
The output should be success or fail, depending on whether the key was found
and removed.
p for print all; e.g.,
p
The output should be all key-value pairs using the function in part (d).
q to quit the program.
You will need to use the following functions to compare and copy strings:
strncmp
strncpy
You can use man to find more information on these two functions. The syntax char *string
is equivalent to char string[].
Question 3[10 points]
In this part, you will test your phonebook program and document the process. You will
construct test cases comprised of input, expected output, and a short description of what
the test case is supposed to test and why. In a text file phonebook-test.txt, document
your test cases, the actual output that you received from your program, and (if needed) the
debugging and modifications that you needed to make to the source code to fix the bug.
When constructing test cases, think about edge cases; e.g. a key longer than 63 characters,
more than 128 items, etc. You may construct as many test cases as you would like, but at
least 4 per function are required to receive full marks.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M. Ricardo, Susan D. Urban, Karen C. Davis

4th Edition

1284231585, 978-1284231588

More Books

Students also viewed these Databases questions