CS 221 Fall 2011 Problem Set 2

CS 221 Fall 2011 Problem Set 2

Out: Wednesday, October 12
Due: 23:59:59 Friday, October 21

The purpose of this assignment is to reinforce the concepts of MATLAB programming, including loops. Parts of the text relevant to this assignment are Chapter 3 and Chapter 4.

Note: Solutions will be given out Monday morning, 24 October. Late submissions will not be accepted after solutions are given out.


Part 1: Implementing an algorithm with a while-loop

The division algorithm is a way to compute the quotient and remainder of one (positive) integer is divided by another. It uses only addition and subtraction.

Let n be the dividend (the number being divided), and d be the divisor. We want to find integers q (the quotient) and r (the remainder) such that:

n = q ⋅ d + r
where 0 ≤ r < d. In words: the dividend is equal to the quotient times the divisor, plus the remainder.

The algorithm works as follows. First we set q to 0 and r to n. (Note that the above equation holds now.) Then, as long as r is greater than or equal to d, we repeatedly subtract d from r, and add 1 to q.

(Note well that this ensures the above equation holds at each iteration; the equation is therefore called an invariant of the loop.)

When r becomes strictly less than d, the algorithm terminates, and the final values of q and r satisfy the specification.

You will implement this algorithm in a script file called divalg.m

  1. Use this file as a starting point. It prompts the user for the two input values and assigns them to variables "dividend" and "divisor". It also initializes the variables "quotient" and "remainder" for you.
  2. You need to write only three lines; the first line is the while statement; you have to write the boolean expression. The loop should continue to iterate as long as the value of "remainder" is at least the value of "divisor". (That is, as long as you can subtract "divisor" from "remainder" without the result being negative.)

  3. The next line updates the value of "quotient" as described above.

  4. The next line updates the value of "remainder" as described above. Note that after this line, the above equation should hold.

  5. The remaining lines output the final values of quotient and remainder using fprintf(). You will need to fill in the format string. If the dividend is 37 and the divisor is 6, the output should look like this:
    The quotient is 6 and the remainder is 1.
    

Part 2: Using for-loops in MATLAB

  1. Write a MATLAB program that uses a for-loop to find the minimum, maximum, and mean of a group of numbers. It should do the following:
    1. Create a 1-by-1000 array of random numbers between 0 and 100.
    2. Initialize variables to hold the minimum, maximum, and sum of the values. Note: the values "Inf" and "-Inf" represent positive and negative infinity, respectively. For any other value x, x < Inf evaluates to true. Note: on each iteration, these variables will hold the smallest and largest values seen so far in the array.
    3. Use a for-loop to examine each of the values in the array, and update the minimum, maximum and sum as required.
    4. After the loop, compute the mean by dividing the sum by the number of elements processed.
    5. Print the minimum, maximum, and mean, all on one line.
    6. Save your program in a file loop.m.

  2. Write a MATLAB script grades.m that does the following:
    1. Create a one dimensional array to hold student grades for 20 students, like this:
        grades = [23 90 45 60 34 56 78 90.......95];
      
      You can insert the 20 random values manually, or use one of the random-number-generating functions. However, the array should contain one value that begins with each of the digits 2-9.
    2. Loop through each element in "grades" using a for-loop, and fill in another one dimensional array "gradesEquivalent" with strings denoting grades:
      'A' if grades(i) >= 90,
      'B' if 90 > grades(i) >= 80,
      'C' if 80 > grades(i) >= 70,
      'D' if 70 > grades(i) >= 60,
      'E' if grades(i) < 60.

      So "gradesEquivalent" will look like:

      gradesEquivalent =
        E A E D E........A
      
  3. Consider a right triangle with sides A and B. Below is a table of possible lengths of sides A and B:
    A B
    1 1
    1 2
    1 3
    2 1
    3 4
    9 10
    4 9
    Write a MATLAB function hypot() that takes two arguments: arrays A and B (both must have the same number of arguments), and returns an array C, containing the length of the hypoteneuse for each triangle. Use a for-loop to iterate over the two arrays and compute the values in C. You can either add elements to C one-by-one, or initialize C to be the correct size and contain zeroes using the zeros() function. Save the function in a file hypot.m.

Part 3: Accessing Sections of Arrays

Re-read Section 3.5 in your text. Write a script file sections.m that does the following. (Omit semicolons, so that MATLAB echoes all assignments.)
  1. Create a row vector V0 with 10 random elements.
  2. Set V1 so its elements are exactly twice those of V0.
  3. Set V3 so its elements are five times those of V1.
  4. Define L0 to be an array equal to the first 4 elements of V0.
  5. Assign the fifth and sixth elements of V1 to L2.
  6. Create a 3 x 10 matrix A whose first row is equal to V0, second row is equal to V1 and third row is equal to V2.
  7. Swap the first and third rows of A. (Use any method you can think of.)

Submission Instructions

To submit your solutions, follow these steps: