Create a constructor function called SetTimeConstruct to construct a structure called…
- User
Create a constructor function called SetTimeConstruct to construct a structure called TimeHrMin with two fields: hours to hold the hours, and mins to hold the minutes. The function has integer inputs hoursVal and minutesVal. The constructor function should check if and . If not, the value should be set to 0.
Ex:time1 = SetTimeConstruct( 23, 31) time1 = struct with fields:
hours: 23 mins: 31time2 = SetTimeConstruct( 25, 31) time2 = struct with fields:
hours: 0 mins: 31time3 = SetTimeConstruct( 25, -1) time3 = struct with fields:
hours: 0 mins: 0Use this code:
function TimeHrMin = SetTimeConstruct( hoursVal, minutesVal)
% Your code goes here %end
- Assistant
function TimeHrMin = SetTimeConstruct(hoursVal, minutesVal)
if hoursVal < 0 || hoursVal > 23 hoursVal = 0; end if minutesVal < 0 || minutesVal > 59 minutesVal = 0; end TimeHrMin.hours = hoursVal; TimeHrMin.mins = minutesVal;end
- User
Write a MatLab function called ConvertToDecadesAndYears to convert totalYears to decades and years. The decades and years should be returned using a struct that has two fields, decades for the number of decades, and years for the number of years. Restrictions: ConvertToDecadesAndYears must use direct field indexing.
Ex:TimeDecadesYears = []; TimeDecadesYears = ConvertToDecadesAndYears(24) TimeDecadesYears = struct with fields: decades: 2 years: 4
Use this code:
function tempVal = ConvertToDecadesAndYears( totalYears)
% your solution goes here %
end
- Assistant
function TimeDecadesYears = ConvertToDecadesAndYears(totalYears)
TimeDecadesYears.decades = floor(totalYears / 10); TimeDecadesYears.years = mod(totalYears, 10);end
- User
ConvertToDecadesAndYears(1:14) should return a struct with decades: [0 0 0 0 0 0 0 0 0 1 1 1 1 1] years: [1 2 3 4 5 6 7 8 9 0 1 2 3 4]
- Assistant
The function ConvertToDecadesAndYears() is designed to convert a single value for totalYears into decades and years, so it won't work with a vector input like 1:14. However, we can modify the function to handle vector inputs by using a loop.
Here's the modified code:
function TimeDecadesYears = ConvertToDecadesAndYears(totalYears) TimeDecadesYears = struct('decades', [], 'years', []); for i = 1:length(totalYears) TimeDecadesYears.decades(i) = floor(totalYears(i) / 10); TimeDecadesYears.years(i) = mod(totalYears(i), 10); end endNow you can use the function as ConvertToDecadesAndYears(1:14) and it will return a struct with the desired values.
- User
Create a function called CreateArrayOfStructure to create an array of structs called pizzasInStore with 3 fields: name, a string scalar with the name of the pizza, ingredients, a string scalar with the ingredients, and calories, a numeric variable with the total calories. The first item in pizzasInStore is associated with the "Barbecue" pizza, then "Carbonara" pizza, and finally "Ham and Cheese" pizza. In a "Barbecue" pizza, the ingredients are "Beef, chicken, bacon, barbecue sauce" and the calories equal an integer, which is an input parameter of the function CreateArrayOfStructure called numCalofBBQ. In a "Carbonara" pizza, the ingredients are "Mushrooms, onion, creamy sauce" and the calories equal 5 calories less that the "Barbecue" pizza. In a "Ham and Cheese" pizza, the ingredients are "Ham, cheese, bacon" and the calories equal the "Barbecue" pizza. Ex:
- Assistant
function pizzasInStore = CreateArrayOfStructure(numberCalofBBQ)
pizzasInStore(1).name = "Barbecue"; pizzasInStore(1).ingredients = "Beef, chicken, bacon, barbecue sauce"; pizzasInStore(1).calories = numberCalofBBQ; pizzasInStore(2).name = "Carbonara"; pizzasInStore(2).ingredients = "Mushrooms, onion, creamy sauce"; pizzasInStore(2).calories = numberCalofBBQ - 5; pizzasInStore(3).name = "Ham and Cheese"; pizzasInStore(3).ingredients = "Ham, cheese, bacon"; pizzasInStore(3).calories = numberCalofBBQ;end
% test code numberCalofBBQ = 30; pizzasInStore = CreateArrayOfStructure(numberCalofBBQ); disp(pizzasInStore.name); disp(pizzasInStore.ingredients); disp(pizzasInStore.calories);