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?
-
Predict the shape of the nitronium ion, NO from its Lewis structure and the VSEPR model. It has one Raman active vibrational mode at 1400 cm-1, two strong IR active modes at 2360 and 540 cm-1, and...
-
For the network of Fig. 4.127, determine: (a) IB. (b) IC. (c) VE. (d) VCE. +18 V 9.1 k 510 k VcE B 130 510 k 7.5 k 18 V
-
Supercritical, uniform flow of water occurs in a 5.0-m-wide, rectangular, horizontal channel. The flow has a depth of \(1.5 \mathrm{~m}\) and a flow rate of \(45.0 \mathrm{~m}^{3} / \mathrm{s}\). The...
-
A well-respected polling agency has conducted a poll for an upcoming Presidential election. The polling agency has taken measures so that its random sample consists of 50,000 people and is...
-
What role does HR play in reinforcing this mission/vision statement? Describe the steps companies like Walt Disney go through to develop a human resource strategy? Discuss current changes in the...
-
Please complete the 2018 federal income tax return for Tim and Linda Jones. Ignore the requirement to attach the form(s) W-2 to the front page of the Form 1040. If required information is missing,...
-
Which statement is the most accurate regarding individual change stages in adoption? During the awareness stage, the target person replaces old habits with new habits. During the interest stage, the...
-
Sodexo Corp. has receivables of $334,227, inventory of $451,000, cash of $73,913, and accounts payables of $469,553. What is the firm's current ratio? Round your final answer to two decimal places.
-
a) Two different strains of cold virus were isolated and put in cultures to grow. Virus A triples every' 8 h while virus B doubles every 4.8 h. If each culture has 1000 viruses to start, which has...
-
1. The undergraduate enrollment at Columbia can be broken down in a number of ways, including by major and by class. The two largest groups in each of those cases are Freshman (which constitute 36%...
-
As the manager of the human resources department at your firm, you are investigating the number of sick days employees took during the last calendar year. For employees in the marketing area, the...
-
A business sets up a sinking fund so they will have a $50,000.00 to pay for a replacement piece of equipment in 10 years when the current equipment will be sold for scrap. If they make deposits at...
-
Write a shell script that finds total no. of users and finds out how many of them are currently logged in.
-
Explain why it is not wise to accept a null hypothesis.
-
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.
-
What are the different methods of conducting marketing research?
-
What is the main difference between a full-service and a limited-service supplier?
-
What are some of the issues that should be considered when conducting marketing research?
Study smarter with the SolutionInn App