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
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*...
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 =...
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...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
write a program that automates the process of generating the final student report for DC faculty...
write a program that automates the process of generating the final student report for DC faculty considering the following restrictions. Consider declaring three arrays for processing the student data: studID studName studGrade The student ID is a random number generated by the 5-digit system in the range of (10000 - 99999). Create a function to assign the student ID generated in an array of numbers. Consider the following to generate the random number: Add the libraries: #include <stdlib.h> #include <ctime>...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
The following program creates a linked list which contains 5 links. Add a method called doubleValue()...
The following program creates a linked list which contains 5 links. Add a method called doubleValue() to the LinkedList class. The doubleValue() method must double the value of the number data field in each link. Add the required code to the main() method to call the doubleValue() method and display the revised list. public class Link { private int number; private Link next;      public Link(int x) { number = x; }    public void displayLink() { System.out.println("The number is:...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT