Need Help on one PASCAL program (it's easy)

06/25/2016 18:48 obeliskk1#1
Ok, i need help about this code. I need someone to calculate value of "k" after each possible loop. The last value of k is 51, but i can't seem to get that value by calculating it by myself. Can anyone help me where i am going wrong?
I need also an explanation how u get to that number, so i can understand in future tasks.


Code:
Program a;
var
  i, j, k, m, n, p, r: integer;
begin
  k:=1;
  p:=1;
  r:=3;
  n:=1;
  j:=1;
repeat
  k:=2*(n+sqr(j-2))+k;
  j:=j+2;
until j>=5;
 
for i:= r downto p do
  if i <> 2 then
  begin
      for m:=1 to 2 do
        k:= k+3*m;
        k:= p mod i + k;
  end
  else
  begin
      for m:=1 to 2 do
        k:=2*k +m;
        k:=k div i + 2;
  end;
writeln(k);
end.
06/25/2016 19:49 Dungedragon#2
Quote:
Originally Posted by obeliskk1 View Post
Can anyone help me where i am going wrong?
Not really. It looks like homework apparently.

So what's the problem exactly? You have problems understanding the whole code or just parts? Which parts? I would suggest to use a table and keep record of all variables while going through the algorithm line by line. Thats what i usually do when i have problems understanding (most iterative) algorithms.

I suggest to post your progress so far so we can actually take a look and see where your problem is.

[I still do not understand why you flag your question as "it's easy" although you have problems understanding it :(]
06/25/2016 20:01 obeliskk1#3
Quote:
Originally Posted by Dungedragon View Post
Not really. It looks like homework apparently.

So what's the problem exactly? You have problems understanding the whole code or just parts? Which parts? I would suggest to use a table and keep record of all variables while going through the algorithm line by line. Thats what i usually do when i have problems understanding (most iterative) algorithms.

I suggest to post your progress so far so we can actually take a look and see where your problem is.

[I still do not understand why you flag your question as "it's easy" although you have problems understanding it :(]
I thought it was super easy for someone who is doing these kind of stuff, since i'm not even beginner.

Ok, here is the thing, in the first FOR loop i have to do that loop 3 times (3 to 1). And i got from compiler results: 19 (i=3), 42 (i=2), 51 (i=1).

To get those results, i had to do next FOR loop, which consisted of 2 "k" equations and also i'm doing it 2 times since it 1 to 2 loop.
But in order to get correct result in second time doing that loop, i am not suppose to calculate the second "k" equation. Does anyone know why?

ALSO not that this is not homework, this just a random code i took to practise on understanding for loop when it becomes little bit messy.
06/25/2016 20:36 .Scy#4
how about just putting a writeln(k) in each of the loops with a text that says where you are? example:
Code:
program a;
var
i, j, k, m, n, p, r: integer;
begin
k:=1;
p:=1;
r:=3;
n:=1;
j:=1;
repeat
k:=2*(n+sqr(j-2))+k; writeln("every value that changes");
j:=j+2;
until j>=5;
for i:= r downto p do
if i <> 2 then
begin
for m:=1 to 2 do
k:= k+3*m;
k:= p mod i + k;
end
else
begin
for m:=1 to 2 do
k:=2*k +m;
k:=k div i + 2;
end;
writeln(k);
end
that should output the results you want to desperatly know, which in turn will help you understand how the code works, also if pascal supports it you can have a pause after each loop for x secs or have it require you pressing a key to continue.
06/26/2016 00:24 obeliskk1#5
Quote:
Originally Posted by .Scy View Post
how about just putting a writeln(k) in each of the loops with a text that says where you are? example:
Code:
program a;
var
i, j, k, m, n, p, r: integer;
begin
k:=1;
p:=1;
r:=3;
n:=1;
j:=1;
repeat
k:=2*(n+sqr(j-2))+k; writeln("every value that changes");
j:=j+2;
until j>=5;
for i:= r downto p do
if i <> 2 then
begin
for m:=1 to 2 do
k:= k+3*m;
k:= p mod i + k;
end
else
begin
for m:=1 to 2 do
k:=2*k +m;
k:=k div i + 2;
end;
writeln(k);
end
that should output the results you want to desperatly know, which in turn will help you understand how the code works, also if pascal supports it you can have a pause after each loop for x secs or have it require you pressing a key to continue.
That's what i've done, i already wrote it:

19 (i=3), 42 (i=2), 51 (i=1).

The problem is i don't get it how it get those numbers. Only way i get them is if i calculate both "k" equations first time and second time only first "k" equation in FOR "m" LOOP. Is it suppose to be like that or what?