Solved

question about a type in pascal

Posted on 2003-10-24
5
158 Views
Last Modified: 2010-05-18
Hi,
it's a simple question i know but it's the base of all my program
i have to take a random number out of 2, 3, 5 and 7
how do i proceed ???

temp:=Random(2, 3, 5, 7);
doesn't seem to work

a type ??
TYPE
 Primes=(2, 3, 5, 7);
a get a message error asking for a type or something like that
"Identifier Expected"

tell me please
thanks a lot
 
0
Comment
Question by:Elvis1
  • 3
  • 2
5 Comments
 
LVL 100

Accepted Solution

by:
mlmcc earned 50 total points
ID: 9619469
I would generate a random number between 1-4 and use that as an index into an array with those values

type
     P_array =  array[1..4] of integer;

var
     Rand_Index  : integer;
     Primes : P_array;


begin
     Primes [1] := 2;
     Primes [2] := 3;
     Primes [3] := 5;
     Primes [4] := 7;

     randomize;
     Rand_index := 4*random +1;

     writeln (Primes[Rand_Index]);

end.

mlmcc
   
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9620211
Good answer, mlmcc. However, I would suggest some minor alterations:

1) use an "P_array = array[0..3] of Integer;".
2) use "Rand_index := Random(4);"

And perhaps the Pascal version used can define arrays of constants like this:

const
  Primes : P_array = (2, 3, 5, 7);

Good answer, though. You deserve all credit in my opinion. ;-)
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 9621731
Glad I could help


Alex:
I grew up with arrays being 1-based so I tend to keep them like that.

Does Random(4) give a number from 0 to 3.999999?

I don't think Pascal allows constant arrays but it might.  Been too long since I used them.

mlmcc
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 9627699
mlmcc:
I know, in Basic arrays used to be 1-based. However, when I learned Pascal a couple of decades ago I was told that arrays have user-definable upper and lower bounds. I do prefer to use 0-based arrays, though. Don't know why but it's just a preference...

Random(4) returns either 0, 1, 2 or 3. Nothing more, nothing less. At least in Turbo Pascal and Delphi.

I know Delphi allows constant arrays. I even use them quite often. I'm not sure but I think Turbo Pascal allows constant arrays too. But it's too long ago that I wrote my last TP application.
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 9630911
Alex: In VB arrays are 0-based.  I learned in FORTRAN IV.  I used to teach Pascal and we generally used arrays as 1 based.  I know you can set the index to be anything (one of the features of Pascal I really like).

mlmcc

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query issue 13 402
Embarcadero Interbase 9 747
Regular Expression to Split a String 7 106
how to manage invalidate between two tvirtualstringtree in same form? 1 104
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A short film showing how OnPage and Connectwise integration works.

930 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now