Pascal ABC repeat untill - pascal

There is a code
program roots;
var a, b, c, d,x1, x2,x:real;
begin
writeln('Введите коэффиценты квадратного уравнения');
write('a='); readln(a);
write('b=');readln(b);
write('c=');readln(c);
begin
while a=0 then
writeln('');
repeat
else
until
if a=0
then
if b=0
then
if c=0
then writeln('Любое x - решение')
else writeln('Нет решений')
else
begin
x:=-c/b;
writeln('x=',x)
end
else
begin
d:=b*b-4*a*c;
if d<0
then writeln('Нет вещенственный корней')
else
begin
x1:=(-b+sqrt(d))/2/a;
x2:=(-b-sqrt(d))/2/a;
writeln('x1=',x1);
writeln('x2=',x2)
end
end
end.
you need to remake it so that when you enter a = 0, the program writes that the value is incorrect and asks you to enter a again, by repeat
it is desirable to make it faster

A repeat .. until loop has the following form:
repeat
<code to perform in the loop>
until <condition to terminate the loop>
So note that the code you need to repeat is between repeat and until and the termination condition is after until.
In your code you have these lines:
...
repeat
else
until
...
There is no else as part of the repeat .. until. However, the code block within the loop might of course contain conditional statements with an else.

Related

Password with 3 different characters from the previous password - PL/SQL

So my task consists in a password with 3 different characters from the previous password in PL/SQL.
Here is the code:
IF old_password IS NOT NULL THEN
differ := length(old_password) - length(password);
differ := abs(differ);
IF length(password) < length(old_password) THEN
m := length(password);
ELSE
m := length(old_password);
END IF;
FOR i IN 1..m LOOP
IF substr(password,i,1) != substr(old_password,i,1) THEN
differ := differ + 1;
END IF;
END LOOP;
IF differ < 3 THEN
raise_application_error(-20011, 'Password should differ from the \
old password by at least 3 characters');
END IF;
END IF;
-- Everything is fine; return TRUE ;
RETURN(TRUE);
If my old password is "aaa" and I change it for "aaa222", it gives me an error. That's fine.
But if my old password is "aaa" and I change it for "aaa2222", it doesn't give me an error I don't know why. This should give me an error..
What's wrong? Any solutions?
From the code you have posted your results do not make sense, I think you have a typo in your posted code (see my comments below) , but lets analyse what the code is actually doing.
obvious
IF old_password IS NOT NULL THEN
These two lines in your first case set the difference to 3 and the second case to 4 as the strings are different lengths
differ := length(old_password) - length(password);
differ := abs(differ);
This block in either case selects the shorter of the two passwords so will in both cases return 3 (inital password of aaa)
IF length(password) < length(old_password) THEN
m := length(password);
ELSE
m := length(old_password);
END IF;
You loop through the length of the short password and compare it character by character with the longer password in both the first three characters are aaa so match so you will not get into the if statment
FOR i IN 1..m LOOP
IF substr(password,i,1) != substr(old_password,i,1) THEN
differ := differ + 1;
END IF;
END LOOP;
In you first case differ will be 3 and in your second case 4 so this will not evaluate to true unless you have <= rather than < (this is where I think you have a typo in the code you have posted)
IF differ < 3 THEN
raise_application_error(-20011, 'Password should differ from the \
old password by at least 3 characters');
END IF;
END IF;
Return true in both cases.
-- Everything is fine; return TRUE ;
RETURN(TRUE);

Ada - Skipping Whitespace using look_ahead

I have a procedure, that in theory, should be skipping whitespace using a look_ahead loop. Problem is, it's not working, if there's any whitespace in the input file, it is adding it to the array of records. I think my logic is correct, but could use another pair of eyes to let me know what I'm missing, and why it's not working.
PROCEDURE Read(Calc: OUT Calculation) IS
EOL: Boolean;
C: Character;
I: Integer := 1;
BEGIN
LOOP
LOOP
Look_Ahead(C, EOL);
EXIT WHEN EOL or C /= ' ';
Get(C);
END LOOP;
EXIT WHEN ADA.Text_IO.END_OF_FILE;
Look_Ahead(C, EOL);
IF Is_Digit(C) THEN
Calc.Element(I).Kind := Number;
Get(Calc.Element(I).Int_Value);
ELSE
Calc.Element(I).Kind := Symbol;
Get(Calc.Element(I).Char_Value);
END IF;
Calc.Len := Calc.Len+1;
IF Calc.Element(I).Char_Value = '=' THEN
EXIT;
END IF;
I := I+1;
END LOOP;
END Read;
EDIT: If any of the other procedures, the code for the record etc is needed for an answer, let me know and I will post it.
For Ada.Text_IO.Look_Ahead, ARM A.10.7(8) says
Sets End_Of_Line to True if at end of line, including if at end of page or at end of file; in each of these cases the value of Item is not specified. Otherwise, End_Of_Line is set to False and Item is set to the next character (without consuming it) from the file.
(my emphasis) and I think the "without consuming it" is key. Once Look_Ahead has found an interesting character, you need to call Get to retrieve that character.
I hacked this little demo together: I left end-of-file to exception handling, and I called Skip_Line once end-of-line’s been seen because just Get wasn’t right (sorry not to be more precise!).
with Ada.Text_IO;
with Ada.IO_Exceptions;
procedure Justiciar is
procedure Read is
Eol: Boolean;
C: Character;
begin
-- Instead of useful processing, echo the input to the output
-- replacing spaces with periods.
Outer:
loop
Inner:
loop
Ada.Text_IO.Look_Ahead (C, Eol);
exit Outer when Eol; -- C is undefined
exit Inner when C /= ' ';
Ada.Text_IO.Get (C); -- consume the space
Ada.Text_IO.Put ('.'); -- instead of the space for visibility
end loop Inner;
Ada.Text_IO.Get (C); -- consume the character which isnt a space
Ada.Text_IO.Put (C); -- print it (or other processing!)
end loop Outer;
Ada.Text_IO.Skip_Line; -- consume the newline
Ada.Text_IO.New_Line; -- clear for next call
end Read;
begin
loop
Ada.Text_IO.Put ("reading: ");
Read;
end loop;
exception
when Ada.IO_Exceptions.End_Error =>
null;
end Justiciar;
Usually it's better to read an entire line and parse it than to try to parse character by character. The latter is usually more complex, harder to understand, and more error prone. So I'd suggest something like
function De_Space (Source : String) return String is
Line : Unbounded_String := To_Unbounded_String (Source);
begin -- De_Space
Remove : for I in reverse 1 .. Length (Line) loop
if Element (Line, I) = ' ' then
Delete (Source => Line, From => I, Through => I);
end if;
end loop Remove;
return To_String (Line);
end De_Space;
Line : constant String := De_Space (Get_Line);
You can then loop over Line'range and parse it. Since I'm not clear if
Get(C);
Get(Calc.Element(I).Int_Value);
Get(Calc.Element(I).Char_Value);
represent 1, 2, or 3 different procedures, I can't really help with that part.

Else Error in program

It gives an error at the first else, saying it was not expected
Also if some one is willing to add me in Skype so I can ask these basic questions, it would help a lot.
Program Game ;
var answer: string;
Begin
writeln('(======= MENU =======)');
writeln('-------- PLAY --------');
writeln('-------- HELP --------');
writeln('-------- EXIT --------');
repeat
writeln('Pick PLAY, HELP or EXIT');
readln(answer);
if answer = 'EXIT' then
writeln();
writeln('Write EXIT again!')
else
if answer = 'HELP' then
writeln();
writeln('Simple commands like observe, look, take.')
else
if answer = 'PLAY' then
writeln();
writeln('You are in a cave!');
until answer = 'EXIT';
End.
Pascal expects one statement between if and else. It understands your code incorrectly:
if answer = 'EXIT' then
writeln(); (* one statement *)
writeln('Write EXIT again!') (* unrelated to the "if" above *)
else (* error - no preceding "if" *)
The solution is, use begin and end to enclose 2 statements in one:
if answer = 'EXIT' then
begin
writeln();
writeln('Write EXIT again!')
end
else
...

How can I make words in even position uppercase?

Here is my eiffel program, which is basically doing space removal (remove the redundant spaces in a given text file) to follow the regular expresson: A+(SA+)*EOL, where for each line in the file, it must start with alphabets and only one spaces between alphabets.
My question is, base on this program, how can I extend it to make every other word to be uppercase? i.e. 2th, 4th, 6th, etc.
feature {NONE} -- Main routine
copy_file
-- Copy a file character by character from input to output
require
input_open: input.is_readable
output_open: output.is_writable
local flag: INTEGER
has_read_space: BOOLEAN
empty_line : BOOLEAN
do
empty_line: True
flag := 0 -- 0 for previous space, 1 for previous char
from read_char -- Must prime the pump by reading the first character
until ch = EOF
loop
from
ch := input.last_character
until
ch = EOL
loop
if ch = Space_char and flag = 0 then -- leading spaces
read_char
elseif ch /= Space_char and flag = 0 then -- see first charater after space
if has_read_space then -- this clause make sure the space will only place in between two words instead of end of lin
output.putchar (Space_char)
end
output.putchar (ch)
empty_line := False
flag := 1
read_char
elseif ch = Space_char and flag = 1 then -- see space after characters
has_read_space := True -- Don't output it right away
flag := 0
read_char
elseif ch /= Space_char and flag = 1 then -- see character after character
output.putchar (ch)
read_char
end
end
if empty_line = False then
output.putchar (EOL) -- if line is not empty, then we place EOL at the end of line
end
flag := 0 -- reset it to 0 to make sure the next follow the same routine
has_read_space := False -- reset it to avoid placing space in the beginning of line
empty_line := True -- reset to proceed to new line
read_char
end
-- At end of file, nothing to do in Eiffel except close the files
input.close
output.close
end
Just figured it out.
Simply set another flag, say evenWord, initialize it to be false.
and in the elseif ch = Space_char and flag = 1 clause flip the flag evenWord := not evenWord It works

Magma CAS. Finding all d in [1..100] such that x^3+y^3=d for x,y in the rationals

> One := procedure();
procedure> P2<x,y,z> := ProjectiveSpace(Rationals(),2);
procedure> for i := 1 to 100 do
procedure|for> C_i := Curve(P2, x^3+y^3-i*z^3);
procedure|for> E_i, C_itoE_i := EllipticCurve(C_i);
procedure|for> G, map := MordellWeilGroup(E_i);
procedure|for> print Generators(E_i);
procedure|for> end for;
procedure> end procedure;
> One;
procedure() ... end procedure
This method is only printing out "procedure() ... end procedure" for some reason. I honestly have no idea why, I tried fixing it but in prev. codes C_i and E_i were valid concepts inside a for loop.
You don't have to tell me the exact mistake or even know Magma but if you can help work with me through this problem that'd be great (like teamwork?).
Try calling the procedure:
> One();
(It's been a long time since I've done anything with Magma, but it looks like One; on its own evaluates to the procedure itself.)

Resources