The topics
- For and While -loop
- Function file, for -loop and if-testing
- Comparison and counting
sample questions
Task 1 (For and While -loop)
- A) Write a program that uses a For-loop to sum the N first odd numbers greater than 30, i.e. the sum:
S = 31 + 33 + 35 +. . . + (31 + 2 · (N − 1)), where N> 0 is a positive integer and the last number in the sum is (31 + 2 · (N − 1)).
- B) Write a program that uses a while loop to find the greatest value for N so that the sum (S) calculated by the program in 1a) does not exceed 1000.
- C) Write a program that examines if there is a N that the sum calculated in 1a) is exactly 5301. If there is such N, the program should display the message “There is a such N” on the screen, and if there is no , the function should display the message “There is no such N”.
Task 2 (function file, for -loop and if-testing)
The average of the numbers in the vector “v” is calculated by summing all the individual values, and then dividing the sum by the number of numbers in “v”.
- A) Create program code for function file “average” which takes as the input vector “v” and uses a for-loop to calculate the average of all the numbers in “v”. Hint: To determine the number of numbers (n) in the vector “v” you can use n = length (v) . Your code must begin with
function x = average (v)
- B) b) Let T be a table with n = 100 rows and m = 100 columns. Write a program that, using pre-loop and if-testing, counts the number of columns in T that have an average value of less than 10.
HINT: You retrieve a copy of column number k in table T by typing t = T (:, k);
Task 3 (comparison and counting)
- A) Create a program code for a function file “quantity” which takes as input one vector “v” and a number “t”, and returns the quantity of numbers in the vector “v” which is equal to the number “t” (Example: quantity t ([2 1 2 3 2], 2) = 3). Your code must begin with
function x = quantity (v, t)
- B) b) Create program code for a function file “quantity A” which generalizes the function from 3a), takes as input a table (matrix) “A” and a number “t”, and returns the quantity of numbers in the table “A” which is equal to the number ” t ”. The code should start with
function x = quantityA (A, t)
HINT: You can determine the number of rows (n) and columns (m) in the table “A” with the code line [n, m] = size (A) ;.
Task 4
A coin toss (‘mk’) can be simulated with the code line mk = randi (2,1); , where we interpret the value 1 as the page “crown”, and the value 2 as the page “coin” (so this code line gives us (randomly) one of the numbers in the set {1,2}).
- A) Create program code for a function coin “coin toss” that simulates “n” coin toss and returns the vector “v” with the result of the coin toss, together with the number “quantity of Coins” which indicates how many of the coin tosses in “v” resulted in the page ” coin”. Your code should begin with
function [v, quantity Coin] = coin toss (s)
- B) Use the function you wrote in problem A) to simulate n = 100-coin toss. Write a program that counts the quantity of simulated coin tosses before the first “crown” occurs. Suggest a “reasonable” value for the case where since “crown” is not observed in the n coin tosses.