Question

The following program implements a key-value data structure in which values are stored based on a...

The following program implements a key-value data structure in which values are stored based on a string keyword. Currently, the program allows new keys and values to be entered, but there is no function to retrieve the values given a search key.

Complete the get_value function to retrieve a value given a search key. The function should print "INVALID KEY" if the key is not present.

Do not remove any code OR add any code to main. You should only add code to the function.

#include 

float get_value(char key[], char keys[],
                char values[], int length) {
    // Your code here
}

int main() {
    char keys[] = {
        "speed",
        "velocity",
        "range"
    };
    float values[] = { 101.8, 22.2, 30.1 };

    // Get the value associated with "speed"
    float value = get_value("speed", keys, values, 3);
    printf("value = %f\n", value);

    // Get the value associated with "change"
    value = get_value("change", keys, values, 3);
    printf("value = %f\n", value);

    return 0;
} 

Need it in 10 minutes, please.

Homework Answers

Answer #1

#include <stdio.h>
#include<string.h>

float get_value(char key[], char keys[], char values[], int length) {
// flag=2 if we find the key in Keys arrays other wise flag=1
int flag=1;
// converting key char array value to string
strcpy(string, key);
// iterating Keys array and comparing with key string
for(int i=0;i<length; i++){
// if we find the key inside kyes array flag value become 2
// and return the value which match with key
if(string==keys[i]){
flag=2;
return values[i];
}
}
//if not mat key with keys arrays values
if(flag==1){
printf("INVALID KEY");
return 0;
}
}

int main() {
char keys[] = {
"speed",
"velocity",
"range"
};
float values[] = { 101.8, 22.2, 30.1 };

// Get the value associated with "speed"
float value = get_value("speed", keys, values, 3);
printf("value = %f\n", value);

// Get the value associated with "change"
value = get_value("change", keys, values, 3);
printf("value = %f\n", value);
return 0;
}
--------------------------------

I have provided the solution as per your requirement, i hope you're satisfied with the way i have approached. please dont hesitate to give me a Like if you like it, i appreciate your like. If you have any queries you can shoot them any time in comments section. I will be glad to help you.
Thank you :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
A program is already given to you.  There are five problems in this skeleton version of the...
A program is already given to you.  There are five problems in this skeleton version of the program, each is 10 points. All you got to do is complete the missing code in each function. What the function does is clearly stated in the name of the function.   // ASSIGNMENT ON FUNCTIONS #include <stdio.h> // Problem 1: Compile with gcc func_assignment.c -Wall // There are some warnings because there is a mismatch between // data type passed and defined. // Find...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
In C language make a structure called funds funds should have variable fields of type int...
In C language make a structure called funds funds should have variable fields of type int id; char bank[10]; float balance; The program would have a function called sum which returns a float. To sum pass structure funds and a pointer called record. initialize record to equal {1, "BOA", 10023.43} call sum passing the pointer record , the function returns a float value into variable result. initialize record to equal {2, "Chase", 4239.87} call sum passing the pointer record ,...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h>...
Convert the following C program to C++. More instructions follow the code. #include <stdio.h> #include <stdlib.h> #define SIZE 5 int main(int argc, char *argv[]) {    int numerator = 25;    int denominator = 10;    int i = 0;    /*    You can assume the files opened correctly and the    correct number of of command-line arguments were    entered.    */    FILE * inPut = fopen(argv[1], "r");    FILE * outPut = fopen(argv[2], "w");    float result =...
convert this code to accept int value instead of float values using python. Make sure to...
convert this code to accept int value instead of float values using python. Make sure to follow the same code. do not change the steps and make sure to point to what code you replaced. make sure to have 2 files Method:----------------------- #define a python user difined method def get_float_val (prompt): is_num = False str_val = input (prompt) #prming read for our while #while is_num == False: (ignore this but it works) old school while not is_num: try: value =...
I need to add two trivial functions to the following code. I am having an issue...
I need to add two trivial functions to the following code. I am having an issue with what i may need to change in the current code to meet the requirements. I am already displaying the bank record but now using a function Here are some examples of what i can add to the code 1. Obtain opening (current) balance. 2. Obtain number the number of deposits. 3. Obtain number of withdrawals. 4. Obtain deposit amounts. 5. Obtain withdrawal amounts....
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether...
C++ PROGRAM SPECIFICATION For the assignment, we will use the previous assignment’s program that determines whether a college class room is in violation of fire law regulations regarding the maximum room capacity and add more logic to that program. We will need to make the following enhancements… The program should now determine the number of classes, and should do so by generating a unique, random number. Replace taking user input for the number of rooms with a computer generated number...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour...
Write a C++ program that converts time of day from a 24-hour notation to a 12-hour notation. For example, it should convert 14:25 to 2:25 PM. (A) The user provides input as two integers separated by ‘:’. The following function prototype should capture the user inputs as described below: void input(int& hours24, int& minutes); //Precondition: input(hours, minutes) is called with //arguments capable of being assigned values. //Postcondition: // user is prompted for time in 24 hour format: // HH:MM, where...