Answered step by step
Verified Expert Solution
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 points
In this question, you will write a program phonebook.c implementing a dictionary data
structure that keeps keyvalue pairs using an array of structs. Each struct will have two
elements: a keya name and a valuethe phone number The key is a string of at
most characters not including the nullterminator 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 this means the struct at this array position is not currently used.
You can assume a maximum of keyvalue pairs will be needed.
a points Write a function called phonebook add that takes arguments:
the array of structs representing the dictionary
the size of the array ie
the struct to add
The function finds an unused position in the array and sets the keyvalue pair. If the
keyvalue pair was added successfully, the function should return If no free position
was found, the function returns The return type is int.
b points Write a function called phonebook find that takes arguments:
the array of structs representing the dictionary
the size of the array
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
The return type is also int.
c points Write a function called phonebook remove that takes arguments:
the array of structs representing the dictionary
the size of the array
a key a name to remove
If the key is found, the key is set to an empty string and the function returns If the
key is not found, the function returns The return type is int. Try not to have a
loop in this function and instead use the previous functions
d points Write a function called phonebook print that takes arguments:
the array of structs representing the dictionary
the size of the array
The function then prints all the valid used keyvalue pairs in the dictionary, each on
a separate line. The output should be similar to the following:
"Jon"
"Kyle"
Note it is not required that you format phone numbers, nor that you check they are of
a valid length
e 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; eg
a
Jon
This should add a keyvalue pair using the given key and value.
f for find; eg
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; eg
r
Jon
The output should be success or fail depending on whether the key was found
and removed.
p for print all; eg
p
The output should be all keyvalue 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 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 phonebooktest.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; eg a key longer than characters,
more than items, etc. You may construct as many test cases as you would like, but at
least per function are required to receive full marks.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started