Can someone help me with Pseudocode? - pseudocode

The variable X starts with the value 0.
The variable Y starts with the value 5.
Add 1 to X.
Add 1 to Y.
Add X and Y, and store the result in Y.
Display the value in Y on the screen.
Output #1:
Pseudocode #1:
The logic of the question makes sense, but I do not even know where to start. Can someone explain and/or help me?

Pseudocode:
X = 0;
Y = 5;
X = X + 1;
Y = Y + 1;
Y = X + Y;
display(Y);
I'll leave the maths to you...

Related

Python storing value of old assigned variable

lets say i have this code
x = 5
while True:
x = 10
break
is there a way I can keep the original value of x and the new assigned value
This pattern might be what you're looking for
x = 5
y = 0
while True:
# y stores old value of x
y = x
x = 10
# example where x actually changes
while True:
y = x
x += 1
print(y, x)

Unable to understand the result of this hash function

I was reading my notes from the algorithms class (several years old) and I found this:
which says: Assuming that
h(k) = k mod m, where m = 4 and k = 100, then h(k) = 4
Is this true? I would think that 4 * 25 = 100, thus h(k) = 0. What am I missing?
I thought it was a typo, but I just checked the newest version of the notes and it's still the same!
The modulo operator can never return that result, as it represents the remainder after integer division.
So this rule holds for positive integers x and y:
x mod y = z ⇒ z < y
Another way to write the above modulo operation is:
⎣x/y⎦.y + z = x
If somehow you would achieve that z == y then obviously you did something wrong in the ⎣x/y⎦ part.

Procedural/imperative programming - Algorithm

Can you please help me understand what ports in r if x = 0,1,2,3
y <-- 0
z <-- 1
r <-- z
while y < x {
Multiply z by 2;
Add z to r;
Increase y; }
In every looping step z is multiplied by 2, so you have the values 2,4,8,16... (or generally 2^n).
r is initially 1, and if you add z, you get 3,7,15,31 (generally 2^(n+1) - 1)
For x = 0 the loop will be skipped, so r stays 1
For x = 1 the loop will... uhm... loop one time, so you get 3
etc.
Apparently, the algorithm computes the sum of the powers of two from 0 to x and uses r as an accumulator for this. On termination, r holds the value 2^(x+1)-1.

factorial algorithm in pseudo code

I've been given the following algorithm, that takes a positive integer K and returns a value:
X = 1
Y = 1
while X ≠ K do
X = X + 1
Y = Y * x
return Y
I'm supposed to figure out what it returns.
As it happens, I know the answer — it returns the factorial of K — but I don't understand why.
How do you go about figuring out what this pseudocode does?
X = 1 <- this is counter which you gonna multiply in every step
Y = 1 <- this is to store the cumulative product after each step
while X ≠ K do <- until you reach K
X = X + 1 <- increase X
Y = Y * X <- multiply with increased X
return Y <- return the product
So in the loop the cumulative product goes like this 1 -> 1*2 - > 2*3 -> 6*4 -> ... -> 1*2*..*(K-1)*K which is K!
Now let's assume that K is 5. Therefore the factorial of 5 is 120.
Then as you enter the loop X value is 2 and y gets the value 2.(1*2)
Then the value of X is 3 after getting into loop, which then makes the value of Y 6 because (3*2).
Then the value of X is 4 after getting into loop, which then makes the value of Y 24 because (4*6).
Then the value of X is 5 after getting into loop, which then makes the value of Y 120.
Then since X==Y the while loop exits and Y value which is the factorial is returned.
this piece of code can be simply rewritten as (in C/C++/Java),
for(X=1;X<=K;X++){
Y=Y*X;
}
Now it describes it self :-)

MatLab - Newton's method algorithm

I have written the following algorithm in order to evaluate a function in MatLab using Newton's method (we set r = -7 in my solution):
function newton(r);
syms x;
y = exp(x) - 1.5 - atan(x);
yprime = diff(y,x);
f = matlabFunction(y);
fprime = matlabFunction(yprime);
x = r;
xvals = x
for i=1:8
u = x;
x = u - f(r)/fprime(r);
xvals = x
end
The algorithm works in that it runs without any errors, but the numbers keep decreasing at every iteration, even though, according to my textbook, the expression should converge to roughly -14 for x. My algorithm is correct the first two iterations, but then it goes beyond -14 and finally ends up at roughøy -36.4 after all iterations have completed.
If anyone can give me some help as to why the algorithm does not work properly, I would greatly appreciate it!
I think
x = u - f(r)/fprime(r);
should be
x = u - f(u)/fprime(u);
If you always use r, you're always decrementing x by the same value.
syms x
y = exp(x) - 1.5 - atan(x); % your function is converted in for loop
x=-1;
n=10;
v=0;
for i=2:n
x(i)=tan(exp(x(i-1))-1.5);
v=[v ;x(i)]; % you will get solution vector for each i value
end
v

Resources