Recent

Author Topic: Would need some help again  (Read 9193 times)

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: Would need some help again
« Reply #15 on: October 06, 2015, 01:14:29 am »
I must use only what we've been given until now, and sadly I can't use "if" or "inc" in my code (yet?)
???  %)

so they teach you loops before decision making (comparison) ;; are you sure you didn't skip classes  :D

any way rvk, gave the most valid code -in my opinion-; so you should at least study it, and comprehend how it works
« Last Edit: October 06, 2015, 01:20:29 am by shobits1 »

guest58006

  • Guest
Re: Would need some help again
« Reply #16 on: October 06, 2015, 07:34:11 pm »
Quote
My guess:

...
...
readln(n);
while (n <= 32000) do begin
   ... // do the calculation
   ... // show the result
   readln(n);
end;
...
...

Note: the code above is not actual code.

I know this might seem strange.. but what does
Code: Pascal  [Select][+][-]
  1. readln(n);
do that stops the loop?
 I've asked my best friend (who also goes to programming with me) and he didn't know aswell. Care explaining?

Quote
???  %)

so they teach you loops before decision making (comparison) ;; are you sure you didn't skip classes  :D

any way rvk, gave the most valid code -in my opinion-; so you should at least study it, and comprehend how it works

We didn't use inc, but I know what it does. We learned if, and we've had enough assignments with "if" "case of" that we've done already.. etc etc, cause I mean I would've come to this forum even earlier if I didn't know how to use them.

Quote
So, in the end i wanted to ask you if you could explain what happens exactly in routine2, because if you can tell me that then you figured out how to solve the repeating div problem from your original question ;-) (well, at least you should be able todo so then).


Sure, no problem :) ! But, sadly neither today nor the next days.. I've got some other things to do (school and extra work for other objects), but I will surely message you with my answer/questions regarding your code on Friday/Thursday! Thanks again for your amazing response!


FOR EVERYBODY READING MY REPLIES:

I'VE JUST STARTED PROGRAMMING - which means I'm still new to it.
I've skipped two lessons(classes, anything you may call it) for personal reasons, but I've recovered everything they've studied. I know this might seem strange, but I'm completely new to programming, I've only been to about 3-4 lessons/classes in TOTAL, which means I still have yet to learn and there are a lot of holes in my understanding.
I still have lots of questions left unanswered and thus I came here. I know, I have to learn everything by myself.. but sometimes my logic doesn't shine...at all.. it may be just me who blocks myself from trying to understand something thinking it's too hard and I do not need it. Sometimes i'm a fast learner, sometimes I can't understand something that seems so simple, just because I'm giving up fast and just making myself think I'm not going to understand this.

I'm not trying to say that all your answers are worthless, they are wonderful and I really appreciate all your hard work you've put into answering (possibly) a simple question (for you) FOR my understanding. But I'm still a beginner in programming and sometimes all those pieces of code just confuse me.

Still, a big thanks to everyone who contributed to the answers. I WILL take a look at all of them (and possibly even more) on Thursday/Friday and leave a reply then.

Thanks again, and have a nice day! :)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Would need some help again
« Reply #17 on: October 06, 2015, 08:01:58 pm »
I know this might seem strange.. but what does
Code: Pascal  [Select][+][-]
  1. readln(n);
do that stops the loop?

The thing that stops the loop is if the condition is not fulfilled, in your case the condition is this one:
(n <= 32000)

The basic while loop construct is:

Code: Pascal  [Select][+][-]
  1. while (condition_fulfilled) do begin
  2.   ... // do something
  3. end;

Now we change the condition with the real thing:

Code: Pascal  [Select][+][-]
  1. while (n <= 32000) do begin
  2.   ... // do something
  3. end;

But you can't simply use it before you set the value for the condition testing. In your case, the condition testing is to test the value of n. So we added a new line: ... set the value for n

Code: Pascal  [Select][+][-]
  1. ... // set the value for n
  2. while (n <= 32000) do begin
  3.   ... // do something
  4. end;

Because the input for n is user key pressing, so we change the line with readln(n):

Code: Pascal  [Select][+][-]
  1. readln(n);
  2. while (n <= 32000) do begin
  3.   ... // do something
  4. end;

If you pay attention on the code above, it means if user provide a value less than or equal to 32000 then the condition is fulfilled. In other words, it means: to stop the looping, user need to provide a value bigger than 32000. For example 33000.

But the while looping is not finished yet. You need to set the value for the condition testing twice. The second one usually just before the end of the loop. So the code become:

Code: Pascal  [Select][+][-]
  1. readln(n);
  2. while (n <= 32000) do begin
  3.   ... // do something
  4.   readln(n);
  5. end;

Hope you can understand my explanation.


Quote
I'VE JUST STARTED PROGRAMMING - which means I'm still new to it.
I've skipped two lessons(classes, anything you may call it) for personal reasons, but I've recovered everything they've studied. I know this might seem strange, but I'm completely new to programming, I've only been to about 3-4 lessons/classes in TOTAL, which means I still have yet to learn and there are a lot of holes in my understanding.
I still have lots of questions left unanswered and thus I came here.

If you have problems or questions with programming, here you can find lots of people are nice and willing to help.

Quote
I know, I have to learn everything by myself.. but sometimes my logic doesn't shine...at all.. it may be just me who blocks myself from trying to understand something thinking it's too hard and I do not need it. Sometimes i'm a fast learner, sometimes I can't understand something that seems so simple, just because I'm giving up fast and just making myself think I'm not going to understand this.

You just need more practices and time. Don't push yourself to understand the things that too far from your ability to understand. Programming is a fun thing, so have fun with programming. Try to program something that is fun, like how to make your computer to play sounds, how to draw something on the screen, how to make object moving. After you have more knowledge of controlling those things, then try to make a computer piano, animation or games.
« Last Edit: October 06, 2015, 09:13:17 pm by Handoko »

shobits1

  • Sr. Member
  • ****
  • Posts: 271
  • .
Re: Would need some help again
« Reply #18 on: October 06, 2015, 11:31:08 pm »
aerowei, so you have learned 'if .... then' after all.
inc is not big deal inc(x) is the equivalent to x:=x+1

well, in programming you should always use the right tool to solve the problem, and unless the assignment explicitly say you should use loop to restrain the user to certain n number; then you shouldn't use the loop but use 'if .... then'.
remember that certain assignment will require you to use skills you learned in past lessons without a hint; and in this case (the assignment) I see it requires the use of 'if...' and 'while...'; and should be use like this.
1.write message and read the  n (number) entered by the user.
2.check if the n has met the requirement, using if...then statement.
2.a.    if n has not met the requirement display error message then exit the program.
2.2.    if n has met the requirement go to 3
3.enter loop while n > 0
3.a     inside the loop use m = n mod 10 to get the the 1st digit of n and add it to previous m.
3.b     inside the loop use n div 10 to discard the 1st digit in n and save it back to n.
3.c     eventually the n will be equal 0 at certain point and the loop will exit.
4.display m the sum of the digits of n
5.exit the program.

this will result in clear and clean program.
« Last Edit: October 06, 2015, 11:33:48 pm by shobits1 »

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Would need some help again
« Reply #19 on: October 07, 2015, 01:46:44 am »

I know this might seem strange.. but what does
Code: Pascal  [Select][+][-]
  1. readln(n);
do that stops the loop?
I've asked my best friend (who also goes to programming with me) and he didn't know aswell. Care explaining?
Very simple. The behaviour of readln is described here.

Quote
Read reads one or more values from a file F, and stores the result in V1, V2, etc. After that it goes to the next line in the file. The end of the line is marked by any of the supported line ending styles, independent of the platform on which the code is running (supported line ending styles are CRLF, LF or CR). The end-of-line marker is not considered part of the line and is ignored.
But since you're not reading from a file, rather from input the following applies:
Quote
If no file F is specified, then standard input is read. The variables V1, V2 etc. must be of type Char, Integer, Real, String or PChar.
But, the upper remark still counts: input is being read until an end of line marker is encountered.

Or to put it into other words: the ReadLn command waits just as long as the user pressed the enter key, then it will store the by the user entered value from input into the variable for you to work with.


Out of curiosity and for other newbies out there:

When programming a solution for these assignments, don't you use Lazarus and/or FPC ?

if so, then are you aware that help can be used?

If you use Lazarus and you have your code in the code-editor window, then the editor allows you to position the cursor over any word. As soon as you press the F1 key, the editor will pop up the specific help page for that 'active' word (as long as that word is a Pascal related/recognized word).

FP-IDE has something similar, but i believe the help must be installed manually in that case.

As an alternative you can download f.e. the windows CHM files, which you could invoke manually, in the case you are using a complete other editor. Such files can manually be searched for keywords (like readLn), and the same help-page will be displayed for you.

Although such help pages don't explain everything there is to learn on how to program in Pascal, it does offer you to get familiar with the keywords, RTL, and additional units/routines that are part of the Free Pascal programming language.

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Would need some help again
« Reply #20 on: October 07, 2015, 05:00:14 am »
To aerowei.
If you can understand my previous explanation about while looping, then you should try what shobits1 said (rvk' code).  It is more complex because it uses looping for process the mod calculation. But it's worth to study.

molly mentioned CHM-file. Yes it is very useful, I used it frequently. If you're interested, here is the link:
http://wiki.freepascal.org/Installing_Help_in_the_IDE

And these are some useful resources that worth to bookmark:
http://www.freepascal.org/docs.var
http://www.freepascal.org/docs-html/ref/ref.html#refch3.html
http://www.freepascal.org/docs-html/rtl/system/index-5.html
http://wiki.freepascal.org/LCL_Components

Also these are good places to start (for beginners only):
http://www.pascal-programming.info/index.php
http://www.taoyue.com/tutorials/pascal/index.html

 

TinyPortal © 2005-2018