USE ORDERED ARRAYS/CELLS FOR THE NAMES, WEIGHTS AND DENSITIES THEN COMPARE THE INPUT (NAME) WITH ALL THE NAMES, IF A MATCH IS FOUND, LOOK UP THE CORRESPONDING VALUES FOR WEIGHT AND DENSITY
'
strcmpi - compares 2 strings (not case sensitive) and returns true (1) if the two strings are equal, false (0) otherwise
Consider the matlab program below:
'
'
____________________________________
clc,clear
%%
% define the variables in [ordered] arrays/cells
names = {'katie','abby'}; % names
w = [120 140]; % weight
d = [2.2,3.4]; % density
%%
% get name from input
name = input('Enter name: ','s');
%%
% search for the name entered and if found, look up the weight and
density
for i=1:length(names) % check all names
if(strcmpi(names(i),name)) % compare the name with all names
% if found, do something with the weight and density
weight = w(i)
density = d(i)
volume = weight/density
end
end
------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,
IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~
Get Answers For Free
Most questions answered within 1 hours.