André Oliveira - 3 years ago 301

Pascal Question

I started learning Pascal :) and I was interested on making a kind of Euromillion... However, I don't know how to forbid the same numbers or stars...

I thought this (below) would solve it... But it didn't... Help?

`Program euromillion;`

var num: array [1..5] of integer;

Procedure numbers;

var i, j: integer;

Begin

write ('Digite o número 1: ');

readln (num[1]);

for i:=2 to 5 do

for j:=1 to (i-1) do

Begin

repeat

write ('Digite o número ', i, ': ');

readln (num[i]);

until (num[i]>=1) and (num[i]<=50) and ((num[i]=num[j])=false);

End;

End;

Begin

numbers;

readln();

End.

Thanks guys :)

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

Although it is tempting to try and write a single block of code, as you have, it is better not to. Instead, a better way to write a program like this is to think about splitting the task up into a number of procedures or functions each of which only does a single part of the task.

One way to look at your task is to split it up into sub-tasks, as follows:

- You prompt the user to enter a series of numbers
- Once each number is entered, you check whether it is already in the array If it isn't, you enter it in the array, otherwise prompt the user for another number
- Once the array is filled, you output the numbers in the array

So, a key thing is that it would be helpful to have a function that checks whether a new number is already in the array and returns True if it is and False otherwise. How to do that is the answer to your question. You need to be careful about this because if you use the array a second time in the program, you need to avoid comparing the new number with the array contents from the previous time. I deliberately have not solved that problem in the example code below, to leave it as an exercise for the reader. Hint: One way would be to write a procedure which "clears" the array before each use of it, e.g. by filling it with numbers which are not valid lottery numbers, like negative numbers or zero. Another way would be to define a record which includes the NumberArray and a Count field which records how many numbers have been entered so far: this would avoid comparing the new number to all the elements in the array and allow you to re-use the array by resetting the Count field to zero before calling ReadNumbers.

```
program LotteryNumbers;
uses crt;
type
TNumberArray = array[1..5] of Integer;
var
Numbers : TNumberArray;
Number : Integer;
function IsInArray(Number : Integer; Numbers : TNumberArray) : Boolean;
var
i : Integer;
begin
Result := False;
for i:= Low(Numbers) to High(Numbers) do begin
if Numbers[i] = Number then begin
Result := True;
break;
end;
end
end;
procedure ReadNumbers(var Numbers : TNumberArray);
var
i : Integer;
NewNumber : Integer;
OK : Boolean;
begin
// Note: This function needs to have a check added to it that the number
// the user enters is a valid lottery number, in other words that the
// number is between 1 and the highest ball number in the lottery
for i := Low(Numbers) to High(Numbers) do begin
repeat
OK := False;
writeln('enter a number');
ReadLn(NewNumber);
OK := not IsInArray(NewNumber, Numbers);
if not OK then
writeln('Sorry, you''ve already chosen ', NewNumber);
until OK;
Numbers[i] := NewNumber;
end;
end;
procedure ListNumbers(Numbers : TNumberArray);
var
i : Integer;
begin
for i := Low(Numbers) to High(Numbers) do
writeln(Numbers[i]);
end;
begin
ReadNumbers(Numbers);
ListNumbers(Numbers);
writeln('press any key');
readkey;
end.
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**