Macro 318: Problem Set #1


Introduction to Programming with Julia

Lecturer:
Dawie van Lill (dvanlill@sun.ac.za)


Instructions

During this tutorial we will quickly show how to install and use Julia.

However, there are several videos uploaded to SUNLearn on how to do this.

The questions for the problem set range from easy to hard.

The first question is easiest and then it gets progressively harder.

You should try and complete these questions before the lecture so that you can ask questions.

The fourth question is optional.

In the tutorial there will be one unseen question that you will have chance to finish in the lecture.

Before we start with the tutorial, make sure to add the LinearAlgebra and Random packages as follows,

In [2]:
import Pkg

Pkg.add("LinearAlgebra")
Pkg.add("Random")

using LinearAlgebra
using Random
   Resolving package versions...
  No Changes to `C:\Users\Dawie\Dropbox\2022\318-macro\git\Macro-318\Project.toml`
  No Changes to `C:\Users\Dawie\Dropbox\2022\318-macro\git\Macro-318\Manifest.toml`
   Resolving package versions...
  No Changes to `C:\Users\Dawie\Dropbox\2022\318-macro\git\Macro-318\Project.toml`
  No Changes to `C:\Users\Dawie\Dropbox\2022\318-macro\git\Macro-318\Manifest.toml`

Exercise #1: Operators and types

Take a few seconds to try and code up an answer to the following question.

This will help you come to grips with the way in which arithmetic operations are defined in Julia.

Determine the value and type of y given by the following expression

$$ y = \frac{(x + 2)^2 - 4}{(x - 2)^{p - 2}}, $$

where x = 4 and p = 5.


Exercise #2: Arrays and indexing

Consider a two dimensional array, which we can call x_2d.

  1. Fill this $4 \times 4$ array with values
  2. Extract the values for the last column of this array
  3. Extract the value along the second row and third column

Exercise #3: Control flow

  1. Create an array called int_array and fill it with five integers between 1 and 15 (this includes both 1 and 15).

  2. Create a for loop that iterates over the elements of int_array. Inside the for loop use if/else statments to print the following.

    • If an element of the array is smaller that 8, print "Small"
    • If an element of the array is greater than 8, print "Large"
    • If an element of the array is equal to 8, print "Eight"

Exercise 4: Control flow (optional)

The following question is optional.

Edit your if/else statement from Question 3 so that if the any of the elements of int_array is a multiple of 3 ONLY print "Multiple of 3"