at the time i am in the process of learning pascal but i have some question that google can't answer, so i am trying to do a program that analyzes how much overweight does a person have relating to a set of data. the thing is that i don;t know how to make another window or to clear the window so that only the results appear in the screen, My teacher wants something like this (Don't mind the spanish ;D ) :
And Here is my code so far:
Program AddNums(output);
uses crt;
var
s, b, a, v: integer;
begin
clrscr;
GotoXY(20, 1);
writeln('Ejercicio 2 - Actividad 2');
GotoXY(25, 3);
writeln('Calculadora de Pesos');
write('Introduce la cantidad de personas para analizar sus pesos: ');
readln(s);
for a := 1 to s do
begin
writeln('Introduzca el peso de la persona numero', a, ':');
readln(v)
end;
clrscr;
if (v <= 90) then
write('Cantidad de personas con Sobrepeso: ...........', v + 1);
else if (v <= 30) then
write('Cantidad de personas con Bajo Peso: ...........', v + 1);
else if (v <= 30 AND v >= 90) then
write('Cantidad de Personas con Peso normal: ...........', v + 1);
else
write('null');
end.
And this
is the error that i am encountering right now
(35,4) Fatal: Syntax error, ";" expected but "ELSE" found
Fatal: Compilation aborted
Hope you guys help me !!
The last few lines of your code need to be corrected as shown below in order to compile, and then you need to check that the code does what you want it to. The program should then compile, and assuming you are running it in an environment like Lazarus, the IDE for Free Pascal, it will appear in a console window as #TomBrunberg has commentedThere were basically two problems with it:
The first three elses were preceded by a semicolon (;). That is a syntax error. Each else is part of an if ... then ...else block. The only circumstances when a semicolon is permitted is when it is inside a compound statement, for example a begin ... end block, as in
begin
DoSomething;
end
else
write('Cantidad de personas con Sobrepeso: ...........', v + 1)
The other problem was that you had
(v <= 30 AND v >= 90)
The syntax problem with that is that in Pascal, and has higher operator precedence than <= so the compiler tries, and fails, to evaluate 30 and v. It fails because the result should be of type Boolean, which it cannot be. To avoid that problem, you need to close the parenthesese, like this
(v <= 30) and (v >= 90)
Then, the expressions on either side of the and both evaluate to Booleans, so the compiler can successfully combine then with and. However, as #Tom has pointed out in a comment, the combined expression can never evaluate to True because v cannot be less or equal to 30 and greater or equal to 90 at the same time: perhaps you meant OR?
Corrected code
if (v <= 90) then
write('Cantidad de personas con Sobrepeso: ...........', v + 1)
else if (v <= 30) then
write('Cantidad de personas con Bajo Peso: ...........', v + 1)
else if (v <= 30) AND (v >= 90) then
write('Cantidad de Personas con Peso normal: ...........', v + 1)
else
write('null');
readln;
The readln at the end makes the program wait for you to press the Enter so that it stays on-screen long enough for you to see it - without the readln, the console window would just close and vanish.
Related
Cant obtain a correct answer it sums incorectly and multiplicates too, i must use repeat when solving this problem, i suppose the problem is in sum:=i+i but i dont know how to solve it
program SAD;
uses crt;
var a, i, sum, prod: integer;
begin
clrscr;
sum:=0;
prod:=0;
{Sum}
repeat
for i:=1 to 26 do
if i mod 4 = 0 then sum:=i+i;
until i = 26;
{Multiplication}
repeat
for a:=1 to 26 do
if a mod 4 = 0 then prod:=a*a;
until a = 26;
writeln('Suma numerelor divizate la 4 este:', sum);
writeln('Produsul numerelor divizate la 4 este:', prod);
end.
I think the instruction "use repeat" probably intends that you should avoid using for as well.
There are a few errors in your code:
In the sum loop, you should add i to sum, not to itself.
In the prod loop, since you set prod to zero at the start, it will stay as zero because zero times anything is zero. So you need to adjust the logic of your prod calculation so that if prod is zero, when the mod 4 condition is satisfied, you set prod to the current value of a, otherwise you multiply it by a.
Here is some code which fixes the above points and avoids the use of for.
program Sad;
uses crt;
var
a, i, sum, prod: integer;
begin
clrscr;
sum:=0;
prod:=0;
{Sum}
i := 0;
repeat
inc(i);
if (i mod 4) = 0 then
sum := sum + i;
until i = 26;
{Multiplication}
a :=0;
repeat
inc(a);
if a mod 4 = 0 then begin
if prod = 0 then
prod := a
else
prod := prod * a;
end;
until a = 26;
writeln('Suma numerelor divizate la 4 este:', sum);
writeln('Produsul numerelor divizate la 4 este:', prod);
readln;
end.
Please how do I rewrite the below FOR....DO loop into a WHILE...DO loop in Pascal programming
Below is the following code
Program Matmuli(input,output);
:
:
FOR i:=1 TO m DO
FOR j:=1 TO p DO
BEGIN
C[i,j]:= 0.0;
FOR k:=1 TO n DO
C[i,j]:= C[i,j] + A[i,k] * B[k,j];
END;
The for loop is well defined in terms of a while loop, confer ISO 7185 “Standard Pascal”, quote:
Apart from the restrictions imposed by these requirements, the for-statement
for v := e1 to e2 do body
shall be equivalent to
begin
temp1 := e1;
temp2 := e2;
if temp1 <= temp2 then
begin
v := temp1;
body;
while v <> temp2 do
begin
v := succ(v);
body
end
end
end
[…]
where temp1 and temp2 denote auxiliary variables that the program does not otherwise contain, and that possess the type possessed by the variable v […]
The important thing from this expanded piece of code is, as linuxfan says Reinstate Monica already noted, that in Pascal the limits of a for loop are only evaluated once. Furthermore, the control-variable is only assigned a value if there was indeed at least one iteration.
However, usually it’s sufficient to transform a for loop as Andreas Rejbrand already suggested, although it is technically not the same.
program Hello;
var
a,b,c,x,d: integer;
x1,x2: real;
begin
readln(a,b,c);
if a = 0 then
begin
if b = 0 then
begin
if c = 0 then
begin
writeln('11');
end
else
writeln('21');
end;
end
else
writeln('31');
end;
end
else
d := b^2 - 4*a*c;
if d < 0 then
begin
writeln('Нет Вещественных корней!');
end
else
x1 := (-b + sqrt(d))/(2*a);
x2 := (-b - sqrt(d))/(2*a);
writeln('Первый Корень:' + x1 + ' ' + 'Второй Корень:' + x2);
end;
end;
end.
The reason for this is that your begins and ends are not balanced; disregarding the opening begin and closing end. for the program's syntax to be correct, you should have equal numbers of each, but you have 4 begins and 8 ends.
Obviously, your code is to compute the solutions of a quadratic equation. What I think you should do is to adjust the layout of your code so that it reflects that and then correctly the begins and ends. In particular, your program is trying to detect whether any of a, b and d is zero and, if so, write a diagnostic message, otherwise calculate the roots by the usual formula.
Unfortunately, your begins and ends do not reflect that. Either the whole of the block starting d := ... needs to be executed or none of it does, so the else on the line before needs to be followed by a begin, as in
else begin
d := b*b - 4*a*c; //b^2 - 4*a*c;
if d < 0 then begin
writeln('Нет Вещественных корней!');
end
else begin
x1 := (-b + sqrt(d))/(2*a);
x2 := (-b - sqrt(d))/(2*a);
// writeln('Первый Корень:' + x1 + ' ' + 'Второй Корень:' + x2);
writeln('Первый Корень:', x1, ' Второй Корень:' , x2);
end;
end;
(You don't say which Pascal compiler you are using, but the above fixes two points which are flagged as errors in FreePascal.
If you need more help than that, please ask in a comment.
Btw, there are some grammatical constructs in Pascal implementations where an end can appear without a matching preceding begin such as case ... of ...end.
I have to code a program in pascal that, given the three coefficients of a polynomial(ax²+bx+c), outputs its roots.
Here's what I have right now:
program poly;
type
polynomial = record
a, b, c : real;
end;
procedure readPolynomial (var p : polynomial);
begin
writeln ('Input 1st coefficient: ');
readln (p.a);
writeln ('Input 2nd coefficient: ');
readln (p.b);
writeln ('Input 3rd coefficient: ');
readln (p.c);
end;
function square (x : real) : real;
begin
square := x * x;
end;
procedure roots (p : polynomial; var rP, rN : real);
begin
rP := (-p.b + (sqrt((square(p.b)) - (4 * p.a * p.c)))) / (2 * p.a);
rN := (-p.b - (sqrt((square(p.b)) - (4 * p.a * p.c)))) / (2 * p.a);
writeln('The roots are: ', rP:0:3, ' y ' ,rN:0:3);
end;
var
myPolynomial : polynomial;
r1, r2 : real;
begin
writeln ('Enter the coefficients: ');
readPolynomial (myPolynomial);
roots (myPolynomial, r1, r2);
end.
It works fine for real roots but I don't know how to make it work with complex numbers.
I am assuming your coefficients are real numbers (they user can't enter complex numbers as coefficients). That would add a whole new level of complexity (no pun intended) to the problem.
You need to check the discriminant ((square(p.b)) - (4 * p.a * p.c)) to see if it's less than 0. Currently, your code just does, sqrt((square(p.b)) - (4 * p.a * p.c)) but you aren't checking if you are taking the square root of a negative number (which you can't do using the Pascal sqrt library function).
If the discriminant is negative, then you have a complex root and you can separate the real and imaginary parts as you wish in your program. It's basic quadratic formula.
For example:
procedure roots (p : polynomial; var rP, rN : real);
var disc: real;
begin
disc := square(p.b) - 4*p.a*p.c;
if disc >= 0 then begin
rP := (-p.b + sqrt(disc)) / (2 * p.a);
rN := (-p.b - sqrt(disc)) / (2 * p.a);
writeln('The roots are: ', rP:0:3, ' y ' ,rN:0:3);
end
else begin
// Roots are:
// -p.b/(2*p.a) + (sqrt(-disc)/(2*p.a))i
// -p.b/(2*p.a) - (sqrt(-disc)/(2*p.a))i
end
end;
Here you use the fact that sqrt(x) if x is negative would be, (sqrt(-x))i where i is sqrt(-1). Note that you could also split out the disc = 0 case to avoid repeating a double root.
Since your roots function prints out the results and your main program doesn't use the returned arguments rN and rP, it's not clear to me if you need to pass back the roots at all. But if want to pass the roots back as arguments (the way you have your function currently designed), I'll leave that as an exercise. You just have to decide on a representation for complex roots. One way is to use the Complex number type for the results (if your compiler library supports them), and when the results are real, the imaginary parts will just be zero. Alternatively, if you need to create your own, just make a type which is a record consisting of a real and imaginary part.
type complex = record
re: real;
im: real;
end;
I need a code to go from decimal to binary numbers, but my program shows them invert, example: needs to show 1011000 but it brings out 0001101.
+ I cant use massives and array in this program.
var
x,y,i:longint;
BEGIN
readln(y);
repeat
x:= y mod 2;
y:= y div 2;
write(x);
until y = 0;
END.
I think you can use recursion function. For example:
procedure dec2bin(y)
BEGIN
x := y mod 2;
y := y mod 2;
if y > 1 then
dec2bin(y)
end
write(x)
END
BEGIN
readln(y);
dec2bin(y)
END.
I'm not sure in correct syntax because I working with Pascal long time ago. But I think you can understand my idea and make this.
This should answer what you are looking for
function decimalToBinary(a:LongInt):String;
var d:Integer;
str:String;
Begin
str:='';
while a>0 do begin
d:=a mod 2;
str:=concat(IntToStr(d),str);
a:=a div 2;
end;
decimalToBinary:=str;
End;
This is my code. It works!
program convert;
var
number : integer;
procedure dec2bin(x : integer);
begin
// general case
if (x > 1) then dec2bin(x div 2);
// to print the result
if (x mod 2 = 0) then write('0')
else write('1');
end;
begin
write('Decimal: '); readln(number);
write('Binary : ');
dec2bin(number);
end.