Discussion Forum

Problem 1 in exercise 1

Problem 1 in exercise 1

av Robert Rekefors -
Antal svar: 6

I have a hard time understanding the instructions for problem 1 in exercise 1. First it says provide a pseudocode for multiplication which sounds easy. In python, it's just def multiplication(a,b):

       return a*b

What does arbitrary positive integers mean? inc seems to mean integer+1. It says we should assign with 0 and then return the value. while-loops are common in python, are DO-functions just common in C++? i:=m and i:=j seems forbidden. What exactly are the instructions for this problem? I have a hard time getting the idea behind it to figure out how to start. 

Som svar till Robert Rekefors

Sv: Problem 1 in exercise 1

av Anna Lindeberg -
Hi!

For this problem, you should not think of your pseudocode as something that resembles actual python code, but maybe closer to a very low-level programming language. Instead, the only allowed instructions are those that are stated in the problem description.
 
To not discuss the given problem we could take something even simpler: suppose we want a function that returns the value x + 2 for any input integer x. It would be ok to solve this by
 
add_two(x):
res <- 0 # assign value 0 to a new variable is OK 
inc(res)
inc(res)
return res
 
but the "python version" 
 
def add_two(x):
    return x + 2
 
is not what we are looking for in this problem. Is that making it clearer?
Kindly,
Anna

ps. Later in the course, you can of course basic arithmetic operations like multiplication in your pseudocode, just not in this problem.
Som svar till Robert Rekefors

Re: Problem 1 in exercise 1

av Marc Hellmuth -
Hi!

Anna explained already very good what is expected and how it works. Here are some additional information w.r.t. your questions:

Please "try to forget" any language specific knowledge you may have. The definition of algorithm is not based on any specific language like python, C++, Java etc. The definition of an algorithm goes back to Turing Machines. The key point is that the exercise is not asking for normal Python multiplication like return a*b. It is asking you to design multiplication from very primitive steps, almost like you are programming a very simple machine.

What does arbitrary positive integers mean?
A: Any positive whole numbers you choose — without restriction.

inc seems to mean integer+1
A: inc(variable) replaces the value of variable by the value variable + 1.

It says we should assign with 0 and then return the value.
A: No it says, you can assign to a variable 0 and do something and then may return some value.

while-loops are common in python, are DO-functions just common in C++?
A: DO is just pseudocode language, not a special Python or C++ feature. In ordinary pseudocode,
"WHILE condition DO instruction" just means: “while the condition is true, perform the instruction(s).”

i:=m and i:=j seems forbidden.
A: right. Moreover, not allowed is e.g.: direct arithmetic like a*b, a+n, a-1; copying values with j := k; assigning any nonzero value directly, like i := 5

What exactly are the instructions for this problem?
A: In essence, the exercise is saying: Build multiplication using only repeated incrementing, zero-initialization, comparisons, and while-loops.

I have a hard time getting the idea behind it to figure out how to start.
A: This exercise is very close to machine language / assembly thinking, but simplified. In high-level languages like Python, you write: "return a * b". But under the hood, the computer does not “magically” know multiplication. It eventually breaks everything down into very simple operations, like: incrementing values, comparing values, looping / jumping. This exercise is forcing you to think at that low level. In essence, this mimics a simplified model of how computers actually work at a low level (like assembly or even theoretical machines).
Som svar till Marc Hellmuth

Sv: Re: Problem 1 in exercise 1

av Robert Rekefors -
so no multiplication in the loops then? just inc(var1) and inc(var2) in the outer and inner while loops? not inc(var1*var2) or something like that?
Som svar till Robert Rekefors

Sv: Re: Problem 1 in exercise 1

av Marc Hellmuth -
As stated in the exercise - no ambiguity in the exercise statement (hence no other operations than those that are allowed).
Som svar till Marc Hellmuth

Sv: Re: Problem 1 in exercise 1

av Robert Rekefors -
Good! I don't quite understand task 4b. I did 4a but don't understand how I should determine the runtime in the big O-notation if it's not the amount of times the loop goes in 4a.
Som svar till Robert Rekefors

Sv: Re: Problem 1 in exercise 1

av Marc Hellmuth -
I do not understand your question. The task is to determine the runtime of the given algorithm. How to do this was fully explained in the lecture - see also lecture skript. You may also ask in the tutorial-session.