Pascal syntax error 85 - pascal

First off, I'm new to programming and I've just started learning pascal. I've encountered an error 85: ";" expected. I searched through the whole thing multiple times but I haven't been able to find the problem. Any suggestions?
Here's the code:
program test;
var
a,b,c:real;
begin
D:=sqr(b)-4*a*c;
writeln('Enter a value for a');
readln(a);
writeln('Enter a value for b');
readln(b);
writeln('Enter a value for c');
readln(c);
if ( D<0 ) then
begin
writeln('There is no solution.');
else
if ( D>0 ) then
begin
x1:=(-b+sqrt(D))/2*a;
x2:=(-b-sqrt(D))/2*a;
writeln('x1 is:');
writeln('x1:=',x1);
writeln(x2 is:);
writeln('x2:=',x2);
end;
end.

You have three begin and only two end statements. Indent your code and you would notice your mistake. Variable D, X1, and X2 are also undefined. There are other syntax errors in your output, ie, missing tic marks 'in one of your writeln statements near the end.

And you need a end before the else ..
program test;
var
a,b,c:real;
begin
D:=sqr(b)-4*a*c;
writeln('Enter a value for a');
readln(a);
writeln('Enter a value for b');
readln(b);
writeln('Enter a value for c');
readln(c);
if ( D<0 ) then
begin
writeln('There is no solution.');
end
else
if ( D>0 ) then
begin
x1:=(-b+sqrt(D))/2*a;
x2:=(-b-sqrt(D))/2*a;
writeln('x1 is:');
writeln('x1:=',x1);
writeln(x2 is:);
writeln('x2:=',x2);
end;
end.

Related

PL/SQL - how to repeat alternating text for a given number of times with a final output?

I want to make a procedure called OE that will have text output based on the number that I define.
For example, inputting the number 6 will give the following output:
odd
even
odd
even
odd
even
= even steven!
and inputting the number 5 will give the following output:
odd
even
odd
even
odd
= you oddball!
I'm completely new at this and have been struggling to get the odd number to load correctly (for some reason, it gets stuck in an infinite loop). Any help would be appreciated! Here is what I got so far:
CREATE OR REPLACE procedure oe
(p_n IN number)
AS
v_n number;
v_on number;
BEGIN
v_n := p_n;
v_on := p_n;
IF v_n>0 THEN LOOP
dbms_output.put_line('odd');
v_n := v_n-1;
dbms_output.put_line('even');
v_n := v_n-1;
If v_n=0 then
exit;
if v_on mod 2 > 0 then dbms_output.put_line('=' || ' you oddball!');
exit;
else
dbms_output.put_line('=' || ' even steven!');
exit;
end if;
end if;
end loop;
end if;
END;
/
You are not using exit conditions properly hence your code is going in infinite loop. You simplify your logic as below. Let me know it it works for you.
You may add few validations to make sure you get proper input parameters such as p_n > 0 and other.
CREATE OR REPLACE procedure oe
(p_n IN number)
AS
begin
for i in 1..p_n
loop
if mod(i,2)=1 then dbms_output.put_line('odd');
else dbms_output.put_line('even');
end if;
end loop;
if mod(p_n,2)=1 then dbms_output.put_line('= you oddball!');
else dbms_output.put_line('= even steven!');
end if;
end;
hemalp108 has already answered this, but I just wanted to add that you don't even need the if/else logic that fills the procedure (except perhaps for handling values less than 1, which I'll leave as an exercise), because we have case:
create or replace procedure oe
( p_n in number )
as
begin
for i in 1 .. p_n loop
dbms_output.put_line(case mod(i,2) when 1 then 'odd' else 'even' end);
end loop;
dbms_output.put_line(case mod(p_n,2) when 1 then '= you oddball!' else '= even steven!' end);
end;
(You may also notice how laying your code out neatly is half the way towards debugging it.)

Backtracking not showing anything

I want to find all the posibilities of moving around these numbers 1,2,3,4,5 with the conditions that "4,3" is not possible and 1 and 5 can't be in the same solution. I have this code but when I compile it it doesn't show me anything.
var x:array[1..50] of integer;
i,k:integer;
avems,evalid:boolean;
procedure init;
begin
x[k]:=0;
end;
procedure succesor;
begin
if x[k]<5 then avems:=true
else avems:=false;
if avems=true then x[k]:=x[k]+1;
end;
function solutie:boolean;
begin
if k=3 then solutie:=true
else solutie:=false;
end;
procedure valid;
begin
evalid:=true;
for i:=1 to k-1 do
if (x[i]=1) or (x[i]=5) and (x[k]=1) or (x[k]=5) then evalid:=false;
if (k>1) and (x[k-1]=4) and (x[k]=3) then evalid:=false;
end;
procedure tipar;
begin
for i:=1 to 3 do
write(x[i],' ');
writeln;
end;
begin
k:=1;
init;
while k>0 do
begin
repeat
succesor;
until not(avems) or (avems and evalid);
if avems then
if solutie then tipar
else begin
k:=k+1;
init;
end
else k:=k-1;
end;
end.
What do you expect? Your code increments x[1] until it reaches 5, then sets avems:=false; and then decrements k to zero and exits the while loop.
Remember that Pascal does not use indentation for blocks like Python, so your
last steps are written more clearly as
if avems then begin
if solutie then tipar
else begin
k:=k+1;
init;
end
end
else k:=k-1;
If this is not what you want, you have to code some begin/end.

Convert real to Integer in loop in Pascal

I'm new in Pascal and I don't know how a fix this error:
Incompatible types: got "S80REAL" expected "LONGINT"
My code is:
Var
number1:Integer;
a,b:Integer;
a,i:Integer;
procedure number(number1: Integer);
begin
a:=1;
b:=number1+(number1-1);
for a:=1 to number1 do
begin
for i:=1 to ((b-a)/2) do
begin
write('#');
end;
end;
end;
Error is here: for i:=1 to ((b-a)/2) do
Thank you for help.
Replace the '/' ( (b-a)/2 ) with 'div'
'/' is real division in Pascal, 'div' is integer division
procedure number(number1: Integer);
begin
a:=1;
b:=number1 + number1 - 1;
for a:=1 to number1 do
begin
for i:=1 to ((b-a) div 2) do
begin
write('#');
end;
end;
end;

Cannot find the error in my code: ";expected"

When i'm running the code it says that there's an syntax error and it marks me the else: Fatal: Syntax Error, ;expected but ELSE found.(btw this is just one part from my program) can anyone tell me what am i doing wrong?
If D>0 then
begin
x1:=(-b)+sqrt(D)/(2*a);
x2:=(-b)-sqrt(D)/(2*a);
Writeln(x1,x2);
else
if D=0 then
begin
x:=(-b)/(2*a);
Writeln(x);
end;
try
If D>0 then
begin
x1:=(-b)+sqrt(D)/(2*a);
x2:=(-b)-sqrt(D)/(2*a);
Writeln(x1,x2);
end
else
if D=0 then
begin
x:=(-b)/(2*a);
Writeln(x);
end;
I believe you need one more end; tag as you have two if begin statements
If D>0 then
begin
x1:=(-b)+sqrt(D)/(2*a);
x2:=(-b)-sqrt(D)/(2*a);
Writeln(x1,x2);
end
else
if D=0 then
begin
x:=(-b)/(2*a);
Writeln(x);
end;

How does this program to count vowels work?

I want to understand this code, especially PROCEDURE
PROGRAM vowels;
USES crt;
{Program that counts the number of vowels in a sentence}
CONST space=' ';
maxchar=80;
TYPE vowel=(a,e,i,o,u);
VAR buffer:ARRAY[1..maxchar] of char;
vowelcount:ARRAY[vowel] of integer;
PROCEDURE initialize;
VAR ch:vowel;
BEGIN
FOR ch:=a TO u DO
BEGIN
vowelcount[ch]:=0;
END;
END;
PROCEDURE textinput;
VAR index:integer;
BEGIN
writeln('Input a sentence');
FOR index:=1 TO maxchar DO
IF eoln THEN buffer[index]:=space
ELSE read(buffer[index]);
readln;
END;
PROCEDURE analysis;
VAR index:integer;
ch:vowel;
BEGIN
index:=1;
WHILE index<>maxchar+1 DO
BEGIN
IF buffer[index] IN ['a','e','i','o','u'] THEN
BEGIN
CASE buffer[index] OF
'a':ch:=a;
'e':ch:=e;
'i':ch:=i;
'o':ch:=o;
'u':ch:=u;
END;
vowelcount[ch]:=vowelcount[ch]+1;
END;
index:=index+1;
END;
END;
PROCEDURE vowelout;
VAR ch:vowel;
BEGIN
clrscr;
writeln;
writeln(' a e i o u');
FOR ch:=a TO u DO
write(vowelcount[ch]:4);
writeln;
END;
BEGIN
initialize;
textinput;
analysis;
vowelout;
END;
Overall: Okay this code is counting the number of vowels supplied in the input string.
Lets Begin....
TYPE vowel=(a,e,i,o,u); VAR
buffer:ARRAY[1..maxchar] of char;
vowelcount:ARRAY[vowel] of integer;
This code is defining a list of the vowels in english (a,e,i,o,u).
PROCEDURE initialize; VAR ch:vowel;
BEGIN FOR ch:=a TO u DO BEGIN
vowelcount[ch]:=0; END; END;
It then defines a variable to collect the number of each vowel, called vowelcount. That variable is an array, looks sort of like this:
vowelcount[a]=0;
vowelcount[e]=0;
vowelcount[i]=0; #... etc
Then the procedure "Analysis" is defined. This takes the input from the screen (which will be called later on in the program) and steps through each letter in the input.
WHILE index<>maxchar+1 DO BEGIN IF
buffer[index] IN ['a','e','i','o','u']
THEN BEGIN CASE buffer[index] OF
'a':ch:=a; 'e':ch:=e; 'i':ch:=i;
'o':ch:=o; 'u':ch:=u; END;
If any of those letters happens to be in the list of letters than matches a vowel, then it will add one to the number in the vowelcount array above. (vowelcount[ch]:=vowelcount[ch]+1) where ch is the matched letter. As you can see this is only triggered if it is a valid vowel (IF buffer[index] IN ['a','e','i','o','u'] )
Finally. The main code of the program, or what is actually run:
BEGIN clrscr; writeln; writeln(' a e i
o u'); FOR ch:=a TO u DO
write(vowelcount[ch]:4); writeln; END;
BEGIN initialize; textinput; analysis;
vowelout; END.
This basically strings the application together, starting by clearing the screen (in a dos prompt) and then outputting the vowels onto the screen. It then adds some formatting and outputs the current count of vowelcount (as above).
It will then request your input and finally it will output the contents of vowelcount again, which has been updated with the vowelcounts from the input you made.

Resources