Recent

Author Topic: arithmetical with pascal [SOLVED]  (Read 1857 times)

bigmaneds

  • New Member
  • *
  • Posts: 13
arithmetical with pascal [SOLVED]
« on: October 18, 2016, 09:25:31 pm »
Hello!I have an problem,I need to get:how many numbers are smaller than middle arithmetical.
All the time I get 1 or 0.Can someone explain me ,where is my mistake,please?
Code: Pascal  [Select]
  1. program project1;
  2. uses crt;
  3. const n=101;
  4. var a:array[1..n] of integer;
  5. var s,i,b:integer;
  6.  
  7.  
  8. begin
  9.   s:=0;
  10.   b:=0;
  11.   for i:=1 to n do
  12.   begin
  13.   a[i]:=random(101);
  14.   s:=a[i]+s;
  15.   writeln('sum ',s);
  16.   writeln(s/n:0:2,' arithmetical ');
  17.   if a[i]<s/n then
  18.   b:=b+1;
  19.   writeln('');
  20.   writeln(b,'numbers smallesr than arithmetical');
  21.   readln;
  22.   end;
  23. end.  
« Last Edit: October 19, 2016, 05:56:31 pm by bigmaneds »

Eugene Loza

  • Hero Member
  • *****
  • Posts: 561
    • My "almost daily" development blog
Re: arithmetical with pascal
« Reply #1 on: October 18, 2016, 10:22:47 pm »
Your "for" loop extends to the end of the program. So basically you get really wierd results.

Code: Pascal  [Select]
  1. begin
  2.   s:=0;
  3.   b:=0;
  4.   for i:=1 to n do
  5.   begin //------------------------------- here the loop begins
  6.   a[i]:=random(101);
  7.   s:=a[i]+s;
  8.   //------------------------------- looks like you've missed "end" here.
  9.   writeln('sum ',s);
  10.   writeln(s/n:0:2,' arithmetical ');
  11.  
Now, what are you trying to do? The following lines look like they need another cycle:
Code: Pascal  [Select]
  1. //---------- start a new "for" cycle here
  2.   if a[i]<s/n then
  3.   b:=b+1;
  4. //---------- and end it here
  5.   writeln('');
  6.   writeln(b,'numbers smallesr than arithmetical');
  7.   readln;
  8.   end;
  9. end.

Try to follow the code. What it does first? What next? Imagine yourself a compiler and try to understand how it'll work and what it does.
Lazarus 1.9 + FPC 3.1.1 Debian Jessie 64 bit.

My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

bigmaneds

  • New Member
  • *
  • Posts: 13
Re: arithmetical with pascal
« Reply #2 on: October 19, 2016, 05:55:38 pm »
Huge thanks to you!  Explanation was very helpful  :)