<texit info> author=Roman Putanowicz backgroundtext=http://www.L5.pk.edu.pl/~putanowr/iten.html title=Lab 4: Control flow : loops and conditional statements showbuttons=off </texit> <texit>\input{/home/prac/putanowr/dokuwiki/data/media/wiki/latex/it_preamb.tex} \setcounter{chapter}{3} </texit> <texit>\begin{comment}</texit> prev | up | next <texit>\end{comment}</texit>

Lab 4 : Control flow : loops and conditional statements

Description

Repeating instructions - the for loops. Programming vector operations: scalar product, vector norms. Calculation of expressions involving sum and product operators ($ \sum$ and $\prod $ respecitively). Comparison operators and Boolean expressions. Checking conditions - the if command. Tabulating simple and compund functions. Writing programs that generate simple ASCII graphics.

Skills to be acquired

  • Understanding of syntax and semantics of for and if statements.
  • Translating expressions involving sums and products into computer code
  • Constructing compound Boolean expressions

Self study

In this lab two basic control structures were introduced - “for” loops and “if” statements. There are some details about these statements that were not mentioned but which can be useful in some programs. They are given as the self study topics below:

  • early loop termination with break and continue statements,
  • boolean expressions involving vector and matrix operands.

Readings

  • Lab 2 handouts – another course lab handouts (in Polish), conditional expressions
  • Lab 3 handouts – another course lab handouts (in Polish), “for” loops
  • Lab 2 handouts – another course lab handouts (in Polish), “while” loops

Solved problems

Ex. 4.1.1

Given two sequences: \begin{equation*} \begin{split} a_i &= sin(2\pi i + 1) \\ b_i &= \frac{1}{i+1} \end{split} \end{equation*} Write a script that calculates the value of the expression below: \begin{equation*} \sum_{j=1}^{j=N}\sum_{i=1}^{i=j}b_ja_i \end{equation*}

Solution.

Ex. 4.1.2

The distance L from point A to B is to be traversed in stages. At each stage one traverses only 1/3 of the remaining distance. Calculate how many steps it will take before one gets no further than the distance 0.1*L from the point B.

Solution.

Questions and open problems

Ex 4.2.1

Write Octave function for the graph shown in the figure below. Write a test script for that function.

Ex 4.2.2

Write Octave function that checks if a point (x,y) belongs to the shaded area as in the figure below. The function should return 1 if the point belongs to the area, otherwise 0. Write a test script for that function.

Ex 4.2.3

Write a function that finds the second maximum element of a given vector.

Ex 4.2.4

Write a function that calculates the area bounded by N-th iteration of Koch snowflake. The tree first iterations of Koch snowflake are shown in the figure.  Three first iterations of Koch snowflake.

Ex 4.2.5

Write a program that writes out at what time (the hour and the minutes) during the day the hour hand and minute hand make square angle.

Ex 4.2.6

Write a program that reads in a vector and the writes it out in reverse order.

Ex 4.2.7

Write a program that randomly picks $N$ points from a square $[-1,1]\times[-1,1]$ N and calculates the bounding box of them. Hint: use function rand() to generate a random number in range (0,1).

Ex 4.2.8

Write a script that writes out all integers divisible by 3 and 4 in the range [a,b] inclusive.

Ex 4.2.9

A 2D curve is given in parametric form: \begin{equation*} \mathbf{r}(t) = \begin{bmatrix} x(t) = 5.0 \cos(t)\\ y(t) = 2.0 \sin(t). \end{bmatrix} \end{equation*} Write a function that will estimate the curve length for $t\in[t_1, t_2]$, by approximating it with a polyline with resolution $\Delta t$.

<texit>\begin{comment}</texit> prev | up | next <texit>\end{comment}</texit>

  • en/teaching/subjects/it/labs/lab4.txt
  • Last modified: 2017/10/02 15:54
  • (external edit)