Math/CSE 1560 Lab 7

Exercise: Writing while and for-loops in Maple

Objective
The objective of this exercise is to test your familiarity with solving problems using while and for-loops in Maple.

Grading
This lab exercise is worth 10% of the course. It is meant to be completed in the lab, individually (Collaboration is NOT allowed for this exercise).

NOTE: This is a 2 hour lab. The submission server will close shortly after 4:30 pm. You will not be able to submit your work after that time. No printouts will be accepted.

Starting off:

  1. Open Maple 12 by clicking on the icon marked as such. If Maple does not start, let the TA know immediately. If asked, choose to start a new worksheet.
  2. On the first 2 lines, enter your name and student number as comments -- start the lines with character #. Then save the file as `lab7.mw` by clicking on `File` and then `save as`.

Problems
Note: Some of these problems can be solved using seq() or map(), For this assignment you must use for-loops instead.

  1. (4 points) Write a procedure that takes a sorted list of integers as input and returns the number of distinct elements in the list. You are NOT allowed to convert the list to a set for this question.
    Test your procedure with lists in ascending order as well as lists in descending order.
  2. (4 points) Without using any Maple functions not covered in class (including the builtin gcd function of Maple), write a procedure that takes as input 2 integers and returns their greatest common divisor (gcd). E.g. the gcd of 6 and 9 is 3.
    You should use Euclid's algorithm for finding the GCD. This is the same algorithm that many (all?) of you learned in elementary/middle school. Formally, the property it uses can be written as follows. Assume a>=b. Then gcd(a,b) = gcd(b, a mod b)
    The algorithm for computing the GCD simply iterates this step until a mod b is 0, and at that point the algorithm returns b.
  3. (5 points) Write a procedure that takes a list with positive integers between 1 and 100 and returns a list of size 10 containing the counts in the ranges 1-10,11-20,21-30,..,91-100.
    For example, when called on the list [11,21,31,41,51,53] it should return [1,1,1,1,2,0,0,0,0]. Feel free to use the ceil function in this question.
  4. (5 points) Estimating Pi: In this question you will write a procedure that can be used to estimate Pi through sampling. The basic idea is simple. You will generate k random points. Each point is generated by computing 2 random numbers between -1 and 1 and interpreting them as the x and y coordinates of the point. Count the number of points satisfying the inequality x^2+y^2 <=1;
    Your procedure should take in a positive integer k and return the number of points that satisfies the above inequality. Be aware that you will get different answers each time you call your procedure.

    The code for generating random numbers between -1 and 1 is as follows:

    myrand:=proc()
    local t;
    t:=rand(1000000);
    return evalf(2*t()/1000000-1);
    end proc;
    
  5. (2 points) Write a procedure that takes as input a positive integer j, and invokes the procedure from Q4 with k=20,40,...,20*j and returns the results divided by k in a list.
    So if j=4, and the procedure in Q4 is called q4proc, the list returned should be evalf(q4proc(20)/20),evalf(q4proc(40)/40), evalf(q4proc(60)/60),evalf(q4proc(80)/80).

Final steps

  1. Save your worksheet.
  2. Submit the assignment as lab 7 in Moodle (the URL is moodle.math.yorku.ca). You can upload several times, but remember to submit using the "send for marking" at the end ONCE - otherwise it may not be sent.
  3. You are done with this assignment. Remember to logout before you leave the lab.