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.
#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 :)
Get Answers For Free
Most questions answered within 1 hours.