Recent

Author Topic: [SOLVED]How to find Lowest (random) Number in array? [Thanks]  (Read 560 times)

Benko

  • Newbie
  • Posts: 1
Hello, i have sort of problem with numbers in Lazarus.
Our teacher gave me a homework.
Homework: Write a program which will randomly generate [n] of numbers. Then find lowest and 2nd lowest number, if there will be more of same numbers it will count them.

Random numbers:58,21,8,16,21,2,8,17,41,23,24

Answer should look like this: First minimum is 8 and its count is 2.
                                          Secound minimum is 2 and its count is 1.


this should be written in Memo1.

I should write to the Edit1 how many numbers i want

and after clicking Button1 it will all proced.



This is all i did so far:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.   var x,i,u,z,min,mini:byte;
  3.    p:array [1..30] of integer;
  4. begin
  5.   memo1.Clear;
  6.  
  7.   x:=strtoint(edit1.text);
  8.   for i:=1 to x do
  9.   begin
  10.     p[i]:=random (80);
  11.     memo1.caption:=memo1.caption+inttostr(p[i])+',';
  12.  
  13.     min:=p[i];
  14.     u:=0;
  15.     for i:=1 to x do
  16.     begin
  17.  
  18.     end;
  19.   end;
  20. end;
  21.  
  22. end.    
  23.  
  24.  
  25.  
  26.  
« Last Edit: April 08, 2020, 10:06:46 pm by Benko »

lucamar

  • Hero Member
  • *****
  • Posts: 2921
Re: How to find Lowest (random) Number in array? Please
« Reply #1 on: April 08, 2020, 03:29:51 pm »
Please, use [code] tags to post code; otherwise you risk the forum software taking parts of the code as formatting codes for itself, as happens in your post: when the software sees p[i] it takes the [i] to mean "start italics".

As for your question, one easy way to solve it is to use a "counting" array of integer:
1) Start all its items to 0
2) each time you read a number n from the "randoms" array, increment the corresponding Counts[n]
3) Once you're done, traverse the "counts" array looking for the first (and second) non-zero values.

HTH
« Last Edit: April 08, 2020, 03:37:21 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.8/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 6374
    • wiki
Re: How to find Lowest (random) Number in array? Please
« Reply #2 on: April 08, 2020, 03:32:16 pm »
Modified by moderator: Added code tags.

jamie

  • Hero Member
  • *****
  • Posts: 3096
Re: How to find Lowest (random) Number in array? Please
« Reply #3 on: April 08, 2020, 06:49:30 pm »
@Benko

 Prebuild the array of randoms first, don't try to pick them out as you are building the array..

after the array Is built then scan it for lowest and duplications
The only true wisdom is knowing you know nothing

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 6374
    • wiki
Re: How to find Lowest (random) Number in array? Please
« Reply #4 on: April 08, 2020, 07:46:26 pm »
Write separate code for each task

1) Fill array with random number

2) Search lowest number
3) count how often this number is in the list.

(You can do 2 and 3 in one step, but it may be easier to do 2 steps)

If your teacher told you about procedures, then each of those is one procedure (or function).

For each try to get some code, then ask again.

READ THE REST, ONCE YOU GOT THE ABOVE

4) Now find the 2nd lowest number... (and I guess then the 3rd lowest and so on)
That requires some thinking.
- The 2nd lowest number is the lowest number, if you ignore the previous found lowest.
- the n-th (3rd, 4th ...) lowest number is the lowest number, if you ignore the previous n-th.

So when going through the list to find the n-th lowest, you do
- check element at index i:
  - is bigger that last found ?
  - is lower that others in the list


Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 6374
    • wiki
Re: How to find Lowest (random) Number in array? Please
« Reply #5 on: April 08, 2020, 07:52:11 pm »
Quote
Code: Pascal  [Select][+][-]
  1. p:array [1..30] of integer;
Allows for max 30 numbers. (So make sure the  user's input is not higher

You should also consider renaming X into "NumberCount" or "NumbersInListCount".

You will need this count for each of the tasks: find minimum, count minimum ......


440bx

  • Hero Member
  • *****
  • Posts: 1883
Re: How to find Lowest (random) Number in array? Please
« Reply #6 on: April 08, 2020, 08:46:43 pm »
Homework: Write a program which will randomly generate [n] of numbers. Then find lowest and 2nd lowest number, if there will be more of same numbers it will count them.

Random numbers:58,21,8,16,21,2,8,17,41,23,24

Answer should look like this: First minimum is 8 and its count is 2.
                                          Secound minimum is 2 and its count is 1.
There is a thread in this forum that solves a problem conceptually identical to the one you've been assigned.  It is https://forum.lazarus.freepascal.org/index.php/topic,46103.0.html  there are a number of solutions, it would be good for you to have a look at them.

HTH.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

 

TinyPortal © 2005-2018