Getting Started
Create a new script (m-file) in MATLAB and add a clear command to the top of your script.
Problem 1: Nest a for loop inside of a for loop
For example, write code to create a 25×25 multiplication table that shows the results of multiplying 1 through 25 by 1 through 25. Tip: Use the row and column variables to assign each value to the correct row and column of a new matrix, then display the newly created matrix at the end.
To create a table with R (# of) rows and C (# of) columns, nest a for loop for the columns inside of a for loop for the rows. Such an algorithm looks like this:
create a 25×25 matrix (of zeros is fine) for each row 1 to 25 for each column 1 to 25 compute and assign the value of (row × col)
Use MATLAB's Debugger to see how this matrix is filled with values.
Problem 2: Nest an if statement inside of a for loop
To perform some action on only some of the rows of a data file, nest an if statement inside of a for loop. The algorithm looks like this:
load the data file into a matrix for each row of the matrix if the row meets the condition perform the computation
Compute the total amount of bonuses that will be paid to employees of Division 1. If the employee gets the bonus, compute the bonus amount as 5% of the employee's current salary and add it to the division's bonus total. Only employees with a 1 in the bonus column should be paid the bonus. Here is the data for Division 1 (right-click on it to save the file). It looks like this (except there are no header rows and the values are all in scientific notation, which is not a problem for MATLAB).
EmpNo | Salary ($) | Bonus (1-yes/0-no) |
---|---|---|
1 |
137,000 |
1 |
2 |
98,000 |
0 |
... |
... |
... |
Use the Debugger to step through a few iterations of the loop. Pay special attention to when each variable comes into scope (shows up in the
window), changes its value, and loses its scope (leaves the window). Be sure to unset the breakpoint when you're done.Problem 3: Nest an if statement inside of a for loop inside of a for loop
Suppose the company has several divisions and we wish compute the total amount to pay out for each division as well as for the whole company. To repeat an action (code) that you have already programmed to work for many files, nest the existing code inside of another for loop. The algorithm looks like this:
for each division reset existing variables that are needed to compute next division load the division's data file into a matrix for each employee of the division if the employee gets a bonus compute the bonus update the total bonus amount paid to this division update the total bonus amount paid to all divisions store the division results for later use
Process each division's salary file and compute the total amount of bonuses that will be paid to employees of each division and the total paid out by the company, if the bonus amount is 5% of the employee's current salary.
Data files: division1.txt (already saved), division2.txt, division3.txt, division4.txt
Tip: It is possible to build a filename from string and numeric data. If you create a file name in this way, you must use the formal data = load(filename) command. For example, to create a loop through four division data files:
N = 4; % the number of divisions for d = 1:N filename = ['division' num2str(d) '.txt'] data = load(filename) ... % do the rest of the stuff for this file end
Problem 4: Find the prime factors of positive integers and ensure that user enters a positive integer.
This will involve using an outer-most while loop to continue "playing" multiple times with an if-else statement nested inside to separate valid user input from invalid input. Valid user input will further be processed using more nested constructs.
Write code to ask a user for a positive integer or 0 to end the program and then find the prime factors of the positive integer entered. Repeat the request for a positive integer or zero until you get zero or a positive integer. If the value is zero, the program is done. If the value is greater than zero, display the prime factors of the integer and then repeat the program by asking the user for a new integer to factor into primes.
Since we don't know how many times the user will play or how many times we'll have to ask the user for a positive integer, it is most appropriate to use a while loop for these actions. After the while loop to get a valid value, determine if the program should end or find all of the prime factors of the value that was input.
prompt for a positive integer or 0 to end program initialize value to the number input by the user of the program while value is not zero if the value is a positive integer initialize factor list to empty list create copy of value as V for values of p from 2 to the V/2 if p is prime while V is evenly divisible by p add p to the list of factors divide V by p display all factors else display message about valid values for this program get next value from the user
Now, that you know how to find the factors, try using the factor() function. :-)
Problem 5: Write a program that counts the number of primes between 1 and 100 (it will involve using a for loop and an if statement).
Problem 6: Write a program that finds and displays the 25th prime number (it will involve using a while loop and an if statement).
Program a dice game like Pig or Craps. Research online for the rules to many types of dice games.
Design and program your own dice game, ask for number of players, number of dice, keep score for all players, display the winner, etc., to make your game more fun.