Question 1 [10 points] Write a Makefile that builds a C program map.c and produces the...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Question 1 [10 points] Write a Makefile that builds a C program map.c and produces the executable object file map. Your Makefile should use the compiler flags discussed in class, and have the 'all' and 'clean' targets as shown previously. Question 2 [90 points] In this question, you will write a program map.c implementing a 'dictionary' data structure that keeps key-value pairs using an array of structs. Each struct will have two elements: a 'key' and a 'value'. The 'key' is a string of at most 63 characters (not including the null- terminator). The 'value' is a double type value 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 map_add that takes 4 arguments: (1) the array of structs representing the dictionary, (2) the size of the array (i.e., 128), (3) a key to add, and (4) the value corresponding to the added key. 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 map_find that takes 3 arguments: (1) the array of structs representing the dictionary, (2) the size of the array (i.e., 128), and (3) a key to check. If a dictionary entry matching the key was found, the function returns the array index at which the entry is found. If no such entry exists in the dictionary, the function should return -1. The return type is also int. (c) (20 points) Write a function called map_remove that takes 3 arguments: (1) the array of structs representing the dictionary, (2) the size of the array (i.e., 128), and (3) a key to remove. 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 map-print that takes 2 arguments: (1) the array of structs representing the dictionary, and (2) the size of the array (i.e., 128). 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: key1123.456789 a_key -> 322.000000 my_key > 2.000000 another_key -> 98.625000 abc -> 4.500000 (e) (20 points) Write a main () function that 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 my_key 2 This should add a key-value pair using the given key and value. 'f' for find; e.g., f my_key In this example, the output should be the value corresponding to "my_key" 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 my_key The output should be "success" or "fail", depending on whether the key was found and removed. 'p' for print all; e.g., 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: strcmp strcpy You can use man to find more information on these two functions. The syntax char *string is equivalent to char string[]. You may assume that keys do not contain any spaces. Also, keep in mind that if you choose to use scanf() to get a string from stdin, you do not need to use the '&' operator. The reason for this will be revealed in the "Pointers" topic. Question 1 [10 points] Write a Makefile that builds a C program map.c and produces the executable object file map. Your Makefile should use the compiler flags discussed in class, and have the 'all' and 'clean' targets as shown previously. Question 2 [90 points] In this question, you will write a program map.c implementing a 'dictionary' data structure that keeps key-value pairs using an array of structs. Each struct will have two elements: a 'key' and a 'value'. The 'key' is a string of at most 63 characters (not including the null- terminator). The 'value' is a double type value 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 map_add that takes 4 arguments: (1) the array of structs representing the dictionary, (2) the size of the array (i.e., 128), (3) a key to add, and (4) the value corresponding to the added key. 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 map_find that takes 3 arguments: (1) the array of structs representing the dictionary, (2) the size of the array (i.e., 128), and (3) a key to check. If a dictionary entry matching the key was found, the function returns the array index at which the entry is found. If no such entry exists in the dictionary, the function should return -1. The return type is also int. (c) (20 points) Write a function called map_remove that takes 3 arguments: (1) the array of structs representing the dictionary, (2) the size of the array (i.e., 128), and (3) a key to remove. 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 map-print that takes 2 arguments: (1) the array of structs representing the dictionary, and (2) the size of the array (i.e., 128). 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: key1123.456789 a_key -> 322.000000 my_key > 2.000000 another_key -> 98.625000 abc -> 4.500000 (e) (20 points) Write a main () function that 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 my_key 2 This should add a key-value pair using the given key and value. 'f' for find; e.g., f my_key In this example, the output should be the value corresponding to "my_key" 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 my_key The output should be "success" or "fail", depending on whether the key was found and removed. 'p' for print all; e.g., 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: strcmp strcpy You can use man to find more information on these two functions. The syntax char *string is equivalent to char string[]. You may assume that keys do not contain any spaces. Also, keep in mind that if you choose to use scanf() to get a string from stdin, you do not need to use the '&' operator. The reason for this will be revealed in the "Pointers" topic.
Expert Answer:
Answer rating: 100% (QA)
a Write a function called mapadd that takes 4 arguments 1 the array of structs representing the dictionary 2 the size of the array 3 a key to add and 4 the value corresponding to the added key The fun... View the full answer
Related Book For
Posted Date:
Students also viewed these programming questions
-
What street used to be located where the Gibbs - Green plaza is location?
-
This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of an array of objects as a class data...
-
Managing Scope Changes Case Study Scope changes on a project can occur regardless of how well the project is planned or executed. Scope changes can be the result of something that was omitted during...
-
How many moles of carbon dioxide, CO2, are in a 22 gram sample of the compound?
-
Conduct additional research on the use of triclosan and other chemical agents in antimicrobial products today. Develop an opinion on whether this process should continue, providing evidence to...
-
For a one-way ANOVA, a. Identify one purpose of one-way ANOVA tables. b. Construct a generic one-way ANOVA table.
-
What is the purpose of a debit-credit memo- randum?
-
Franklin Paper Company manufactures newsprint. The product is manufactured in two departments, Papermaking and Converting. Pulp is first placed into a vessel at the beginning of papermaking...
-
1%20Assignment%20Unit%202.pdf Q E X ages y! Yahoo _ Zoominfo C Bitterwater-Tully .... ( Bayshore Elementar... Prog 1 /2 - 80% + 1. The fox population in a certain region has an annual growth rate of...
-
Given a database of the results of an election, find the number of seats won by each party. There are some rules to going about this: There are many constituencies in a state and many candidates who...
-
Provide feedback to this post? Ethics reflects the value beliefs about what is good and what is bad. Ethics is the moral principles that govern a person's behavior of the conducting of an activity....
-
Discuss the ethical dimensions of a UK-based Manufacturer of vacuum cleaners outsourcing their manufacturing to a developing country contractor who is alleged to have repeatedly used illegal child...
-
Estimating the stock value. (10 marks) Using the Discounted dividend model (DDM) estimate the intrinsic value of the firms' stocks using the following steps: Estimate the dividend growth rate using...
-
discuss briefly on board governance as it relates to ethics and corporate culture.
-
What do you think the advantages and disadvantages of a business plan are? Name and describe 5 public resources you could use to find business plan resources (you can use your book)? What Business(s)...
-
Discuss the economic pressures and incentives for professional sports teams to relocate. Are professional sports teams an important tool for a citys economic development? Explain why or why not.
-
Solve the problem. A restaurant serves 8 ounces of soup to each customer. How many quarts of soup should be prepared in order to serve 196 customers?
-
After looking at the resources, explain what a spirit image is. Why might looking at a god and/or a human in terms of their spirit be helpful if you want to eliminate some of the divisions between...
-
Do Programming Exercise 5 but use a two-dimensional array to store input for 3 years of monthly sales. Report the total sales for each individual year and for the combined years.
-
Suppose we have the following structure declaration: struct applicant { char name[30]; int credit_ratings[3]; }; a. Write a function that takes an applicant structure as an argument and displays its...
-
Write a statement that displays the value of the second element in the float array ideas.
-
Use Figure 12.262 to determine if the given sequence of vertices is a Hamilton path, an Euler trail, both, or neither. 1. Graph \(A, a ightarrow b ightarrow e ightarrow d ightarrow c\) 2. Graph \(F,...
-
Suppose that you lost the combination to a combination lock that consisted of three digits, and each was selected from the digits \(0,1,2,3,4,5,6,7,8\), or 9 . You desperately need to open the lock...
-
Use Graph \(L\) to find a Hamilton path between each pair of vertices or indicate that there isn't one. 1. \(p\) to \(r\) 2. \(m\) to \(p\) 3. o oto \(q\) Graph L S
Study smarter with the SolutionInn App