MAT-07A Conditional Statements HW

Before beginning your homework, refer to the ENGR_1187_Homework_Formatting_and_Tips.pdf document on Carmen.

Your homework must be submitted in the format outlined in the document referenced above to the Carmen dropbox before the next class.

E-mail your instructor with any questions you have.

Homework Problems (MATLAB Book):

·  Chapter: 6

·  Problems: [12 (A) Only]

Special Instructions

Problem 12 Part A:

Solve Problem 12 Part A using the given flowchart. Follow the flowchart as a programming strategy.

A good practice when debugging a script file is to test each portion of the code separately. Instead of creating separate files to test code, one of the nice things in MATLAB is the ability to highlight a block of code, right click, and select comment. The commented code will be ignored on subsequent runs. So one can debug problem A, comment it, and work on problem B. When problem B is working, the code for problem A can be highlighted again, only this time select uncomment. One final run can be done to create a copy of the command window to turn in.

Additional Homework Problems:

Problem A:

Write a program that determines the time of day as a fraction of a day, and then if it is in the morning (i.e. the fraction of a day is less than 0.5), print the message

It is now morning

Note that the built-in function now will return the current time as an integer day and fractional part of day. There are several ways to get the fractional part of a number, s. One way is to use the remainder function, i.e. fraction=rem(s,1). Another is to find the integer part and subtract it, i.e. fraction = s – floor(s).

Problem B:

Write a program that creates a 16 element array of integers between -20 and 20 using the command randi([-20 20],1,16). Have the program complete the following tasks

a)  If there are more positive elements than negative elements, print the following message

There are XX positive elements

If there are more negative elements than positive elements, print the following message

There are XX negative elements

If the number of positive and negative elements is the same print

There are both XX positive elements and XX negative elements

where XX is the actual number elements

b)  Print the sum of all the positive elements using fprintf

Don’t forget to add comment statements to your script file to describe what you’re having the computer do in each portion.