Enumerated type and registers - pascal

I'm having difficulties with some code, basically I need to fill 3 vectors that work as data storage, where every vector position correspond to a specific province, I'm sorry about the variable names being in spanish.
CONST
F = 24;
TYPE
T_vector = array [1..F] of Real;
subrango = 1..F;
registro_infectados = record
name, email, date_of_birth,estado: string[30];
street, city, provincia: string[30];
street_number, pc,: integer;
id,phone_number:real;
end;
T_archivo_infectados = FILE OF registro_infectados;
PROCEDURE INFECTADOS_POR_PROVINCIA(VAR arch:T_archivo_infectados; VAR vect_activos:t_vector; VAR vect_fallecidos: t_vector; VAR vect_recuperados:t_vector); // This will read every register, count the "reg.state" and sum 1 to the corresponding vector.
var
lim:subrango;
REG:registro_infectados;
Begin
Inicializar_Vector(vect_activos, vect_fallecidos, vect_recuperados, lim); //This iniatiate the vectors.
RESET(ARCH);
Begin
WHILE NOT(EOF(ARCH)) DO
BEGIN
READ(ARCH, REG);
case reg.estado of
'activo':begin
case reg.provincia of
'Entre Rios': vect_activos[1]:= vect_activos[1] + 1 ;
'Buenos Aires':vect_activos[2]:= vect_activos[2] + 1;
'Cordoba': vect_activos[3]:= vect_activos[3] + 1;
'Corrientes': vect_activos[4]:= vect_activos[4] + 1 ;
'Misiones': vect_activos[5]:= vect_activos[5] + 1 ;
'Formosa': vect_activos[6]:= vect_activos[6] + 1 ;
'Chaco': vect_activos[7]:= vect_activos[7] + 1 ;
'Jujuy': vect_activos[8]:= vect_activos[8] + 1 ;
'Salta': vect_activos[9]:= vect_activos[9] + 1 ;
'Tucuman': vect_activos[10]:= vect_activos[10] + 1 ;
'La Rioja': vect_activos[11]:= vect_activos[11] + 1 ;
'Santiago del Estero': vect_activos[12]:= vect_activos[12] + 1 ;
'Catamarca': vect_activos[13]:= vect_activos[13] + 1 ;
'La Pampa': vect_activos[14]:= vect_activos[14] + 1 ;
'Santa Fe': vect_activos[15]:= vect_activos[15] + 1 ;
'Mendoza': vect_activos[16]:= vect_activos[16] + 1 ;
'San Juan': vect_activos[17]:= vect_activos[17] + 1 ;
'Rio Negro': vect_activos[18]:= vect_activos[18] + 1 ;
'Tierra del Fuego': vect_activos[19]:= vect_activos[19] + 1 ;
'Santa Cruz': vect_activos[20]:= vect_activos[20] + 1 ;
'Chubut': vect_activos[21]:= vect_activos[21] + 1 ;
'San Luis': vect_activos[22]:= vect_activos[22] + 1 ;
'Neuquen': vect_activos[23]:= vect_activos[23] + 1 ;
'CABA': vect_activos[24] := vect_activos[24] + 1;
end;
end;
'fallecido': begin
case reg.provincia of
'Entre Rios': vect_fallecidos[1]:= vect_fallecidos[1] + 1 ;
'Buenos Aires':vect_fallecidos[2]:= vect_fallecidos[2] + 1;
'Cordoba': vect_fallecidos[3]:= vect_fallecidos[3] + 1;
'Corrientes': vect_fallecidos[4]:= vect_fallecidos[4] + 1 ;
'Misiones': vect_fallecidos[5]:= vect_fallecidos[5] + 1 ;
'Formosa': vect_fallecidos[6]:= vect_fallecidos[6] + 1 ;
'Chaco': vect_fallecidos[7]:= vect_fallecidos[7] + 1 ;
'Jujuy': vect_fallecidos[8]:= vect_fallecidos[8] + 1 ;
'Salta': vect_fallecidos[9]:= vect_fallecidos[9] + 1 ;
'Tucuman': vect_fallecidos[10]:= vect_fallecidos[10] + 1 ;
'La Rioja': vect_fallecidos[11]:= vect_fallecidos[11] + 1 ;
'Santiago del Estero': vect_fallecidos[12]:= vect_fallecidos[12] + 1 ;
'Catamarca': vect_fallecidos[13]:= vect_fallecidos[13] + 1 ;
'La Pampa': vect_fallecidos[14]:= vect_fallecidos[14] + 1 ;
'Santa Fe': vect_fallecidos[15]:= vect_fallecidos[15] + 1 ;
'Mendoza': vect_fallecidos[16]:= vect_fallecidos[16] + 1 ;
'San Juan': vect_fallecidos[17]:= vect_fallecidos[17] + 1 ;
'Rio Negro': vect_fallecidos[18]:= vect_fallecidos[18] + 1 ;
'Tierra del Fuego': vect_fallecidos[19]:= vect_fallecidos[19] + 1 ;
'Santa Cruz': vect_fallecidos[20]:= vect_fallecidos[20] + 1 ;
'Chubut': vect_fallecidos[21]:= vect_fallecidos[21] + 1 ;
'San Luis': vect_fallecidos[22]:= vect_fallecidos[22] + 1 ;
'Neuquen': vect_fallecidos[23]:= vect_fallecidos[23] + 1 ;
'CABA': vect_fallecidos[24] := vect_fallecidos[24] + 1;
end;
end;
'recuperado': begin
case reg.provincia of
'Entre Rios': vect_recuperados[1]:= vect_recuperados[1] + 1 ;
'Buenos Aires':vect_recuperados[2]:= vect_recuperados[2] + 1;
'Cordoba': vect_recuperados[3]:= vect_recuperados[3] + 1;
'Corrientes': vect_recuperados[4]:= vect_recuperados[4] + 1 ;
'Misiones': vect_recuperados[5]:= vect_recuperados[5] + 1 ;
'Formosa': vect_recuperados[6]:= vect_recuperados[6] + 1 ;
'Chaco': vect_recuperados[7]:= vect_recuperados[7] + 1 ;
'Jujuy': vect_recuperados[8]:= vect_recuperados[8] + 1 ;
'Salta': vect_recuperados[9]:= vect_recuperados[9] + 1 ;
'Tucuman': vect_recuperados[10]:= vect_recuperados[10] + 1 ;
'La Rioja': vect_recuperados[11]:= vect_recuperados[11] + 1 ;
'Santiago del Estero': vect_recuperados[12]:= vect_recuperados[12] + 1 ;
'Catamarca': vect_recuperados[13]:= vect_recuperados[13] + 1 ;
'La Pampa': vect_recuperados[14]:= vect_recuperados[14] + 1 ;
'Santa Fe': vect_recuperados[15]:= vect_recuperados[15] + 1 ;
'Mendoza': vect_recuperados[16]:= vect_recuperados[16] + 1 ;
'San Juan': vect_recuperados[17]:= vect_recuperados[17] + 1 ;
'Rio Negro': vect_recuperados[18]:= vect_recuperados[18] + 1 ;
'Tierra del Fuego': vect_recuperados[19]:= vect_recuperados[19] + 1 ;
'Santa Cruz': vect_recuperados[20]:= vect_recuperados[20] + 1 ;
'Chubut': vect_recuperados[21]:= vect_recuperados[21] + 1 ;
'San Luis': vect_recuperados[22]:= vect_recuperados[22] + 1 ;
'Neuquen': vect_recuperados[23]:= vect_recuperados[23] + 1 ;
'CABA': vect_recuperados[24] := vect_recuperados[24] + 1;
end;
end;
end;
end;
end;
End;
The code is working just fine, but I'm afraid is too long and I can't come up with a solution to make it shorter.

Whats the actual problem,as We dont have all the functions
All your parameters are declared as variables, which means that, all new values will be in the same varables, and as you know procedures do not return* values.
Example:
procedure INFECTED_BY_PROVINCE(VAR X : Integer);
begin
X := 12345; // will set the incoming variable to that value
end;
VAR
TestVar : Integer;
begin
TestVar := 0; /// Set X to 0
INFECTED_BY_PROVINCE(TestVar);
writeln(TestVar); // Output/TestVar will be 12345;
end.

Instead of repeating the case REG.provincia of block for each REG.estado, do it only once and assign a variable, e.g. indx: integer a value that corresponds to the province:
var
lim: subrango;
REG: registro_infectados;
indx: integer; // new variable
case REG.provincia of
'Entre Rios': indx := 1;
'Buenos Aires': indx := 2;
'Cordoba': indx := 3;
//
//... fill in all other provinces
//
'CABA': indx := 24;
end;
Now you can simplify the assignment to the vectors for the three states (REG.estado) using the province indx to just a few lines:
case REG.estado of
'activo': inc(vect_activos[indx]);
'fallecido': inc(vect_fallecidos[indx]);
'recuperado': inc(vect_recuperados[indx]);
end;
As you see, using inc() also shortens the assignment.

Related

Pascal - Sum of odd numbers between 0 and X

I've beeng having some trouble with this code... I need to create an algorithm which makes the user input a number (X), and then the program calculates the sum of all the odd numbers below (x).
This what I've tried so far, but can't really wrap my head around the logic behind it:
Program odd_numbers;
Var
Num, Limite, Soma: integer;
Begin;
Soma := 0;
Writeln('Choose a limit:');
Readln(Limite);
While (Limite / 2 > 0) do
Begin;
Soma := ((Num < Limite) mod 2 > 0);
Writeln('The sum of odd numbers from 0 to ', Limite, ' é ', Soma);
End;
if (Limite mod 2 = 0) then
Begin;
Soma := ((Num < Limite) mod 2 = 0);
Writeln('The sum of odd numbers from 0 to ', Limite, ' é ', Soma);
End;
End.
*PS: Been writing the code with variables in Portuguese, so don't mind the variables appearing weird to understand. *
I see that everyone is happily looping, but this is not necessary. This is a simple arithmetic sequence, and the sum can be calculated without a loop.
Just think of the following:
1 + 3 = 2 * (1 + 3) / 2 = 2 * 2 = 4 ; limits 3 and 4
1 + 3 + 5 = 3 * (1 + 5) / 2 = 3 * 3 = 9 ; limits 5 and 6
1 + 3 + 5 + 7 = 4 * (1 + 7) / 2 = 4 * 4 = 16 ; limits 7 and 8
1 + 3 + 5 + 7 + 9 = 5 * (1 + 9) / 2 = 5 * 5 = 25 ; limits 9 and 10
1 + 3 + 5 + 7 + 9 + 11 = 6 * (1 + 11) / 2 = 6 * 6 = 36 ; limits 11 and 12
But not only that, you'll see that it is in fact always a perfect square: Sqr((n+1) div 2).
So just calculate:
program odd_numbers;
var
Num, Limite, Soma: Integer;
begin
Write('Choose a limit: ');
Readln(Limite);
Num := (Limite + 1) div 2;
Soma := Num * Num;
Writeln('The sum of odd numbers from 0 to ', Limite, ' is ', Soma);
end.
Looks a little simpler than what the others propose.
The loop While (Limite / 2 > 0) do ... uses real arithmetic and not integer arithmetic. I guess you mean While (Limite div 2 > 0) do ... And you should change Limite in the loop otherwise you get stuck because the exit condition can never be reached.
After you have asked the user to enter a number, Limite, you need to keep that unchanged, because you need it in the final message. You also need a loop where you go through all numbers from Limite towards 0.
You started with a while loop which is ok, you are just missing the loop control variable. That is a variable that eventually gets a terminating value which then stops the loop. Use for example the Num variable you already have declared. You can use the same variable to investigate the numbers between user input and 0, for being odd values.
num := limite-1; // give num a start value based on user input (-1 because of "... numbers below (x)")
while num > 0 do // stop the loop when 0 is reached
begin
// here you investigate if `num` is a odd number (e.g. using `mod` operator or
// possibly your pascal has a built in `function Odd(value: integer): boolean;`)
// and add it to `Soma` if it is
num := num - 1;// decrement num at every iteration
end;
Finally you need to consider changes to the above, to handle negative input from the user.
To test if an integer is an odd value, you could use following function:
function IsOdd( value : Integer) : Boolean;
begin
IsOdd := (value mod 2) <> 0;
end;
Many pascal compilers have a built-in function called Odd(), which you could use.
A while loop works well to solve this problem. If you start with lowest odd number above zero, i.e. one and continue upwards so long we do not exceed the limit value we have a simple start:
function GetOddSumBelowX( X : Integer) : Integer;
var
i,sum: Integer;
begin
i := 1; // Start with first odd number
sum := 0;
while (i < X) do begin // as long as i less than X, loop
if IsOdd(i) then begin
sum := sum + i; // add to sum
end;
i := i + 1; // Increment i
end;
GetOddSumBelowX := sum;
end;
Now, that was simple enough. Next step to simplify the loop is to increment the i variable by two instead, just to jump between all odd numbers:
function GetOddSumBelowX( X : Integer) : Integer;
var
i,sum: Integer;
begin
i := 1; // Start with first odd number
sum := 0;
while (i < X) do begin // as long as i less than X, loop
sum := sum + i; // add to sum
i := i + 2; // Increment to next odd number
end;
GetOddSumBelowX := sum;
end;

oracle Converting ISBN10 to ISBN13

I need to convert ISBN from 10 digits to 13 digits. I tried but not getting correct result.This is the code that I have tried and its not giving correct result.
Create or replace FUNCTION F_ISBN_CONV_13 (ISBN_10 IN VARCHAR2) RETURN VARCHAR2
AS
V_ISBN_13 VARCHAR2(13);
BEGIN
SELECT
CASE WHEN LENGTH(ISBN_10) = 10 THEN
CASE WHEN SUBSTR(ISBN_10,1,3) = '801' THEN
'201' ||
SUBSTR(ISBN_10,1,9) ||
SUBSTR(
(
10 -
SUBSTR(
(
(
2 +
1 +
SUBSTR(ISBN_10,2,1) +
SUBSTR(ISBN_10,4,1) +
SUBSTR(ISBN_10,6,1) +
SUBSTR(ISBN_10,8,1)
) +
(
(
0 +
SUBSTR(ISBN_10,1,1) +
SUBSTR(ISBN_10,3,1) +
SUBSTR(ISBN_10,5,1) +
SUBSTR(ISBN_10,7,1) +
SUBSTR(ISBN_10,9,1)
)
) * 3
)
,-1
,1
)
)
,-1
,1
)
ELSE
'978' ||
SUBSTR(ISBN_10,1,9) ||
SUBSTR(
(
10-
SUBSTR(
(
(
9 +
8 +
SUBSTR(ISBN_10,2,1) +
SUBSTR(ISBN_10,4,1) +
SUBSTR(ISBN_10,6,1) +
SUBSTR(ISBN_10,8,1)
) +
(
(
7 +
SUBSTR(ISBN_10,1,1) +
SUBSTR(ISBN_10,3,1) +
SUBSTR(ISBN_10,5,1) +
SUBSTR(ISBN_10,7,1) +
SUBSTR(ISBN_10,9,1)
)
) * 3
)
,1
,1
)
)
,-1
,1
)
END
ELSE
ISBN_10
END INTO V_ISBN_13
FROM
DUAL;
RETURN V_ISBN_13;
EXCEPTION
--< code >
END F_ISBN_CONV_13;
I think the following code should be self-explanatory. For details see the Wikipedia links.
-- https://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-10_to_ISBN-13_conversion
create or replace function isbn10_to_13(p_isbn10 in varchar2)
return varchar2 is
v_isbn10_digits constant varchar2(9) := substr(replace(p_isbn10, '-', ''), 1, 9);
v_isbn13 varchar2(17) := '978-' || substr(p_isbn10, 1, length(p_isbn10) - 1);
v_checkdigit number;
begin
-- https://en.wikipedia.org/wiki/International_Standard_Book_Number#ISBN-13_check_digit_calculation
v_checkdigit :=
(1 * 9)
+ (3 * 7)
+ (1 * 8)
+ (3 * to_number(substr(v_isbn10_digits, 1, 1)))
+ (1 * to_number(substr(v_isbn10_digits, 2, 1)))
+ (3 * to_number(substr(v_isbn10_digits, 3, 1)))
+ (1 * to_number(substr(v_isbn10_digits, 4, 1)))
+ (3 * to_number(substr(v_isbn10_digits, 5, 1)))
+ (1 * to_number(substr(v_isbn10_digits, 6, 1)))
+ (3 * to_number(substr(v_isbn10_digits, 7, 1)))
+ (1 * to_number(substr(v_isbn10_digits, 8, 1)))
+ (3 * to_number(substr(v_isbn10_digits, 9, 1)))
;
v_checkdigit := 10 - mod(v_checkdigit, 10);
if v_checkdigit = 10
then
v_checkdigit := 0;
end if;
return v_isbn13 || v_checkdigit;
end;
/
Example
col isbn10 for a13
col isbn13 for a17
with isbn10(i10) as (
select '0-30-640615-x' from dual union all
select '0-07-223065-7' from dual union all
select '0-596-51446-x' from dual
)
select isbn10.i10 as isbn10, isbn10_to_13(isbn10.i10) as isbn13
from isbn10;
ISBN10 ISBN13
------------- -----------------
0-30-640615-x 978-0-30-640615-7
0-07-223065-7 978-0-07-223065-9
0-596-51446-x 978-0-596-51446-4
I have got the correct result.
Create or replace FUNCTION F_ISBN_CONV_13 (ISBN_10 IN VARCHAR2) RETURN VARCHAR2
AS
V_ISBN_13 VARCHAR2(13);
BEGIN
SELECT
CASE WHEN LENGTH(ISBN_10) = 10 THEN
CASE WHEN SUBSTR(ISBN_10,1,3) = '801' THEN
'201' ||
SUBSTR(ISBN_10,1,9) ||
SUBSTR(
(
10 -
SUBSTR(
(
(
2 +
1 +
SUBSTR(ISBN_10,2,1) +
SUBSTR(ISBN_10,4,1) +
SUBSTR(ISBN_10,6,1) +
SUBSTR(ISBN_10,8,1)
) +
(
(
0 +
SUBSTR(ISBN_10,1,1) +
SUBSTR(ISBN_10,3,1) +
SUBSTR(ISBN_10,5,1) +
SUBSTR(ISBN_10,7,1) +
SUBSTR(ISBN_10,9,1)
)
) * 3
)
,-1
,1
)
)
,-1
,1
)
ELSE
'978' ||
SUBSTR(ISBN_10,1,9) ||
SUBSTR(
(
10-
SUBSTR(
(
(
9 +
8 +
SUBSTR(ISBN_10,2,1) +
SUBSTR(ISBN_10,4,1) +
SUBSTR(ISBN_10,6,1) +
SUBSTR(ISBN_10,8,1)
) +
(
(
7 +
SUBSTR(ISBN_10,1,1) +
SUBSTR(ISBN_10,3,1) +
SUBSTR(ISBN_10,5,1) +
SUBSTR(ISBN_10,7,1) +
SUBSTR(ISBN_10,9,1)
)
) * 3
)
,-1 -- Need to change here from '1' to '-1'
,1
)
)
,-1
,1
)
END
ELSE
ISBN_10
END INTO V_ISBN_13
FROM
DUAL;
RETURN V_ISBN_13;
EXCEPTION
--< code >
END F_ISBN_CONV_13;

Why is my PASCAL program not running?

Program TypeofCreditCard;
Var
AppliName: array[1..99] of String;
SSnum: array[1..99] of Integer;
GSal: array[1..99] of Integer;
TSalD: array[1..99] of Integer;
Name, CC : String;
Rep,Exp, GS,NS,Sum,TSD , YS,SSN,i, ,PofIncome : integer ;
YS, C_Amt: Real
Begin
Writeln ( 'Enter applicants who applied for a type of credit card');
Readln (Rep,Exp,GS,YS,NS,Sum,TSD,SSN,CC,PofIncome) ;
While ( Name <> ' Stop ' ) do
Begin
NS:= GS-TSD ;
Sum:= Exp + Rep ;
PofIncome:=(NS * 0.45);
Begin
If ( GS >4000) AND ( CC = 'Bronze Card' ) then
YS:= GS * 12 ;
C_Amt := YS * 0.25;
i:= i + 1;
AppliName [i]:= Name;
SSNum [i]:= SSN ;
GSal [i]:= GS ;
TSalD [i]:= TSD ;
End ;
Begin
If (GS >= 7500) AND (CC= 'Gold Card') then
YS:= GS * 12 ;
C_Amt:= YS * 0.3;
i:= i + 1 ;
AppliName [i]:= Name;
SSnum [i]:=SSN;
GSal [i]:=GS;
TSalD [i]:=TSD;
End;
Begin
If (GS>=10,000) AND ( CC = ' Platinum Card') then
YS := GS * 12;
C_Amt: = YS * 0.4;
i:= i + 1;
AppliName [i]:= Name;
SSNum [i]:= SSN;
GSal [i]:=GS;
TSalD [i]:= TSD;
End if
End if
End if
End While
End.
I am using free IDE Pascal and it is saying that I have 2 errors. If there are anymore errors or you see anything strange please inform me. The error says : Fatal:Syntax Error, ";" expected but "identifier Writeln found - Source of error- Writeln ( 'Enter applicants who applied for a type of credit card');
There's a ; missing after YS, C_Amt: Real. Also after each then should be a beginand all End if and End while at the end should all be End;

Magic square error in visual basic 6.0

I'm developing a program in visual basic 6.0 to display magic square. I've developed the logic, but the values are not getting displayed in the magic square. Here's the code :
Private Sub Command1_Click()
Dim limit As Integer
Dim a(100, 100) As Integer
limit = InputBox("Enter the limit")
If limit Mod 2 = 0 Then ' Rows and columns must be
MsgBox "Can't be done", vbOKCancel, "Error"
Else ' set number of rows and columns to limit
mfgsquare.Rows = limit
mfgsquare.Cols = limit
j = (n + 1) / 2
i = 1
For c = 1 To n * n
mfgsquare.TextMatrix(i, j) = c
If c Mod n = 0 Then
i = i + 1
GoTo label
End If
If i = 1 Then
i = n
Else
i = i - 1
End If
If j = n Then
j = 1
Else
j = j + 1
End If
label:
Next c
End If
End Sub
Try this:
n = InputBox("Enter the limit")
If n Mod 2 = 0 Then ' Rows and columns must be
MsgBox "Can't be done"
Else ' set number of rows and columns to limit
mfgsquare.Rows = n + 1
mfgsquare.Cols = n + 1
For i = 1 To n
For j = 1 To n
mfgsquare.TextMatrix(i, j) = n * ((i + j - 1 + Int(n / 2)) Mod n) + ((i + 2 * j - 2) Mod n) + 1
Next j
Next i
End If

Pascal beginner program

Program q2;
Var input: integer ;
Var repeatvar: char ;
Procedure display(input : integer) ;
Begin
If (input = 9) then
Write ('********************************************************') ;
Write ('9! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 362880 ');
Write ('********************************************************') ;
If (input = 8) then
Write ('********************************************************') ;
Write ('8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320 ');
Write ('********************************************************') ;
If (input = 7) then
Write ('********************************************************') ;
Write ('7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040 ');
Write ('********************************************************') ;
If (input = 6) then
Write ('********************************************************') ;
Write ('6! = 6 x 5 x 4 x 3 x 2 x 1 = 720 ');
Write ('********************************************************') ;
If (input = 5) then
Write ('********************************************************') ;
Write ('5! = 5 x 4 x 3 x 2 x 1 = 120 ');
Write ('********************************************************') ;
If (input = 4) then
Write ('********************************************************') ;
Write ('4! = 4 x 3 x 2 x 1 = 24 ');
Write ('********************************************************') ;
If (input = 3) then
Write ('********************************************************') ;
Write ('3! = 3 x 2 x 1 = 6 ');
Write ('********************************************************') ;
If (input = 2) then
Write ('********************************************************') ;
Write ('2! = 2 x 1 = 2 ');
Write ('********************************************************') ;
If (input = 1) then
Write ('********************************************************') ;
Write ('1! = 1 = 1 ');
Write ('********************************************************') ;
If (input = 0) then
Write ('********************************************************') ;
Write ('0! = 1 = 1 ');
Write ('********************************************************') ;
End ;
Begin
While (repeatvar = 'y') do
Begin
Write('Enter an integer between 0 and 9 or -1 to quit');
readln(input);
If (input = -1) then
begin
Write('Sentinel value - Exiting program !');
Readln;
end
Else
display(input);
End
End.
This is the warning I get after compiling it .
Free Pascal Compiler version 2.4.0 [2010/05/18] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling q2.pas
q2.pas(61,8) Warning: Variable "repeatvar" does not seem to be initialized
Linking q2
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
78 lines compiled, 0.2 sec
1 warning(s) issued
I am typing in q2 but cannot see any output , it's not doing anything. I have tried a number of things , also I am totally new to Pascal , and fpc . Can anyone tell me what's the problem ?
In the part of the repeatvar , i wanted to implement this :
i wanted to implement this c++ code :
do
{
cout << "Enter an integer between 0 and 9 or -1 to quit => " ;
cin >> input ; // get number
if(input == -1)
{
cout << "Sentinel value - Exiting program !" << endl;
exit ;
system("pause");
return 0;
}
else
{
// display function
display(input) ;
}
cout << endl;
cout << " Press y to repeat , any other key and enter to terminate: " ;
cin >> repeat ;
if(repeat != 'y')
exit;
}while( repeat == 'y');
Because the repeatvar is never initialized, your program never enters the while loop, because there is no way for repeatvar to be 'y' to begin with.
You forgot to initialize the variable
repeatvar := 'y'
insert the above line before the lines
While (repeatvar = 'y') do
Begin
...
End
i hope that the correction of code
program Project1;
Var input: integer ;
Var repeatvar: char ;
Procedure display(input : integer) ;
Begin
If (input = 9) then
begin
Writeln ('********************************************************') ;
Writeln ('9! = 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 362880 ');
Writeln ('********************************************************') ;
end;
If (input = 8) then
begin
Writeln ('********************************************************') ;
Writeln ('8! = 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 = 40320 ');
Writeln ('********************************************************') ;
end;
If (input = 7) then
begin
Writeln ('********************************************************') ;
Writeln ('7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040 ');
Writeln ('********************************************************') ;
end;
If (input = 6) then
begin
Writeln ('********************************************************') ;
Writeln ('6! = 6 x 5 x 4 x 3 x 2 x 1 = 720 ');
Writeln ('********************************************************') ;
end;
If (input = 5) then
begin
Writeln ('********************************************************') ;
Writeln ('5! = 5 x 4 x 3 x 2 x 1 = 120 ');
Writeln ('********************************************************') ;
end;
If (input = 4) then
begin
Writeln ('********************************************************') ;
Writeln ('4! = 4 x 3 x 2 x 1 = 24 ');
Writeln ('********************************************************') ;
end;
If (input = 3) then
begin
Writeln ('********************************************************') ;
Writeln ('3! = 3 x 2 x 1 = 6 ');
Writeln ('********************************************************') ;
end;
If (input = 2) then
begin
Writeln ('********************************************************') ;
Writeln ('2! = 2 x 1 = 2 ');
Writeln ('********************************************************') ;
end;
If (input = 1) then
begin
Writeln ('********************************************************') ;
Writeln ('1! = 1 = 1 ');
Writeln ('********************************************************') ;
end;
If (input = 0) then
begin
Writeln ('********************************************************') ;
Writeln ('0! = 1 = 1 ');
Writeln ('********************************************************') ;
end;
End ;
Begin
repeat
writeln('enter number between 1-9 or -1 to quit: ');
readln(input);
display(input);
until input=-1;
End.

Resources