Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Matlab help: Function Name trickOrTreat Inputs 1. (struct) 1XN Vector of structures representing houses 2. (cell) Mx2 cell array of candy preferences 3. (double) Number

Matlab help:

Function Name trickOrTreat

Inputs

1. (struct) 1XN Vector of structures representing houses

2. (cell) Mx2 cell array of candy preferences

3. (double) Number of houses you can visit

Outputs

1. (cell) Px2 cell array of candy you have at the end of the night

2. (cell) Order of houses which you visited

Background

After you have your sp00ky costume and completed your sp00ky CS 1371 homework,

it's time for the most important part of Halloween: candy! Your neighborhood has some great

trick or treating, but you can't go to every house because you need to wake up early to go to CS

1371 lecture! Naturally, you turn to MATLAB to help you plan out your Halloween night.

Function Description

You are given a vector of structures where each structure represents a single house.

The structure will have a 'Name' field, containing the name of the family in the house, a

'Count' field, containing the number of candy you can get from that house, and a 'Candy'

field, containing a cell array of the types of candy that house has. You are also provided a cell

array with the names of candies you like in the first column and the corresponding numerical

rating value for each candy in the second column. Determine the total score of every house by

multiplying the number of candy you can get by the rating of your favorite candy you can get

from that house.

You then need to determine the order in which you will visit the houses. You will always

start at the middle house, your own, and get candy from your own house. Store the name of the

candy that you got from each house into the first column of a cell array, and the amount of

candy you got into the second column.

Then, you will either go to the house on the left or on the right, choosing the one with the

higher score. Store how much candy you got at that house by either adding the name and count

to the bottom of the cell array if you have not already gotten that type of candy, or by adding the

number of that type candy you just got to the number of that type of candy you already have.

After that house, you will go to either the closest house on the left of the house you are

at that you have not already visited, or the closest house on the right in the vector that you have

not already visited. If you are all the way to the right or left, then you keep going in the other

direction. If they have the same score, go to the house in the direction of your own house (in the

middle). Repeat the process of storing the candy you got for each house. Output a cell array of

the names of the houses you visited in the order you visited them and the candies cell array.

Homework 10 - Structures

Example

houses =

Name 'Htay' 'Ho' 'My house' 'Schmitt' 'Profili'

Count 6 6 9 2 2

Candy {'Twix',

'Skittles',

'Kit Kat'}

{'Mochi',

'Twix',

'Skittles'}

{'Mochi',

'Kit Kat'}

{'Kit Kat',

'Pocky',

'Mochi'}

{'Mochi',

'Skittles'}

candy =

'Twix' 4

'Pocky' 1

'Skittles' 3

'Mochi' 5

'Kit Kat' 2

[bag,path] = trickOrTreat(houses,candy,4)

bag = {'Mochi',17;'Twix',6}

path = {'My House','Ho','Htay','Schmitt'}

First, calculate the scores of all the houses, which are [6 * 4, 6 * 5, 9 * 5, 2 * 5, 2 *

5] = [24,30,45,10,10] . We then start at My house , since that is in the middle. We add 9

Mochi to our bag. We then compare the Ho house to the Schmitt house, since they are to the

left and right, and realize the Ho house has a higher score. We then move to the Ho house and

add 6 Mochi to our bag. We then compare the Htay house to the Schmitt house and realize the

Htay house has a higher score. We then move to the Htay house and add 6 Twix to our bag.

Since there are no houses to the left, we move to the Schmitt house on the right, and then add 2

Mochi. That was the fourth house, so we stop there.

Notes

Every candy has a unique rating value.

There will always be an odd number of houses.

At each house, you only have to consider at most two houses for the next house you

visit.

You are guaranteed to have a rating for every candy you can find.

The number of houses you can visit includes visiting your own.

The names of the houses will be unique.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft Visual Basic 2005 For Windows Mobile Web Office And Database Applications Comprehensive

Authors: Gary B. Shelly, Thomas J. Cashman, Corinne Hoisington

1st Edition

0619254823, 978-0619254827

More Books

Students also viewed these Databases questions

Question

1. Write down two or three of your greatest strengths.

Answered: 1 week ago