Write a function called get_distance that accepts two character vector inputs representing the names of two cities. The function returns the distance between them as an output argument called distance. For example, the call get_distance('Seattle, WA' Variable distance has an incorrect value.
Answers
Answer:
function y = get_distance(city_3_1,city_3_2)
global raw;
[~,~,raw] = xlsread('Cities.xlsx');
city_3 = {};
for i = 1:337
city_3{1,i} = raw{1,i};
end
city_3_4 = {};
for j = 1:337
city_3_4{1,j} = raw{j,1};
end
search_c1 = strcmp(city_3_4,city_3_1);
for ii = 1:337
if (search_c1(1,ii) == 1)
break
else
ii = 1;
end
end
search_c2 = strcmp(city_3,city_3_2);
for jj = 1:337
if (search_c2(1,jj) == 1)
break
else
jj = 1;
end
end
if ((ii == 1) || (jj == 1))
y = -1;
else
y = raw{ii,jj}(1,1);
end
distance = y;
end
Step-by-step explanation:
Let's consider,
We have "Cities.xlsx" file with all the city names and distances. Above function will read data from "Cities.xlsx" file and search for the given cities in file, if city is found then it will calculate distance in y and return in distance veriable.
if cities not found then it will return -1 value as distance variable.