Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this homework we will make use of structures and all of the functions that we can use on structures as well as any function

In this homework we will make use of structures and all of the functions that we can use on structures as well as any function we can use on a general array.

Given what we know about structures, we will write a script file that will ask the user if they wish to create 3 shapes: circle, right triangle, and square. Each of these shapes will be a structure. Each of those structures will be nested WITHIN a super-structure: shape. The hierarchy looks like this:

Shape Structure o Field: Circle Structure

Field: Radius Field: Center (2-element vector) Field: Area

o Field: Right Triangle Structure Field: Side Lengths (2-element vector) (you cannot arbitrarily prescribe all 3 side

lengths) Field: Center (2-element vector) Field: Area

o Field: Square Structure Field: Side Length

Field: Center (2-element vector) Field: Area

Requirements:

  • Create a script file named: shapes.m

  • Using comments, put the following information at the top of the file:

Include the clear and clc commands

Part 1: Creating Shapes

The script will ask the user, in succession, if they would like to create a circle, right triangle, or square. If the user responds with a y (upper or lower case), create the structure they desire within the shape structure and assign random values to all attributes EXCEPT the area. You must calculate the area yourself. You must use a text variable for the user input.

Use randi() to assign random values to the attributes. You can specify your own maximum value for the randi() function. Your fields for center and area must be named the same across ALL the shapes. You must have a single shape structure containing the nested sub-structures.

Part 2: How Many Shapes?

Check to see if the user has created any shapes. If they have not created any shapes, inform them that they have no shapes, and therefore cannot continue on with the calculations and end the program. If they only created one shape, inform them that they have only created one shape, and tell them which shape they created and end the program.

The shape structure should only contain shapes the user has selected to create. If they have created more than one shape, move on to Part 3.

Part 3: Distance Calculation

Now that we know the user has created at least 2 shapes, print out the list of shapes to the user. (Hint: print out the list of fieldnames).

Ask the user to choose a shape, and be sure to inform them they must type it exactly as you have displayed it. Store this user input as a text variable. Check to see if the user input matches any of the fieldnames of the shape structure. If it does not match any of the fieldnames of the shape structure, inform the user they must type in their choice again.

Repeat this process again for the second shape (Note: user can enter the same shape twice, thats okay!)

Once you have the two shape names stored as strings, use dynamic field names to calculate the distance between their centers. For a shape with center [X1 Y1] and another shape with center [X2 Y2], the distance between the centers is:

222 = (21) +(21)

Print out the distance with the names of the shapes. Format with 3 digits after the decimal.

Part 4: Ratio of Areas

Follow the same process as in Part 3 to ask the user for two valid shapes.

Once you have the two valid shapes, calculate the ratio of the area of shape 1 to shape 2 using dynamic field names. For example: if the user chooses the square and the circle, calculate the ratio of the areas of the square to the circle.

Print out the ratio with the names of the shapes. Format with 3 digits after the decimal.

Sample Output:

This is a sample of what the program should show in the command window. If the user enters any information, it will be highlighted in RED:

Would you like to create a circle? (Y/N): y Would you like to make a triangle? (Y/N): y Would you like to make a square? (Y/N): y You created these shapes:

circle triangle square Choose two shapes to calculate the distance between centers. You must spell them EXACTLY as they were written

First shape: circle Second shape: Triangle No shape by that name! Second shape: triangle The distance between the circle and the triangle is 9.434 Choose two shapes to calculate the ratio of the areas. You must spell them EXACTLY as they were written

First shape: circle Second shape: circle The ratio of the areas (circle:circle) is 1.000 ----Running it another time

Would you like to Would you like to Would you like to No shapes created ----Running it another time Would you like to create a circle? (Y/N): n

Would you like to make a triangle? (Y/N): n

Would you like to make a square? (Y/N): y

You only created a single shape: square ----Running it another time

Would you like to create a circle? (Y/N): y Would you like to make a triangle? (Y/N): n Would you like to make a square? (Y/N): y You created these shapes:

circle square

Choose two shapes to calculate the distance between centers. You must spell them EXACTLY as they were written First shape: circle Second shape: square The distance between the circle and the square is 2.236 Choose two shapes to calculate the ratio of the areas. You must spell them EXACTLY as they were written First shape: square Second shape: circle The ratio of the areas (square:circle) is 0.063

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

Money And Banking

Authors: Robert E. Wright, Vincenzo Quadrini

1st Edition

0982043082, 9780982043080

More Books

Students also viewed these Accounting questions

Question

Distinguish between intrinsic and extrinsic teleology.

Answered: 1 week ago

Question

Identify HRM systems, practices, and policies.

Answered: 1 week ago