Recent

Author Topic: For an arbitrary one-dimensional array D of 10 elements  (Read 447 times)

garandr

  • New Member
  • *
  • Posts: 10
For an arbitrary one-dimensional array D of 10 elements
« on: November 27, 2020, 10:49:21 pm »
For an arbitrary one-dimensional array D of 10 elements, find the number of elements between the numbers k1 through k2 inclusive, where k1, k2 are entered from the keyboard.
Code: Pascal  [Select][+][-]
  1. unit Unit3;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm3 }
  13.  
  14.   TForm3 = class(TForm)
  15.     Button1: TButton;
  16.     Edit1: TEdit;
  17.     Edit2: TEdit;
  18.     Edit3: TEdit;
  19.     procedure Button1Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  const N=10;
  26. var k1,k2,i:INTEGER;
  27.      d: array[1..N] of integer;
  28.   Form3: TForm3;
  29.  
  30. implementation
  31.  
  32. {$R *.lfm}
  33.  
  34. { TForm3 }
  35.  
  36. procedure TForm3.Button1Click(Sender: TObject);
  37. begin
  38.   k1:=StrToInt(Edit1.Text);
  39.   k2:=StrToInt(Edit2.Text);
  40.      randomize;
  41.     for i:=1 to N do d[i]:=random(10)-5;
  42.  
  43. end;
  44.  
  45. end.
  46.  
  47.  

jamie

  • Hero Member
  • *****
  • Posts: 4055
Re: For an arbitrary one-dimensional array D of 10 elements
« Reply #1 on: November 27, 2020, 10:55:44 pm »
Well, it's school time !  :D
The only true wisdom is knowing you know nothing

garandr

  • New Member
  • *
  • Posts: 10
Re: For an arbitrary one-dimensional array D of 10 elements
« Reply #2 on: November 27, 2020, 10:59:24 pm »
 :(

jamie

  • Hero Member
  • *****
  • Posts: 4055
Re: For an arbitrary one-dimensional array D of 10 elements
« Reply #3 on: November 27, 2020, 11:03:21 pm »
Well you have the meat of it done now..

so what's next ? find the number of matching elements within the selected range ? is that the task ?

I don't quite understand the use of the word "Inclusive" in the description. That could mean elements that have no matches or the other way round.


The only true wisdom is knowing you know nothing

wildfire

  • Jr. Member
  • **
  • Posts: 77
Re: For an arbitrary one-dimensional array D of 10 elements
« Reply #4 on: November 27, 2020, 11:05:43 pm »
Well, it's school time !  :D

To be fair the problem (as stated) needn't involve an array at all.

Assuming K2 > K1 then the answer is...

K2-K1+2

EDIT:

OK, the question is quite ambiguous, the above was also assuming K1,K2 were indices, if as I suspect they are max/min values for the elements of the array then a pointer in the right direction is to step through the array and compare the elements to check if they are within the range k1 <= element <= K2, if so add 1 to a counter.

« Last Edit: November 27, 2020, 11:15:42 pm by wildfire »
Windows 10 64bit version 1809
Laz 2.0.0 FPC 3.0.4

Zoran

  • Hero Member
  • *****
  • Posts: 1643
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: For an arbitrary one-dimensional array D of 10 elements
« Reply #5 on: November 27, 2020, 11:14:52 pm »
Let's count.
- Declare the counter variable.
- Set the counter to zero.
- For each element of the array test the condition (is it between k1 and k2). If the condition is satisfied, increase the counter.
- Display the counter.

jamie

  • Hero Member
  • *****
  • Posts: 4055
Re: For an arbitrary one-dimensional array D of 10 elements
« Reply #6 on: November 27, 2020, 11:23:50 pm »
or, "Inclusive" meaning, values that don't fit in that range !

its a confusing question..
The only true wisdom is knowing you know nothing

Zoran

  • Hero Member
  • *****
  • Posts: 1643
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: For an arbitrary one-dimensional array D of 10 elements
« Reply #7 on: November 28, 2020, 12:52:41 am »
or, "Inclusive" meaning, values that don't fit in that range !

its a confusing question..

I don't think that it is confusing, I'm sure inclusive is meant as include boundaries, that is the condition is "k1 <= Xi <= k2", not "k1 < Xi < k2".

winni

  • Hero Member
  • *****
  • Posts: 2122
Re: For an arbitrary one-dimensional array D of 10 elements
« Reply #8 on: November 28, 2020, 01:07:58 am »
Hi!

I think that Zoran is right.

But one step back:
I hate stupid and lazy teachers.

What about negativ numbers?
What about the range of the number?
Is the poor boy forced to insert something  between −2.147.483.648 and 2.147.483.647 into the two edit fields?
And the information about "inclusive" - let's be kind - is unsharp.

To make a version that is maybe OK for school let's  do two things:
Reduce the range to 0..255 - also known as a byte.
This makes the question about negative numbers obsolete.

And let's assume that the "inclusive" is used the way like Zoran thinks it is right.

Then - Mister Teacher - it goes that way:
Code: Pascal  [Select][+][-]
  1. .....
  2. var D : array[1..10] of byte;
  3. K1,K2 : byte;
  4.  
  5. procedure TForm1.FormCreate(Sender: TObject);
  6. var i : integer;
  7. begin
  8. for i := 1 to 10 do D[i] := random (256);
  9. end;
  10.  
  11. procedure TForm1.Button1Click(Sender: TObject);
  12. var err1, err2, hits,i : integer;
  13. begin
  14. val(Edit1.text, K1, err1);
  15. val (Edit2.Text,K2, err2);
  16. if err1 <> 0 then begin showMessage ('Error in Edit1'); exit; end;
  17. if err2 <> 0 then begin showMessage ('Error in Edit2'); exit; end;
  18. hits := 0;
  19. for i := 1 to 10 do
  20.   begin
  21.   if (K1 <= D[i]) and (K2>= D[i]) then inc (hits);
  22.         end;
  23. ShowMessage ( IntToStr(hits)+ ' Elements between '+IntToStr(K1)+' and '+IntToStr(K2) );
  24. end;                  
  25.  

And finally:
Teaching the teacher - it's an impossible job.

Winni


PS.:
God knows everything. Teachers know everything better.
« Last Edit: November 28, 2020, 01:12:19 am by winni »

lucamar

  • Hero Member
  • *****
  • Posts: 3461
Re: For an arbitrary one-dimensional array D of 10 elements
« Reply #9 on: November 28, 2020, 07:03:20 am »
I hate stupid and lazy teachers.

Don't be so unkind; there is probaly a lot of context which we are missing. Maybe the lesson dealt with numeric arrays or something like that. ;D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018