6.11: Practice Exercises

Fundamentals of MATLAB Programming · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    6.11: Practice Exercises

    Exercise 1: First Zero with a for Loop

    Write the function firstZeroIndex.m that receives a one-dimensional array of numbers and returns the index of the first zero it finds in the array. Use a for loop.

    Exercise 2: First Zero with a while Loop

    Write the function firstZeroIndex2.m that receives a one-dimensional array of numbers and returns the index of the first zero it finds in the array. Use a while loop.

    Exercise 3: First Zero with Logical Indexing

    Write the function firstZeroIndex3.m that receives a one-dimensional array of numbers and returns the index of the first zero it finds using logical indexing.

    Exercise 4: Make a Vector

    Write a script called makeVector.m that asks the user to enter elements of a vector one at a time. Once the user is done, print the elements of the vector in sentence format.

    Exercise 5: Get an Integer

    Write a script called getInteger.m that prompts the user for an integer. If the user enters a non-integer value, repeat the prompt until an integer is entered.

    Exercise 6: Tuition Calculator

    Write a script called tuition.m and a local function called fourYearTuition. The script should ask the user for a starting year between 2024 and 2045. The function should calculate the cost of a four-year education using the given percentage increases.

    Exercise 7: Verify Euler’s Series

    Write the script verifyEuler.m to numerically verify that the sum of 1/i^2 approaches pi^2/6. Use a while loop and stop when the approximation is within a chosen tolerance.

    Exercise 8: Count Negative Values

    Write a function countNegatives.m that receives a vector and returns how many elements are negative. Use a for loop.

    Exercise 9: Matrix Row Sums

    Write a function rowSums.m that receives a matrix and returns a column vector containing the sum of each row. Use nested loops.

    Exercise 10: Multiplication Table

    Write a script that uses nested for loops to print a multiplication table from 1 through 10.


    This page titled 6.11: Practice Exercises was last modified on Tue, 30 Jun 2026 02:11:14 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.