How do I execute this pseudocode?

310 Views Asked by At

My son ask me a math question and I can't answer it because I'm too old to remember this. Can anybody help me to solve this and i can help my son to find the answer. thanks in advance

so here the question : ` 1. given $$a = 10$$ $$b = 20$$ then $$a = a + b$$ $$b = a + b$$ Find $a,b$.

  1. given $$a = 0$$ $$i = 1$$

$$a = a + 0$$ $$i = a + i$$ loop it five time. Then $a = ??$`

2

There are 2 best solutions below

7
On BEST ANSWER

Interpreting the math as a computer program, which is often called pseudocode, we have:

$\quad a \leftarrow 10$

$\quad i \leftarrow 20$

$\quad a \leftarrow a+b \quad \textbf{a=10+20=30}$

$\quad b \leftarrow a+b \quad \textbf{b=30+20=50}$

I've added comments in bold, hoping that this makes things more clear. So at the end of this code, we have $a=30$ and $b=50$. For the next code, I've added a loop:

$\quad a \leftarrow 0$

$\quad i \leftarrow 1$

$\quad$ For $j=1$ to $5$ Do

$\quad \quad a \leftarrow a+0 \quad \textbf{a=a}$

$\quad \quad i \leftarrow a+i \quad \textbf{i=1+0=1}$

$\quad$ EndFor

We can observe that $a$ does not change in the loop, since we always add $0$ to $a$, which equals $a$. So we can basically ignore $a$, which I hope is clear.

Then, having this intuition, we see that $i$ will not change in the loop, since we always add $0$ to it. So no matter how many times we loop, $i$ will always be equal to $1$.

0
On

Simple way

So here the question : ` 1. given a = 10, b = 20, then a = a + b b = a + b a =? b =?

Inicial condiction a=10 and b=20 
a = a + b
b = a + b

FIRST Value
a=10+20=    Now a=30
b=10+20=   and b= 30

Second Value
a=30+30=    Now a=60
b=30+30=   and b= 60

repeat Then  a=b

2-) given a = 0, i = 1 a = a + 0 i = a + i loop it five time. a = ?? given a=0 i=1 a=a+0 i=a+i loop it five time. Then a=??a=??

Inicial condiction a=0 and i=1 
a = a + 0
i = a + i

FIRST Value
a=0+0=    Now a=0
i=0+1=   and i= 1

Second Value
a=0+0=    Now a=0
i=0+1=   and i= 1

repeat and stop when 5 times Then  a=0