Assignment 2, AH, Fall 2024
Assignment 2, AH, Fall 2024
Chapters 1, 2, 3, 4, 5, 6
Due date: Monday October 21
Note: Questions 1-6 have equal marks (0.65% each question = 0.45% code +
0.2% results); Question 7 worth 1.1 (0.8% code + 0.3% results)
Question 1:
In a lottery the player has to select several numbers out of a list. Write a user-defined
function that generates a list of n integers that are uniformly distributed between the
numbers a and b. All the selected numbers on the list must be different. For function
name and arguments, use x=lotto (a, b, n) where the input arguments are the numbers
a and b, and n, respectively. The output argument x is a vector with the selected numbers.
a) Use the function to generate a list of seven numbers from the numbers 1 through 59.
b) Use the function to generate a list of eight numbers from the numbers 50 through 65.
c) Use the function to generate a list of nine numbers from the numbers -25 through -2.
Question 2:
Create Write a user-defined function that finds the largest element of a matrix. For the
function name and arguments, use [Em, rc] = matrixmax (A), where A is any size matrix.
The output argument Em is the value of the largest element, and rc is a two-element
vector with the address of the largest element (row and column numbers). If there are
two, or more, elements that have the maximum value, the output argument rc is a two-
column matrix where the rows list the addresses of the elements.
Test your function with a 4 x 6 random matrix (created by randi) and show the outputs
in the Command Window.
1
Question 3:
Write a user-defined function that sorts the elements of a vector from the largest to the
smallest. For the function name and arguments, use y=downsort (x). The input to the
function is a vector x of any length, and the output y is a vector in which the elements of
x are arranged in a descending order.
Test your function on a vector with 14 numbers (integers) randomly distributed between
-30 and 30 (using randi)
Do not use the MATLAB built-in functions sort, max, or min.
Question 4:
The factorial n! of a positive number (integer) is defined by n! = n×(n-1) × (n-2 ) ×·...·
3×2×1, where 0! = 1. Write a user-defined function that calculates the factorial n! of a
number. For function name and arguments, use y=fact (x), where the input argument x
is the number whose factorial is to be calculated and the output argument y is the value
x!.
Error checking part of the code: The function displays an error message if a negative or
non-integer number is entered when the function is called.
Do not use MATLAB built-in function factorial.
Use fact with the following numbers: (a) 7! (b) 5.5! (c) 0! (d) -11!
Question 5:
Write a script that finds the smallest even integer that is divisible by 13 and by 16 whose
square root is greater than 120. Use a loop in the program. The loop should start from 1
and stop when the number is found. The program finally prints "The required number is:"
and then the number.
Question 6:
Write a script that (a) generates a vector with 20 random integer elements with integers
between -10 and 10, (b) replaces all the negative integers with random integers between
-10 and 10, (c) repeats (b) until all the elements are positive. The program should also
count how many times (b) is repeated before all the elements are positive. When done,
2
the program displays the vector and a statement that states how many iterations were
needed for generating the vector.
Question 7:
Write a menu-driven program to investigate the constant π (Pi). Model it after the program
that explores the constant e (we discussed this in Lectures 5 and 6). Pi (π) is the ratio of
a circle’s circumference to its diameter. Many mathematicians have found ways to
approximate π. For example, Machin’s formula is:
This is called a sum of a series. There are six terms shown in this series. The first term is
4, the second term is –4/3, the third term is 4/5, and so forth.
For example, the menu-driven program might have the following options:
• Print the result from Machin’s formula.
• Print the approximation using Leibniz’ formula, allowing the user to specify how
many terms to use.
• Print the approximation using Leibniz’ formula, looping until a “good”
approximation is found.
• Exit the program.