Recent

Author Topic: Can someone help me with this  (Read 13586 times)

obeliskk1

  • New member
  • *
  • Posts: 7
Can someone help me with this
« on: April 22, 2016, 05:45:01 pm »
Ok, i need help not with making the code, but actually calculating it.
Like the code is written and with given values i have to calculate what some variable will end up with what value.
Like i think i'm doing the functions properly, but i always end up with wrong number at the end, so if you can just type numbers instead of letters in step by step, so i know where i go wrong.
Here are the 2 examples, if someone can go step by step solving this:

1) k=2, p=3, r=4, k=?
Code: [Select]
Program example1;
var
i, j, k, n, p, r: integer;
begin
read (k,p,r);
n:=0;
j:=0;
while(j<r) do
begin
k:= 2*n + k + abs(j-4);
n:=n+1;
j:=j+2;
end;
i:=0
repeat
k:=2*k+sqr(i-1)+n;
for j:=r downto p do
if j mod 2 <>0 then
k:=n+2*(j+1)+k
else
k:=n+2*j+k;
i:=i+2;
until i>=p;
writeln (k)
end.

2) k=7, m=4, k=?

Code: [Select]
Program example2;
var
i,j,k,n,m,r: integer;
begin
read (k,m);
n:=6;
i:=3;
while (i<5) do
begin
r:=i-1;
for j:=r to m do
begin
n:=n div 3 + k mod j + r;
k:=k + sqr(i-3) + n + j;
end
else
begin
n:= sqr(k div 3) + n mod 3;
k:=abs(k-10) + n;
end;
end;
i:=i+1;
end;
writeln (k)
end.
« Last Edit: April 22, 2016, 06:42:41 pm by obeliskk1 »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Can someone help me with this
« Reply #1 on: April 22, 2016, 05:58:42 pm »
could you please help us too
and put the code into code tags. Otherwise it's hard to read.

Bart

  • Hero Member
  • *****
  • Posts: 5721
    • Bart en Mariska's Webstek
Re: Can someone help me with this
« Reply #2 on: April 22, 2016, 06:41:15 pm »
Just run the program in the IDE and step through the code.
You can inspect variables (what's their value) then.

Alternatively, after each assignement ( := ), and as a first step in each loop do a writelln('avariable = ',avariable); where avariable is the variable you are interested in.

Bart

obeliskk1

  • New member
  • *
  • Posts: 7
Re: Can someone help me with this
« Reply #3 on: April 22, 2016, 06:50:44 pm »
could you please help us too
and put the code into code tags. Otherwise it's hard to read.
Ok, i did it. Sorry i didn't know for it.

Just run the program in the IDE and step through the code.
You can inspect variables (what's their value) then.

Alternatively, after each assignement ( := ), and as a first step in each loop do a writelln('avariable = ',avariable); where avariable is the variable you are interested in.

Bart

Can you do it for me and tell me what values comes after each function? If you can just paste the code and enter those writeln.
I would do it, but i never used pascal on computer, we are just doing it on paper in school.

balazsszekely

  • Guest
Re: Can someone help me with this
« Reply #4 on: April 22, 2016, 07:46:49 pm »
Jedi code format did the job for you(project1)  ;)  The second project cannot be compiled/formated, there is an else without if.
Code: Pascal  [Select][+][-]
  1. program Example1;
  2. var
  3.   i, j, k, n, p, r: integer;
  4. begin
  5.   Read(k, p, r);
  6.   n := 0;
  7.   j := 0;
  8.   while (j < r) do
  9.   begin
  10.     k := 2 * n + k + abs(j - 4);
  11.     n := n + 1;
  12.     j := j + 2;
  13.   end;
  14.   i := 0;
  15.   repeat
  16.     k := 2 * k + sqr(i - 1) + n;
  17.     for j := r downto p do
  18.       if j mod 2 <> 0 then
  19.         k := n + 2 * (j + 1) + k
  20.       else
  21.         k := n + 2 * j + k;
  22.     i := i + 2;
  23.   until i >= p;
  24.   writeln(k);
  25. end;

wp

  • Hero Member
  • *****
  • Posts: 13515
Re: Can someone help me with this
« Reply #5 on: April 22, 2016, 08:01:52 pm »
Can you do it for me and tell me what values comes after each function?
You will never learn programming with this attitude. You must try it yourself

but i never used pascal on computer, we are just doing it on paper in school.
It is a shame that your IT classes obviously are very low quality. However, there are excellent beginner videos in the web, showing you the first steps for "non-paper" coding. It's really not too difficult once you got started. Please google for "Lazarus video", or begin with links on this site: http://wiki.freepascal.org/Lazarus_videos

obeliskk1

  • New member
  • *
  • Posts: 7
Re: Can someone help me with this
« Reply #6 on: April 22, 2016, 08:43:57 pm »
Can you do it for me and tell me what values comes after each function?
You will never learn programming with this attitude. You must try it yourself

but i never used pascal on computer, we are just doing it on paper in school.
It is a shame that your IT classes obviously are very low quality. However, there are excellent beginner videos in the web, showing you the first steps for "non-paper" coding. It's really not too difficult once you got started. Please google for "Lazarus video", or begin with links on this site: http://wiki.freepascal.org/Lazarus_videos

Who says i want to become a programmer?
I want to study economics, and i choosed the IT for entry exam, which is relatively easy (theory part), and for the other part of exam i thought it would be classic programming, but they put these "kind" of tasks, which is kind of stupid, since why would i need to pretend i am a program when i should be creating that program instead. 0 sense.
That's why i found this forum, in case someone can help me, because i really don't want to waste a lot of time searching for tutorials, installing programs, just to calculate something.
I thought you people already have it installed, so you might do it in 5 minutes.
I mean if you can't it's fine, it's worth trying right? :D

EDIT: Ok, someone else helped me what to downloand and what to do, so i'm fine now.
Thanks for your assistance.
« Last Edit: April 22, 2016, 08:52:31 pm by obeliskk1 »

Windsurfer

  • Sr. Member
  • ****
  • Posts: 368
    • Windsurfer
Re: Can someone help me with this
« Reply #7 on: April 22, 2016, 09:04:33 pm »
Learning to program is learning to think logically. Clearly, from global events over the last ten years, economists only need to ask someone else for the answer. That's why they all follow like sheep!

obeliskk1

  • New member
  • *
  • Posts: 7
Re: Can someone help me with this
« Reply #8 on: April 22, 2016, 09:16:39 pm »
Learning to program is learning to think logically. Clearly, from global events over the last ten years, economists only need to ask someone else for the answer. That's why they all follow like sheep!
So you figured i can't think logically? I am stuck at calculating something i honestly don't even care about and going to forget as soon as i pass the exam, and now you litteraly try to say economists are stupid and can't think logically? Someone pissed you today, i can see that definatly.

And my opinion (if you are even interested to hear), programming is just telling computer what to do, so therefore translating. Using logic is for the creating idea how to say what you want to computer.
« Last Edit: April 22, 2016, 09:18:58 pm by obeliskk1 »

balazsszekely

  • Guest
Re: Can someone help me with this
« Reply #9 on: April 22, 2016, 09:57:08 pm »
Quote
And my opinion (if you are even interested to hear), programming is just telling computer what to do, so therefore translating.
That's not true!
Now back to the problem, the second project cannot be compiled(as I told you in my previous post), the first one returns 109. So k = 109.

Bart

  • Hero Member
  • *****
  • Posts: 5721
    • Bart en Mariska's Webstek
Re: Can someone help me with this
« Reply #10 on: April 22, 2016, 10:13:02 pm »
Just run the program in the IDE and step through the code.
You can inspect variables (what's their value) then.

Alternatively, after each assignement ( := ), and as a first step in each loop do a writelln('avariable = ',avariable); where avariable is the variable you are interested in.

Can you do it for me and tell me what values comes after each function? If you can just paste the code and enter those writeln.
I would do it, but i never used pascal on computer, we are just doing it on paper in school.

Really?
You can find a forum, become a member, ask questions, but you cannot install fpc?
They teach you programming, but they don't use a compiler or an interpreter?

And later on in life you will tell my government I only cost money, so they should get rid of me, yeah right.

I hope you fail the course miserably.

Bart

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Can someone help me with this
« Reply #11 on: April 22, 2016, 10:39:31 pm »
Really?
You can find a forum, become a member, ask questions, but you cannot install fpc?
They teach you programming, but they don't use a compiler or an interpreter?
don't get mad so quickly :)
instead you could point obeliskk1 to the installer or online-fpc (note fpc 2.6.4 used).

Please remember, if a student doesn't *want* to understand a task, you could ask to donate to the community and sell the solution :)

Btw, tracing an algorithm on paper written in pascal is a normal educational approach. Keep in mind that Pascal was designed as educational language in the first place. Readable and easy to understand.
« Last Edit: April 22, 2016, 10:46:14 pm by skalogryz »

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2770
    • havefunsoft.com
Re: Can someone help me with this
« Reply #12 on: April 22, 2016, 10:54:52 pm »
2: obeliskk1

Code: Pascal  [Select][+][-]
  1. program Example1;
  2. var
  3.   i, j, k, n, p, r: integer;
  4. begin
  5.   Read(k, p, r);
  6.   n := 0;
  7.   j := 0;
  8.   while (j < r) do
  9.   begin
  10.     k := 2 * n + k + abs(j - 4);
  11.     n := n + 1;
  12.     j := j + 2;
  13.   end;
  14.   i := 0;
  15.   repeat
  16.     k := 2 * k + sqr(i - 1) + n;
  17.     for j := r downto p do
  18.       if j mod 2 <> 0 then
  19.         k := n + 2 * (j + 1) + k
  20.       else
  21.         k := n + 2 * j + k;
  22.     i := i + 2;
  23.   until i >= p;
  24.   writeln(k);
  25. end.

a question:
 if I enter k=2, p=3, r=4 at the line #5 "Read(k, p, r)".
What the value of "k" would be when the code reaches line #8 "while (j<r)" for the first time?

« Last Edit: April 22, 2016, 10:56:52 pm by skalogryz »

Bart

  • Hero Member
  • *****
  • Posts: 5721
    • Bart en Mariska's Webstek
Re: Can someone help me with this
« Reply #13 on: April 23, 2016, 01:34:32 pm »
Please remember, if a student doesn't *want* to understand a task, you could ask to donate to the community and sell the solution :)

I find that offending.
But it may very well be in line with the common morality of his profession.

I'm OK if you (as in: a person, I'm not pointing to skalogryz here) don't want to learn something, but then be brave about it and also accept you will fail on the subject.
I also don't mindif you ar not interested in the task at hand, but if you cannot accept that you (as in: "not somebody else") still have to do it, then please don't bother getting an education at all.

If you have enough money (or you parents have), you really don't need an education at all, you'll get a well payed job anyway.
Not like the poor basterds that aren't so lucky and really strive for better education and are willing to put in the effort.
You will most likely end up being their boss anyway, just because you were born in the correct environment.

Thank god you are not going to be a health care professional, you would end up having a bigger graveyard then most of us.

Btw, tracing an algorithm on paper written in pascal is a normal educational approach. Keep in mind that Pascal was designed as educational language in the first place. Readable and easy to understand.

Yes, I still sometimes do so when I want to understand a complicated algorithm.
(And so could TS have done, but I really doubt he put any effort into it.)

Bart

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12857
  • FPC developer.
Re: Can someone help me with this
« Reply #14 on: April 23, 2016, 02:11:58 pm »
Btw, tracing an algorithm on paper written in pascal is a normal educational approach. Keep in mind that Pascal was designed as educational language in the first place. Readable and easy to understand.

(It was meant as an aid for algorithmic education to students quite far in their math/CS curriculum. To my best knowledge it was never designed for beginners)

 

TinyPortal © 2005-2018