Solved

Trying to count a list of random numbers.

Posted on 2001-07-30
12
175 Views
Last Modified: 2010-04-06
Hi All,
While working with a list of 4 digit random numbers, I parse the random number into seperate digits to be tallied by counters until some digit appears 21 times. Using the function Halt seems to wipe the list and everything off the form. Is there a better reserved  word to use so that once  I stop that I can resume tallying until the next digit total 21 happens to be reported?

Delphi3
0
Comment
Question by:delphi3
12 Comments
 
LVL 21

Expert Comment

by:gemarti
ID: 6335608
Why not create an exception?
Halt terminates the procedure completely.
0
 
LVL 4

Expert Comment

by:YodaMage
ID: 6335730
You can raise an exception, you can test a condition, then use your test result as an event.

You can use "break", though it is considered BAD programming.
0
 
LVL 6

Expert Comment

by:edey
ID: 6335744
Better yet put your code in a seperate thread & call Suspend/Resume

GL
Mike
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 5

Expert Comment

by:scrapdog
ID: 6336664
You may be confusing 'Halt' for 'Exit'.

Halt terminates the *program* abnormally.

Exit exits the current procedure block.  This is semantically equivalent to jumping straight to the "end" in the current procedure.
0
 
LVL 2

Expert Comment

by:FrodoBeggins
ID: 6336694
Well, sure 'Halt' is like 'TerminateProcess(...)' for your process. Very bad idea.

Exit is better. it is not 'semantically equivalent to jumping straight to the "end" in the current procedure', but many times it is. Scrapdog, think about what happens if you call 'exit' from within a try...finaly block :).
Anyway, if your list is not in the stack (e.g. it is global variable) you can't loose it.

But I would suggest putting the routine in separate thread. Then it will generate and count numbers/digits and that is all it will do. Be aware not to touch the list from the main thread while it is updated by the worker thread. You may synchronize using TCriticalSection

Rgds,
Frodo
0
 
LVL 3

Expert Comment

by:rondi
ID: 6336866
There's a variety of options available to you.
exit; break; goto...; raise...;    etc...

I suggest you post a little code snippet so someone
can recommend the most appropriate method.

rondi.
0
 
LVL 4

Author Comment

by:delphi3
ID: 6337514
Hi All,
Thanks for responding

As someone wanted to see what was going on. Here it is
This is more dirty (not very pretty) programming than I really care to show.
but it does the job of counting.

unit EEPermStudyUnit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;
const
  MinValue = 123;
  MaxValue = 9876;
  ValueRange = MaxValue - MinValue + 1;
type
  TValues = array[0..ValueRange - 1] of integer;
  TAlpha = array[0..ValueRange - 1] of string;
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    procedure RandomValues(var Values: TValues; MinValue, MaxValue: integer);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  values: TValues;
begin
  RandomValues(values, MinValue, MaxValue);
end;

procedure TForm1.RandomValues(var Values: TValues; MinValue, MaxValue: integer);
{ Populate an array with all the values between MinValue and MaxValue,
 in a random order. }
var
  IA, i, j, tmp, ans, Totl, T: integer;
  Tz1, T11, T21, T31, T41, T51, T61, T71, T81, T91: integer;
  Tz2, T12, T22, T32, T42, T52, T62, T72, T82, T92: integer;
  Tz3, T13, T23, T33, T43, T53, T63, T73, T83, T93: integer;
  Tz4, T14, T24, T34, T44, T54, T64, T74, T84, T94: integer;
  HoldItT: string;
  HoldItI: integer;
  Ais: string;
begin
 // Populate the array with values in order
  for i := 0 to ValueRange - 1 do
    Values[i] := MinValue + i;

 // Shuffle the array of values
  for i := 0 to ValueRange - 1 do begin
   // Choose a random position with which to swap
    j := Random(ValueRange);
   // swap values at indices i and j (unless they are the same index)
    if (i <> j) then begin
      tmp := Values[i];
      Values[i] := Values[j];
      Values[j] := tmp;
    end;
  end;
  T := 0;  // tz1 --> teacher zero one first quarter,
  Tz1 := 0; T11 := 0; T21 := 0; T31 := 0; T41 := 0; T51 := 0; T61 := 0; T71 := 0; T81 := 0; T91 := 0;
  Tz2 := 0; T12 := 0; T22 := 0; T32 := 0; T42 := 0; T52 := 0; T62 := 0; T72 := 0; T82 := 0; T92 := 0;
  Tz3 := 0; T13 := 0; T23 := 0; T33 := 0; T43 := 0; T53 := 0; T63 := 0; T73 := 0; T83 := 0; T93 := 0;
  Tz4 := 0; T14 := 0; T24 := 0; T34 := 0; T44 := 0; T54 := 0; T64 := 0; T74 := 0; T84 := 0; T94 := 0;

  for i := 1 to 600 do // and some  random will be rejected so make enough
  begin
    AIS := inttostr(Values[i]);
    if length(AIS) < 4 then AIS := '0' + AIS;
    // get rid of the doubles in a 'word' as in  1223 or 1231
    if (AIS[1] = AIS[2]) or (AIS[1] = AIS[3]) or (AIS[1] = AIS[4]) or (AIS[2] = AIS[3]) or (AIS[2] = AIS[4]) or (AIS[3] = AIS[4]) then
    begin // true
    end
    else
    begin
      T := T + 1;  // a count of how many are OK
      Memo1.lines.add(AIS);
     //first digit Teacher 0..9 and a quarter 1
      if Ais[1] = '0' then begin //teacher '0', first quarter
        Tz1 := Tz1 + 1;
        if tz1 = 21 then begin HoldItT := 'Teacher Z1'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[1] = '1' then begin //teacher '1', first quarter
        T11 := T11 + 1;
        if t11 = 21 then begin HoldItT := 'Teacher11'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[1] = '2' then begin  //teacher '2', first quarter
        T21 := T21 + 1;
        if t21 = 21 then begin HoldItT := 'Teacher21'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI)); Halt;end;
      end;
      if Ais[1] = '3' then begin   //teacher '3', first  quarter
        T31 := T31 + 1;
        if t31 = 21 then begin HoldItT := 'Teacher31'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[1] = '4' then begin   //teacher '4', first  quarter
        T41 := T41 + 1;
        if t41 = 21 then begin HoldItT := 'Teacher41'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[1] = '5' then begin
        T51 := T51 + 1;
        if t51 = 21 then begin HoldItT := 'Teacher51'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[1] = '6' then begin
        T61 := T61 + 1;
        if t61 = 21 then begin HoldItT := 'Teacher61'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[1] = '7' then begin
        T71 := T71 + 1;
        if t71 = 21 then begin HoldItT := 'Teacher71'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[1] = '8' then begin
        T81 := T81 + 1;
        if t81 = 21 then begin HoldItT := 'Teacher81'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[1] = '9' then begin
        T91 := T91 + 1;
        if t91 = 21 then begin HoldItT := 'Teacher91'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;

       //Check the second digit
       //second  digit Teacher 0..9 and a quarter 2

      if Ais[2] = '0' then begin  //teacher '0',second quarter
        Tz2 := Tz2 + 1;
        if tz2 = 21 then begin HoldItT := 'TeacherZ2'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[2] = '1' then begin  //teacher '1',second quarter
        T12 := T12 + 1;
        if t12 = 21 then begin HoldItT := 'Teacher12'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[2] = '2' then begin
        T22 := T22 + 1;
        if t22 = 21 then begin HoldItT := 'Teacher22'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[2] = '3' then begin
        T32 := T32 + 1;
        if t32 = 21 then begin HoldItT := 'Teacher32'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[2] = '4' then begin
        T42 := T42 + 1;
        if t42 = 21 then begin HoldItT := 'Teacher42'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[2] = '5' then begin
        T52 := T52 + 1;
        if t52 = 21 then begin HoldItT := 'Teacher52'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[2] = '6' then begin
        T62 := T62 + 1;
        if t62 = 21 then begin HoldItT := 'Teacher62'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[2] = '7' then begin
        T72 := T72 + 1;
        if t72 = 21 then begin HoldItT := 'Teacher72'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[2] = '8' then begin
        T82 := T82 + 1;
        if t82 = 21 then begin HoldItT := 'Teacher82'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[2] = '9' then begin
        T92 := T92 + 1;
        if t92 = 21 then begin HoldItT := 'Teacher92'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;

        // Check the 3rd digit
 

      if Ais[3] = '0' then begin //teacher '0',Third quarter
        Tz3 := Tz3 + 1;
        if tz3 = 21 then begin HoldItT := 'TeacherZ3'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[3] = '1' then begin
        T13 := T13 + 1;
        if t13 = 21 then begin HoldItT := 'Teacher13'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI)); end;
      end;
      if Ais[3] = '2' then begin
        T23 := T23 + 1;
        if t23 = 21 then begin HoldItT := 'Teacher23'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[3] = '3' then begin
        T33 := T33 + 1;
        if t33 = 21 then begin HoldItT := 'Teacher33'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[3] = '4' then begin
        T43 := T43 + 1;
        if t43 = 21 then begin HoldItT := 'Teacher43'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[3] = '5' then begin
        T53 := T53 + 1;
        if t53 = 21 then begin HoldItT := 'Teacher53'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[3] = '6' then begin
        T63 := T63 + 1;
        if t63 = 21 then begin HoldItT := 'Teacher63'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[3] = '7' then begin
        T73 := T73 + 1;
        if t73 = 21 then begin HoldItT := 'Teacher73'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[3] = '8' then begin
        T83 := T83 + 1;
        if t83 = 21 then begin HoldItT := 'Teacher83'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[3] = '9' then begin
        T93 := T93 + 1;
        if t93 = 21 then begin HoldItT := 'Teacher93'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;

     // Check the 4th digit
 
      if Ais[4] = '0' then begin //teacher '0',fourth quarter
        Tz4 := Tz4 + 1;
        if tz4 = 21 then begin HoldItT := 'TeacherZ4'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[4] = '1' then begin
        T14 := T14 + 1;
        if t14 = 21 then begin HoldItT := 'Teacher14'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[4] = '2' then begin
        T24 := T24 + 1;
        if t24 = 21 then begin HoldItT := 'Teacher24'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[4] = '3' then begin
        T34 := T34 + 1;
        if t34 = 21 then begin HoldItT := 'Teacher34'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[4] = '4' then begin
        T44 := T44 + 1;
        if t44 = 21 then begin HoldItT := 'Teacher44'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[4] = '5' then begin
        T54 := T54 + 1;
        if t54 = 21 then begin HoldItT := 'Teacher54'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[4] = '6' then begin
        T64 := T64 + 1;
        if t64 = 21 then begin HoldItT := 'Teacher64'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI)); Halt;end;
      end;
      if Ais[4] = '7' then begin
        T74 := T74 + 1;
        if t74 = 21 then begin HoldItT := 'Teacher74'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[4] = '8' then begin
        T84 := T84 + 1;
        if t81 = 21 then begin HoldItT := 'Teacher84'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI));Halt; end;
      end;
      if Ais[4] = '9' then begin
        T94 := T94 + 1;
        if t94 = 21 then begin HoldItT := 'Teacher94'; HoldItI := T;
          Memo1.lines.add(HoldItT + ' ' + IntToStr(HoldItI)); Halt;end;
      end;
     //Whew !!!

    end;
  end;


  Memo1.lines.add('TeacherZ1' + ' ' + IntToStr(Tz1));
  Memo1.lines.add('TeacherZ2' + ' ' + IntToStr(Tz2));
  Memo1.lines.add('TeacherZ3' + ' ' + IntToStr(Tz3));
  Memo1.lines.add('TeacherZ4' + ' ' + IntToStr(Tz4));
  Memo1.lines.add('Teacher11' + ' ' + IntToStr(T11));
  Memo1.lines.add('Teacher12' + ' ' + IntToStr(T12));
  Memo1.lines.add('Teacher13' + ' ' + IntToStr(T13));
  Memo1.lines.add('Teacher14' + ' ' + IntToStr(T14));
  Memo1.lines.add('Teacher21' + ' ' + IntToStr(T21));
  Memo1.lines.add('Teacher22' + ' ' + IntToStr(T22));
  Memo1.lines.add('Teacher23' + ' ' + IntToStr(T23));
  Memo1.lines.add('Teacher24' + ' ' + IntToStr(T24));
  Memo1.lines.add('Teacher31' + ' ' + IntToStr(T31));
  Memo1.lines.add('Teacher32' + ' ' + IntToStr(T32));
  Memo1.lines.add('Teacher33' + ' ' + IntToStr(T33));
  Memo1.lines.add('Teacher34' + ' ' + IntToStr(T34));
  Memo1.lines.add('Teacher41' + ' ' + IntToStr(T41));
  Memo1.lines.add('Teacher42' + ' ' + IntToStr(T42));
  Memo1.lines.add('Teacher43' + ' ' + IntToStr(T43));
  Memo1.lines.add('Teacher44' + ' ' + IntToStr(T44));
  Memo1.lines.add('Teacher51' + ' ' + IntToStr(T51));
  Memo1.lines.add('Teacher52' + ' ' + IntToStr(T52));
  Memo1.lines.add('Teacher53' + ' ' + IntToStr(T53));
  Memo1.lines.add('Teacher54' + ' ' + IntToStr(T54));
  Memo1.lines.add('Teacher61' + ' ' + IntToStr(T61));
  Memo1.lines.add('Teacher62' + ' ' + IntToStr(T62));
  Memo1.lines.add('Teacher63' + ' ' + IntToStr(T63));
  Memo1.lines.add('Teacher64' + ' ' + IntToStr(T64));
  Memo1.lines.add('Teacher71' + ' ' + IntToStr(T71));
  Memo1.lines.add('Teacher72' + ' ' + IntToStr(T72));
  Memo1.lines.add('Teacher73' + ' ' + IntToStr(T73));
  Memo1.lines.add('Teacher74' + ' ' + IntToStr(T74));
  Memo1.lines.add('Teacher81' + ' ' + IntToStr(T81));
  Memo1.lines.add('Teacher82' + ' ' + IntToStr(T82));
  Memo1.lines.add('Teacher83' + ' ' + IntToStr(T83));
  Memo1.lines.add('Teacher84' + ' ' + IntToStr(T84));
  Memo1.lines.add('Teacher91' + ' ' + IntToStr(T91));
  Memo1.lines.add('Teacher92' + ' ' + IntToStr(T92));
  Memo1.lines.add('Teacher93' + ' ' + IntToStr(T93));
  Memo1.lines.add('Teacher94' + ' ' + IntToStr(T94));
end;

end;



procedure TForm1.Button2Click(Sender: TObject);
begin
  Application.Terminate;
end;

end.

Delphi3
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6337665
Insert an Action Item.
Create a new action

Change all Halt statements to Action1.Execute;

Add this to the Action1.Execute

procedure TForm1.Action1Execute(Sender: TObject);
begin
 ShowMessage('Number Reached');
end;
0
 
LVL 4

Author Comment

by:delphi3
ID: 6338332
gemarti,
I have d4 the standard edition and Win2k with NT. I can access an actionlist1. but Action1 is not available to me. I have never used either words please explain if it is simply something that I should know. More details would  be appreciated.

Delphi3
 
0
 
LVL 21

Accepted Solution

by:
gemarti earned 50 total points
ID: 6338639
Sorry for the delay. I had to pull out my copy of D4 and Install it on a machine.

Put the actionlist component on the form.
Double click the component and a form will pop up. It will have a split view. On the left you'll see Categories and on the right its blank.

right click your mouse on the right side of the actionlist form. Select "New Action". A action1 item should appear.

if you double click on the word action1 it will take you to the code editor event. This is where you will insert your code. In my example I used ShowMessage(). You may use anything you like.

To replace all the HALT commands do this:

From the Delphi 4 Menu select SEARCH |REPLACE

Type in Halt
on the next line "replace line" type in Action1.Execute;

Now when the app runs it'll list out your information and then stop when it reaches 21. At this point it will get kicked out to Action1.Execute, It'll do something there and then resume where it left your program.

GEM

0
 
LVL 4

Author Comment

by:delphi3
ID: 6338964
GEM,

You are GREAT  !!!! this  Action procedure is  a real eye opener for me. The first time through it, that is your explanation  WORKED!!!!  I am impressed !!!!

Delphi3
0
 
LVL 21

Expert Comment

by:gemarti
ID: 6339915
Anytime.

Happy Programming! :-)

GEM
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 89
add combobox item based on numbers 9 141
Run video youtube webbrowse 10 61
oracle global variables 4 68
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question