Recent

Author Topic: Deviation  (Read 3173 times)

DieterV

  • New member
  • *
  • Posts: 8
Deviation
« on: September 26, 2017, 02:01:46 pm »
Hello all,

Once again I am stuck with my mind in a code.
It goes like this.
I am trying to teach myself. So I would love to get some explanation, not just copy paste code. So I can think about it.

I already checked this forum but didn't find any correct solution.

Code: Pascal  [Select][+][-]
  1. program afwijkingDieter;
  2.  
  3. // declare your variables here
  4.    var arrHoogte: array of real;
  5.   var aantal, teller, waarde:integer;
  6.   var totaalHoogte, gemiddeldeHoogte, deviatie, standaardDeviatie:real;
  7.   var vergelijkGemiddelde, totaalVanVergelijkteGemiddelde:real;
  8.   var aboveOnce,aboveTwice,tooShort:integer;
  9. begin
  10.  
  11.   aantal:=1;
  12.   totaalHoogte:=0;
  13.   SETLENGTH(arrHoogte,aantal);
  14.   repeat
  15.     writeln('Input hoogte of 0 om te stoppen.');
  16.     readln(waarde);
  17.     if (waarde <> 0) then
  18.     begin
  19.       arrHoogte[aantal] := waarde;
  20.       totaalHoogte:= totaalHoogte + arrHoogte[aantal];
  21.       aantal := aantal+1;
  22.       SETLENGTH(arrHoogte,aantal);
  23.     end;
  24.   until (waarde = 0);
  25.   gemiddeldeHoogte:= totaalHoogte/(aantal-1);
  26.   totaalVanVergelijkteGemiddelde:= 0;
  27.   for teller := 1 to aantal-1 do
  28.   begin
  29.     vergelijkGemiddelde:= SQR(arrHoogte[teller] - gemiddeldeHoogte);
  30.   end;
  31.   totaalVanVergelijkteGemiddelde:= totaalVanVergelijkteGemiddelde + vergelijkGemiddelde;
  32.   deviatie:= totaalVanVergelijkteGemiddelde / (aantal-1);
  33.   standaardDeviatie:= SQRT(deviatie);
  34.   aboveOnce:=0;
  35.   aboveTwice:= 0;
  36.   tooShort:=0;
  37.   for teller := 1 to aantal do
  38.   begin
  39.     if (arrHoogte[teller] > standaardDeviatie) then
  40.     begin
  41.       aboveOnce:= aboveOnce+1;
  42.       writeln(aboveOnce);
  43.     end;
  44.     if (aboveOnce > standaardDeviatie*2) then
  45.     begin
  46.       aboveTwice:= aboveTwice+1;
  47.     end;
  48.   end;
  49.   writeln();
  50.   writeln(('The average height is : '), gemiddeldeHoogte:0:2);
  51.   writeln(('The standard deviation is : '), standaardDeviatie:0:2);
  52.   writeln(('Amount of students one time above standard deviation are : '), aboveOnce);
  53.   writeln(('Amount of students two times above standard deviation are : '), aboveTwice);
  54.   writeln();
  55.   writeln(('Press enter to QUIT'));
  56.   readln();
  57. end.                              

When I use the 2 digits  170 and 150, the  deviation should be 35,35.

When I run this program i get 102.53.

I think the calculation is correct ....


Thanks for the feedback.

DieterV

  • New member
  • *
  • Posts: 8
Re: Deviation
« Reply #1 on: September 26, 2017, 02:18:36 pm »
Code: Pascal  [Select][+][-]
  1. for teller := 1 to aantal-1 do
  2.   begin
  3.     vergelijkGemiddelde:= SQR((arrHoogte[teller] - gemiddeldeHoogte));
  4.  
  5.   end;
  6.   writeln (vergelijkGemiddelde:0:2) ;                                                                                                                  


Hello, I get the value 21025,00 while It should be 1250.
Isnt this for count 1 SQR(170-145) so 25²
and for count 2 SQR(120-145) so -25²?


rvk

  • Hero Member
  • *****
  • Posts: 6109
Re: Deviation
« Reply #2 on: September 26, 2017, 02:22:35 pm »
First problem when looking at your code.
You have SETLENGTH(arrHoogte,aantal);
Directly after you try to use arrHoogte[aantal] := waarde;

But you should know that a dynamic array is always 0-based !!
So when you so SetLength(arrHoogte, 1) you only get arrHoogte[0]
and if you do SetLength(arrHoogte, 2) you only get arrHoogte[0] and arrHoogte[1].

So you need to take that into account.
(Hint: You can put {$R+} at the top of your program. It will show you when you try to access something that you shouldn't)

rvk

  • Hero Member
  • *****
  • Posts: 6109
Re: Deviation
« Reply #3 on: September 26, 2017, 02:31:03 pm »
Try to put in some writelns to show the results in between the calculations.
(but I see you already did that)

If I fix the array problem I get 7,07 as deviation for 170 and 150.

But you say it should be 35,35?
Should the deviation of 170 and 150 be 14,14 ???
See http://www.calculator.net/standard-deviation-calculator.html?numberinputs=170%2C150&x=47&y=19

(my result is exactly half that so there is still something wrong in your calculations)

DieterV

  • New member
  • *
  • Posts: 8
Re: Deviation
« Reply #4 on: September 26, 2017, 02:35:01 pm »
Thanks for the reply RVK!

Sorry I tested it with 170 & 120 :D not 150  :-[


rvk

  • Hero Member
  • *****
  • Posts: 6109
Re: Deviation
« Reply #5 on: September 26, 2017, 02:38:27 pm »
Sorry I tested it with 170 & 120 :D not 150  :-[
Yeah, that would explain it :D
That should give 35.355339059327. (35.36 rounded)

After fixing the array it gives me 17.68 instead of 35.36. So exactly half.

DieterV

  • New member
  • *
  • Posts: 8
Re: Deviation
« Reply #6 on: September 26, 2017, 02:40:35 pm »
Ok, ill check it out.

Still didn't fix the arrays tho. I do not understand it yet I guess  :-X

rvk

  • Hero Member
  • *****
  • Posts: 6109
Re: Deviation
« Reply #7 on: September 26, 2017, 02:45:29 pm »
I do not understand it yet I guess  :-X
If you have
Code: Pascal  [Select][+][-]
  1. var
  2.   arrHoogte: array of real;
You always get an array beginning at index 0 (so arrHoogte[0]).
So if you set the number of elements of arrHoogte to 10 you get arrHoogte[0] through arrHoogte[9].
Notice that you DON'T get arrHoogte[10] and you may not use that number.

You can either remember always to use arrHoogte[number - 1] or you can cheat a little and always do SetLength with one number higher then really needed.

So if you change the SetLength(arrHoogte, aantal); to SetLength(arrHoogte, aantal + 1); you CAN access arrHoogte[aantal].
(you have the SetLength in two places in your code at the moment, so change them both)

B.T.W. you don't use arrHoogte[0] at all in that case so it is a waste of space, but for one real that really doesn't matter.
You DO need to remember that a dynamic array is always 0-based because you are going to run into this in the future some more.
« Last Edit: September 26, 2017, 02:47:42 pm by rvk »

rvk

  • Hero Member
  • *****
  • Posts: 6109
Re: Deviation
« Reply #8 on: September 26, 2017, 03:00:11 pm »
B.T.W. May you cheat :D

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses Math;
  3. var
  4.   arrHoogte: array of double;
  5. begin
  6.   SetLength(arrHoogte, 2);
  7.   arrHoogte[0] := 170;
  8.   arrHoogte[1] := 120;
  9.   writeln(stddev(arrHoogte): 0: 3);
  10.   readln;
  11. end.

Of course you can see in the sources how stddev() is done in the math-unit  :D

DieterV

  • New member
  • *
  • Posts: 8
Re: Deviation
« Reply #9 on: September 26, 2017, 03:10:08 pm »
I do not understand it yet I guess  :-X
If you have
Code: Pascal  [Select][+][-]
  1. var
  2.   arrHoogte: array of real;
You always get an array beginning at index 0 (so arrHoogte[0]).
So if you set the number of elements of arrHoogte to 10 you get arrHoogte[0] through arrHoogte[9].
Notice that you DON'T get arrHoogte[10] and you may not use that number.

You can either remember always to use arrHoogte[number - 1] or you can cheat a little and always do SetLength with one number higher then really needed.

So if you change the SetLength(arrHoogte, aantal); to SetLength(arrHoogte, aantal + 1); you CAN access arrHoogte[aantal].
(you have the SetLength in two places in your code at the moment, so change them both)

B.T.W. you don't use arrHoogte[0] at all in that case so it is a waste of space, but for one real that really doesn't matter.
You DO need to remember that a dynamic array is always 0-based because you are going to run into this in the future some more.

That actually solved all indeed. Should have found it with your first post tho.  :(
No cheating allowed! :D The course is about thinking like a programmers logic, not really about pascal.

Thumbs up rvk. Thanks

DieterV

  • New member
  • *
  • Posts: 8
Re: Deviation
« Reply #10 on: September 27, 2017, 11:23:39 am »
B.T.W. May you cheat :D

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses Math;
  3. var
  4.   arrHoogte: array of double;
  5. begin
  6.   SetLength(arrHoogte, 2);
  7.   arrHoogte[0] := 170;
  8.   arrHoogte[1] := 120;
  9.   writeln(stddev(arrHoogte): 0: 3);
  10.   readln;
  11. end.

Of course you can see in the sources how stddev() is done in the math-unit  :D

Anyone got an idea how I can throw the deviation formula in a function?
I really dont understand the functions with parameters and stuff

 

TinyPortal © 2005-2018