Q:

Create a vector of structures experiments that stores information on subjects used in an experiment. Each struct has four fields: num, name, weights, and height

0

 Create a vector of structures experiments that stores information on subjects used in an experiment. Each struct has four fields: num, name, weights, and height. The field num is an integer, name is a string, weights is a vector with two values (both of which are double values), and height is a struct with fields feet and inches (both of which are integers). The following is an example of what the format might look like.

Write a function printhts that will receive a vector in this format and will print the name and height of each subject in inches (1 foot = 12 inches). This function calls another function howhigh that receives a height struct and returns the total height in inches. This function could also be called separately. 

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Ch8Ex26.m

% Create an "experiments" vector of structures 

% variable, and pass it to a function that will

% print the height of each subject

experiments(2) = struct('num', 11, 'name', 'Sally', ...

 'weights', [111.45, 111.11], 'height',...

 struct('feet',7, 'inches',2));

experiments(1) = struct('num',33,'name', 'Joe', ...

 'weights', [200.34 202.45], 'height', ...

 struct('feet', 5, 'inches', 6));

prinths(experiments)

prinths.m

function prinths(exps)

% Prints height of every subject

% Format of call: prinths(experiments vector)

% Does not return any values

for i = 1: length(exps)

 high = howhigh(exps(i).height);

 fprintf('%s is %d inches tall\n', ...

 exps(i).name, high)

end

end

howhigh.m

function ht = howhigh(expstruct)

% Calculates height in inches of a subject

% Format of call: howhigh(experiment struct)

% Returns height in inches

ht = expstruct.feet*12+expstruct.inches;

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now