Loops n for loop n Nested for loop

  • Slides: 26
Download presentation
Loops n for loop n Nested for loop

Loops n for loop n Nested for loop

Example: A tomato trader used to go to the whole sale market every day.

Example: A tomato trader used to go to the whole sale market every day. If the price of the tomato box is less than SR 5, he would buy 200 boxes; otherwise he would buys 100 boxes. Write a MATLAB program that accepts the tomato’s prices for 30 days and then computes the following: n q q The number of boxes that the trader would buy during the 30 days. The total price of the tomatoes.

Example:

Example:

Example:

Example:

Example n Write a MATLAB function to create a special nxn matrix that has

Example n Write a MATLAB function to create a special nxn matrix that has ones in the first row and first column, and whose remaining elements are the sum of two elements, the element above and the element to the left, if the sum is less than 20. Otherwise, the element is the maximum of those two elements.

Example:

Example:

Example: n The maximum height of an object thrown at angle θ is given

Example: n The maximum height of an object thrown at angle θ is given by the Newton’s law as a function of the angle θ and the initial speed v: n Create a table showing the maximum height for the following values of v and θ v = 10, 12, 14, 16, 18, 20 θ = 50°, 60°, 70°, 80° n n

Example:

Example:

Example:

Example:

Example: n Three types of products require four manufacturing processes to be produced as

Example: n Three types of products require four manufacturing processes to be produced as shown in the following table: Hours required to produce one unit Process Hourly cost Product 1 Product 2 Product 3 1 10 6 5 4 2 12 2 3 14 3 2 5 4 9 4 0 3

Example: n n n Determine the cost of each process to produce one unit

Example: n n n Determine the cost of each process to produce one unit of product 1 Determine the cost to make one unit of each product Compute the total cost of producing 10 units of product 1, 5 units of product 2, and 7 units of product 3.

Example

Example

while loop

while loop

Example n Write a MATLAB program to approximate the following so that the relative

Example n Write a MATLAB program to approximate the following so that the relative difference is less than 1%:

Example: n Write a MATLAB function that accepts an integer number x and returns

Example: n Write a MATLAB function that accepts an integer number x and returns the sum of its digits.

Example n Write a function called prime to compute the prime factors of an

Example n Write a function called prime to compute the prime factors of an integer k.

The switch statement n An alternative for the if statement

The switch statement n An alternative for the if statement

Example

Example

Example: n Use the switch statement to compute the total elapsed days in a

Example: n Use the switch statement to compute the total elapsed days in a year given the number of the month, the day, the year. - 1

Example:

Example:

Strings and Conditional Statements n A string is a variable that contains characters.

Strings and Conditional Statements n A string is a variable that contains characters.

Strings and Conditional Statements

Strings and Conditional Statements

Strings and Conditional Statements

Strings and Conditional Statements

Strings and Conditional Statements

Strings and Conditional Statements

The break and the fprintf statements

The break and the fprintf statements

The break and the fprintf statements

The break and the fprintf statements