Recent

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

guest58006

  • Guest
Would need some help again
« on: October 05, 2015, 08:10:18 pm »
I know I may become annoying but since I found this amazing forum, why not use it to find my mistakes easier and fix them?
So I've been doing another program which calculates the sum of the numbers inside a number (this may sound confusing, here's an example : 21900 = 2 + 1 + 9 + 2*0 = 12)
I've done everything correctly (so glad) but I've encountered a little problem :

  • If I place the "answer line" inside the loop (you'll see the code soon) the line repeats itself to infinity, which is a bit annoying.
  • But if I place it outside the loop, I just introduce the number and then the program stops(not the program itself) it just stays there, doing nothing..

Here's the code, and I would be very grateful if someone could help me repair the mistake and maybe even avoid it in the future :

Code: Pascal  [Select][+][-]
  1. program modsidiv;
  2. var
  3.    n,a,b,c,d,e,m: integer;
  4. begin
  5.  writeln('Introduce a number from 1-32000');
  6.  readln(n);
  7. while n<=32000 do
  8. begin
  9. a:= n mod 10;
  10. b:= (n div 10) mod 10;
  11. c:= (n div 100) mod 10;
  12. d:= (n div 1000) mod 10;
  13. e:= (n div 10000) mod 10;
  14. writeln ('The sum is', a+b+c+d+e);
  15. end;
  16. writeln('Press enter to continue...');
  17. readln;
  18. end.

balazsszekely

  • Guest
Re: Would need some help again
« Reply #1 on: October 05, 2015, 08:18:07 pm »
Quote
I know I may become annoying
You're not annoying at all! Feel free to ask whatever question you like, you don't have to apologize.

Quote
Here's the code, and I would be very grateful if someone could help me repair the mistake and maybe even avoid it in the future
Same mistake as yesterday, you create an infinite loop. In this particular case you don't need that while loop at all. Hope this helps!
« Last Edit: October 05, 2015, 08:20:08 pm by GetMem »

guest58006

  • Guest
Re: Would need some help again
« Reply #2 on: October 05, 2015, 08:22:19 pm »
Quote
In this particular case you don't need that while loop at all
Yeah, I know.
 We've just studied loops and I'm still a bit shaky when using them.
I know that I shouldn't use while loop, but we've stopped there, and our homework was to do this.
I'm trying to use a loop to not allow people to use numbers higher than 32000.
But if I put
Code: Pascal  [Select][+][-]
  1. writeln ('the sum is', a+b+c+d+e);
OUTSIDE the repeat loop, Pascal just stops and does nothing. Also when trying to introduce a new value, m which will be inbetween 1-32000 and then just place it inside the while loop instead of 32000, all the answers I'm given are 0..

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Would need some help again
« Reply #3 on: October 05, 2015, 08:25:17 pm »
Quote
I'm trying to use a loop to not allow people to use numbers higher than 32000.
I'm a bit unsure why one would want to do that. Wouldn't it be more logical to use an if comparison for that ?

If n < 32000 then
begin
  // some code
end.

However, what could be programmed inside a loop is the divisions you do ;-)

guest58006

  • Guest
Re: Would need some help again
« Reply #4 on: October 05, 2015, 08:33:19 pm »
Quote
However, what could be programmed inside a loop is the divisions you do ;-)

I've already tried placing this piece of code
Code: Pascal  [Select][+][-]
  1. writeln ('the sum is', a+b+c+d+e);
outside the loop, but then Pascal just does nothing..

balazsszekely

  • Guest
Re: Would need some help again
« Reply #5 on: October 05, 2015, 08:50:40 pm »
Quote
I'm trying to use a loop to not allow people to use numbers higher than 32000.
This is very strange, but if those are the requirements then:
Code: Pascal  [Select][+][-]
  1. //...
  2. i := 1;
  3. while i <= 32000 do
  4. begin
  5.   if i = n then
  6.   begin
  7.     a:= n mod 10;
  8.     b:= (n div 10) mod 10;
  9.     c:= (n div 100) mod 10;
  10.     d:= (n div 1000) mod 10;
  11.     e:= (n div 10000) mod 10;
  12.     writeln ('The sum is', a+b+c+d+e);
  13.   end;
  14.   Inc(i);
  15. end;
  16. writeln('Press enter to continue...');
  17. //...
  18.  

Scoops

  • Full Member
  • ***
  • Posts: 100
Re: Would need some help again
« Reply #6 on: October 05, 2015, 08:54:10 pm »
Code: Pascal  [Select][+][-]
  1. program modsidiv;
  2. var
  3.    n,a,b,c,d,e: integer;
  4. begin
  5.  writeln('Introduce a number from 1-32000');
  6.  readln(n);
  7. if n<=32000 then
  8. begin
  9. a:= n mod 10;
  10. b:= (n div 10) mod 10;
  11. c:= (n div 100) mod 10;
  12. d:= (n div 1000) mod 10;
  13. e:= (n div 10000) mod 10;
  14. writeln ('The sum is ', a+b+c+d+e);
  15. end;
  16. writeln('Press enter to continue...');
  17. readln;
  18. end.              
  19.  

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Would need some help again
« Reply #7 on: October 05, 2015, 09:04:40 pm »
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.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Would need some help again
« Reply #8 on: October 05, 2015, 09:09:32 pm »
Since OP stated to be learning about loops, i would guess that his/her problem is about how to apply loops.

I'm not giving an exact answer here, but please analyze the following code and notice the difference.

Code: Pascal  [Select][+][-]
  1. program loop1;
  2.  
  3. // counting numbers 'manually'
  4. Procedure count1;
  5. var
  6.   x: integer;
  7.   a,b,c,d,e : integer;
  8. begin
  9.   x := 1000;
  10.   a := x + 1;
  11.   b := x + 2;
  12.   c := x + 3;
  13.   d := x + 4;    
  14.   e := x + 5;
  15.  
  16.   writeln('the sum = ', a + b + c + d + e);
  17. end;
  18.  
  19.  
  20. // counting numbers in a somewhat 'smarter' way
  21. Procedure count2;
  22. const
  23.   min = 1;
  24.   max = 5;
  25. var
  26.   i: integer;
  27.   x: integer;
  28.   sum : integer;
  29. begin
  30.   i   := min;
  31.   sum := 0;
  32.   x   := 1000;
  33.  
  34.   while i <= max do
  35.   begin
  36.     writeln('x + ', i, ' = ', x + i);
  37.     sum := sum + x + i;
  38.     i := i + 1;
  39.   end;
  40.   writeln('the sum = ', sum);
  41. end;
  42.  
  43.  
  44. begin
  45.   Count1;
  46.   Count2;
  47. end.
  48.  

not implying Handoko's suggestion is wrong, as it might indeed be the case.

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Would need some help again
« Reply #9 on: October 05, 2015, 09:28:33 pm »
Advice for aerowei and beginners:
Indent your code properly, looping will become much easier.

aerowei's code didn't indent properly. It doesn't seem too bad (for now). But you will see big different if your code reaches hundreds of lines. Learn to code neatly if you want to become a great programmer. What is neat code? See the code of GetMem and molly.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: Would need some help again
« Reply #10 on: October 05, 2015, 09:43:24 pm »
If you really want/need to use a loop, I would use it for incrementing the div parameter so you would only need one add-statement within the loop.

Something like this:
Code: Pascal  [Select][+][-]
  1. program modsidiv;
  2. var
  3.   n, i, sum: integer;
  4. begin
  5.   writeln('Introduce a number from 1-32000');
  6.   readln(n);
  7.   sum := 0;
  8.   i := 1;
  9.   while i <= 32000 do
  10.   begin
  11.     sum := sum + (n div i) mod 10;
  12.     i := i * 10;
  13.   end;
  14.   writeln('The sum is ', sum);
  15.   writeln('Press enter to continue...');
  16.   readln;
  17. end.
Once i becomes 100.000 (is >32.000) the loop will end.

A whole other approach would be to read the number as a string and add the numbers that are in the string. That way you're not limited to 32.000 (or any larger integer-type). But my guess is that that's not according to the assignment.


Edit: Woops. That's probably what molly was trying to show and I gave away too much here. Sorry.

« Last Edit: October 05, 2015, 09:46:40 pm by rvk »

Mando

  • Full Member
  • ***
  • Posts: 181
Re: Would need some help again
« Reply #11 on: October 05, 2015, 09:47:51 pm »
Hi, if you want to use a loop in sum calculating, you can use this code:

Code: Pascal  [Select][+][-]
  1.  sum:=0;
  2.   while n>0 do
  3.   begin
  4.     sum:=sum+n mod 10;
  5.     n:=n div 10;
  6.   end;  
  7.  

In addition, the number (n) can have any number of digits.


Regards

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Would need some help again
« Reply #12 on: October 05, 2015, 09:48:09 pm »
Quote
However, what could be programmed inside a loop is the divisions you do ;-)

I've already tried placing this piece of code
Code: Pascal  [Select][+][-]
  1. writeln ('the sum is', a+b+c+d+e);
outside the loop, but then Pascal just does nothing..

Well, since the writeln is then placed _outside_ the while loop, the used variables in that writeln doesn't get calculated, now would they ? So how do you expect the writeln do produce sensible output ?

See also rvk's answer.

edit: @rvk: yeah, i guessed what the problem might be and you indeed gave a little too much hint imho ;-p No problem, a simple miscommunication (and you don't seem to have been the only one).


@aerowei:
a small hint.

Don't just copy-paste the proposed code, probably checking if the codes works for you.

Try to understand what happens and why. The key is here to learn how to interpret your assignment and finding a proper solution to solve the problem presented in the assignment. Simply copying the code doesn't make you learn anything, unless you understand the why.
« Last Edit: October 05, 2015, 09:55:53 pm by molly »

guest58006

  • Guest
Re: Would need some help again
« Reply #13 on: October 05, 2015, 09:48:34 pm »
THANK YOU SO MUCH FOR ALL YOUR AMAZING RESPONSES!

To be honest, I didn't expect so many answers from you guys. Thank you so much for taking your time to help me solve this (seemingly little) problem.

In the end I chose Handoko's code, even though I'm sure Scoop's and Getmem's would work aswell (tested them both), I must use only what we've been given until now, and sadly I can't use "if" or "inc" in my code (yet?). I might just end up asking my teacher if I can use some lines of code I've learned on the internet, etc. Since our subject were the loops, I had to use Handoko's code, since it was the simpliest one.

Quote
Advice for aerowei and beginners:
Indent your code properly, looping will become much easier.

aerowei's code didn't indent properly. It doesn't seem too bad (for now). But you will see big different if your code reaches hundreds of lines. Learn to code neatly if you want to become a great programmer. What is neat code? See the code of GetMem and molly

Yeah, you are right here, I'm just starting with programming (we don't even have that many lessons per week, only 1, wtf, but my biggest worry isn't indenting at the moment, but as you told me, in the future I WILL take care of it)

I've been looking at molly's code for about 5-10 mins and I seem to understand some most of it (maybe it's just me being tired and I hope tommorow I'll think clearer), but maybe because my logic isn't the shiniest of all and I can't understand some things which may sound hard for me. Mind explaining what you did in the 2nd count?

EDIT: Oh wow... 3 more replies , in such a short time - you're amazing guys! :) I'll take a look tommorow, but I still have to stay with Handoko, as we're just starting Pascal, and I'm sure my teacher won't even like the idea of me copying someone's code instead of doing it myself, and also I think (after take a very quick look) some of you have gone further and tried explaining me how to use sums in a loop.. I really appreciate that, but I do not know if I need that yet.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Would need some help again
« Reply #14 on: October 05, 2015, 10:55:11 pm »
I've been looking at molly's code for about 5-10 mins and I seem to understand some most of it (maybe it's just me being tired and I hope tommorow I'll think clearer), but maybe because my logic isn't the shiniest of all and I can't understand some things which may sound hard for me. Mind explaining what you did in the 2nd count?
Hmz. i could, but then you would not learn anything (in fact i've written down my whole analyses and wanted to post until i realized that's your job ;-) ).

If you really don't fully understand how routine1 evolved into routine2, then please ask me anything specific what i can explain to you, but please don't expect me to analyze routine1 which should be your first goal.

If you analyze routine1 properly (please use a clear head for that) then information can be extracted that leads to the solution used in routine2. Asking questions and recognizing the pattern in routine1 automatically leads to something as shown in routine2.

As a hint, when not fully understanding what happens, then you can always create small example routines that mimics things. for example change the while loop inside routine2 into something that reads:

Code: Pascal  [Select][+][-]
  1.  while i <= max do
  2.   begin
  3.     writeln('The value of i = ', i);
  4.     i := i + 1;
  5.   end;
  6.  
And see for yourself what this outputs.

Using such small examples make you understand what the code actually does. In this case you will see that the loop increases variable i until it has reached a particular value. The variable i also started at a particular value. The loop itself print out all the values while being inside the loop. Hopefully that will let you understand how a while loop works.

The fact that i do some calculation inside the loop is secondary (even while in the end it is used to produce the result), but could just as easily be analyzed by yourself ;-)

FWIW: in case you have been given a clear assignment, then don't hesitate to literally post the assignment in the forum so that we know for sure how to advise you. But please don't expect users to give you a full example that meets the requirements of your assignment. In case someone would post a fully working example that meets the requirements then please ignore that, as it won't help you learn anything.

Programming can be hard to learn, but becomes even harder when just copy-pasting someone else's code without fully understanding the code. Analyzing presented working code in hindsight is always more easy then doing things yourself. Doing things yourself has the benefit of sticking into memory better (at least that goes for most people, i am aware there are exceptions).

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).

 

TinyPortal © 2005-2018