Recent

Author Topic: Can't figure out Average of array (updated)  (Read 4511 times)

PascalCode

  • Newbie
  • Posts: 1
Can't figure out Average of array (updated)
« on: November 13, 2018, 07:35:19 pm »
I'm quite new to Free Pascal and when my average is calculated, it is not accurate and comes in an odd format e.g 4.1500000000000000E+001

Update: I believe I have fixed the problems I had. Thank You!

« Last Edit: November 16, 2018, 07:25:31 pm by PascalCode »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Can't figure out Average and Exit problem!
« Reply #1 on: November 13, 2018, 07:58:45 pm »
I see bug in this block:
Code: Pascal  [Select][+][-]
  1.  if TempVar=-1
  2.        then
  3.           Begin
  4.            numbers:=MarkNo;
  5.            MarkNo:=11
  6.           End
  7.         else;
  8.          Scores[MarkNo]:=TempVar;
  9.          numbers:=MarkNo;
  10.          MarkNo:=MarkNo+1;

The semicolon right after else is taken as an empty command. Use new begin..end block instead:
Code: Pascal  [Select][+][-]
  1.  if TempVar=-1
  2.        then
  3.           Begin
  4.            numbers:=MarkNo;
  5.            MarkNo:=11
  6.           End
  7.         else begin
  8.          Scores[MarkNo]:=TempVar;
  9.          numbers:=MarkNo;
  10.          MarkNo:=MarkNo+1;
  11.        end;
  12.  

(I didn't study the whole code closely.)
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Can't figure out Average and Exit problem!
« Reply #2 on: November 13, 2018, 08:28:35 pm »
Some more corrections, with the understanding that if this is homework you study the new code and make a good faith effort to understand what I did (which wasn't as much as I could have):

Code: Pascal  [Select][+][-]
  1. Program Student_Marks;
  2.  
  3. Var
  4.   Scores: Array[1..10] of integer;
  5.   MarkNo: integer;  //Number for the array
  6.   TempVar: integer;//Temporary Variable
  7.   numbers: integer;//Numbers in the array
  8.   MinM: integer;   //Minimum Mark
  9.   MaxM: integer;   //Maximum Mark
  10.   Av: real;        //Average Mark
  11.  
  12. Begin
  13.   MarkNo:=1;
  14.  
  15.   Repeat
  16.     Writeln('Please write the mark no. ',MarkNo);
  17.     readln(TempVar);
  18.  
  19.     numbers := MarkNo;
  20.     if TempVar = -1 then
  21.       MarkNo := 11
  22.     else begin
  23.       Scores[MarkNo] := TempVar;
  24.       MarkNo := MarkNo+1;
  25.     end;
  26.   Until MarkNo = 11; //Maximum number of scores
  27.  
  28.   MinM:=999; //Highest number to find minimum
  29.   MaxM:=0;   //Lowest positve to find maximum
  30.   Av := 0;  // Initialize average
  31.  
  32.   //Finding the Min and Max marks
  33.   for MarkNo := 1 to numbers do begin
  34.      {
  35.       NOTE that a value may be both bigger than the maximum
  36.       AND smaller than the minimum.
  37.       In fact, the very first value is almost guaranteed to be both.
  38.      }
  39.      //If current score is less than prev. minimum it's the current minimum
  40.      if Scores[MarkNo] < MinM then
  41.        MinM := Scores[MarkNo];
  42.      //If current score is more than prev. maximum it's the current maximum
  43.       if Scores[MarkNo] > MaxM then
  44.         MaxM:=Scores[MarkNo];
  45.      Av := Av + Scores[MarkNo]; // Add to the running total
  46.   end;
  47.  
  48.   Av := Av/numbers; //Finding the average
  49.  
  50.   writeln('The average mark is', Av:10:2);
  51.   writeln('The Highest mark is ', MaxM);
  52.   writeln('The Lowest mark is ', MinM);
  53. End.
  54.  

ETA: Applied Jaime's remark below--I had forgotten to add that  :-[)
« Last Edit: November 14, 2018, 01:06:19 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14200
  • Probably until I exterminate Putin.
Re: Can't figure out Average of array (updated)
« Reply #3 on: November 13, 2018, 10:27:00 pm »
Uhhh, do we not have something like that in the math unit?... Cruel me...
Specialize a type, not a var.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Can't figure out Average of array (updated)
« Reply #4 on: November 13, 2018, 10:58:30 pm »
Uhhh, do we not have something like that in the math unit?... Cruel me...

Yes we have, but then we need to call 3 functions on the array, all of them will iterate the array, and now we iterate the array only once.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Can't figure out Average of array (updated)
« Reply #5 on: November 13, 2018, 10:59:51 pm »
@TS:
What happens when user inputs e.g. -2 or 9999 or 1.5 or foobar as a mark?
Probably NOT what you would want.

Bart
« Last Edit: November 13, 2018, 11:05:53 pm by Bart »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Can't figure out Average of array (updated)
« Reply #6 on: November 13, 2018, 11:15:00 pm »
@TS:
What happens when user inputs e.g. -2 or 9999 or 1.5 or foobar as a mark?
Probably NOT what you would want.

Bart

I'm not TS but ...

Code: Text  [Select][+][-]
  1. lucamar@luna:~/SoftDev/Test/Forum/studentmarks$ ./Studentmarks
  2. Please write the mark no. 1: 5
  3. Please write the mark no. 2: 4
  4. Please write the mark no. 3: 3
  5. Please write the mark no. 4: 2.5
  6. Runtime error 106 at $08048131
  7.   $08048131
  8.   $0806659D
  9.  

Of course, error detection and recovery is left to the reader as an exercise :D
« Last Edit: November 13, 2018, 11:16:35 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Can't figure out Average of array (updated)
« Reply #7 on: November 13, 2018, 11:33:28 pm »
-1 as first input would also be a nice experience for the user...

Bart

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Can't figure out Average of array (updated)
« Reply #8 on: November 13, 2018, 11:40:17 pm »
Of course, error detection and recovery is left to the reader as an exercise :D

I would dare "PascalCode" to hand in the following to his teacher  O:-)

Code: Pascal  [Select][+][-]
  1. program avg;
  2.  
  3. {$modeswitch result}
  4. {$modeswitch out}
  5.  
  6. uses
  7.   SysUtils;
  8.  
  9. type
  10.   TMark = 0..10; //Allowed range for a mark
  11.   TIndex = 1..10; //Allowed range for the Scores array
  12.  
  13. Var
  14.   Scores: Array[TIndex] of integer;
  15.   Index: TIndex;      //Index for the array
  16.   CurrentMark: TMark; //Current Mark
  17.   NrOfMarks: integer; //Amount of marks in the array, can be zero, so not of type TIndex
  18.   MinMark: TMark;     //Minimum Mark
  19.   MaxMark: TMark;     //Maximum Mark
  20.   Sum: Integer;       //Sum of all Marks
  21.   Av: Double;         //Average Mark
  22.  
  23. function GetMarkForIndex(const Index: TIndex; out AMark: TMark): Boolean;
  24. var
  25.   Ans: String;
  26.   Value: LongInt;
  27. begin
  28.   Result := False;
  29.   repeat
  30.     write(Format('Please write the mark no. %d (Q to stop): ',[Index]));
  31.     readln(Ans);
  32.     Ans := UpperCase(Ans);
  33.     if (Ans = 'Q') then Exit;
  34.     Value := StrToIntDef(Ans, -1);
  35.     Result := Value in [Low(TMark)..High(TMark)];
  36.     if not Result then writeln(Format('"%s" is not a valid mark, please try again.',[Ans]));
  37.   until Result;
  38.   AMark := Value;
  39. end;
  40.  
  41. Begin
  42.   NrOfMarks := 0;
  43.   for Index := Low(TIndex) to High(TIndex) do
  44.   begin
  45.     if GetMarkForIndex(Index, CurrentMark) then
  46.     begin
  47.       Scores[Index] := CurrentMark;
  48.       Inc(NrOfMarks);
  49.     end
  50.     else
  51.       Break;
  52.   end;
  53.   if (NrOfMarks > 0) then
  54.   begin
  55.     MinMark := High(TMark);
  56.     MaxMark := Low(TMark);
  57.     Sum := 0;
  58.     for Index := 1 to NrOfMarks do
  59.     begin
  60.       Sum := Sum + Scores[Index];
  61.       if (Scores[Index] > MaxMark) then
  62.         MaxMark := Scores[Index];
  63.       if (Scores[Index] < MinMark) then
  64.         MinMark := Scores[Index];
  65.     end;
  66.     Av := Sum / NrOfMarks;
  67.     writeln(Format('The average mark is: %.1f',[Av]));
  68.     writeln(Format('The highest mark is: %d',[MaxMark]));
  69.     writeln(Format('The lowest mark is : %d ',[MinMark]));
  70.   end
  71.   else
  72.     writeln('Array is empty ...');
  73. end.

Bart

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Can't figure out Average of array (updated)
« Reply #9 on: November 14, 2018, 12:47:09 am »
You guys are brutal on the poor chap  :D

 To simplify his issue of display format one can use the built in formatting in the WRITE..

 Writeln(' Your message', YourNumberVariable:10:3);

 the 10:3 is the key here... its a total of 10 digits, 3 of which are on the right.
The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Can't figure out Average of array (updated)
« Reply #10 on: November 14, 2018, 01:04:56 am »
To simplify his issue of display format one can use the built in formatting in the WRITE..
  Writeln(' Your message', YourNumberVariable:10:3);
the 10:3 is the key here... its a total of 10 digits, 3 of which are on the right.

Oops! I actually forgot that was asked too  :-[ I have updated my little piece of code.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018