My program bounces back and forth between for and if, I have no idea why - pascal

I'm doing some homework and there's this problem where I have to make a program that tells me how many prime numbers there are between two numbers given by the user. For some reason, when I test the program with F8 I can see the program bounces back and forth between "for" and "if", for no apparent reason. And yes, I HAVE to use this language.
I have tried restanting the IDE, hoping it'ts just some bug (like it often happens on Android Studio). I don't know what else to try, this is the first time I use Lazarus and PASCAL.
program PrimoEntre;
var
a, b, i, q: Integer;
begin
write('Ingrese el primer valor: ');
readln(a);
write('Ingrese el segundo valor: ');
readln(b);
for i:=a to b do //It starts bouncing back and forth from here...
begin
q:=2;
if((b>=2)AND(b<=10)) then //...to here.
begin
while((q>=2) and (q<=b)) do
begin
if(a<>b) then
begin
if((a MOD b)=0) then
begin;
//i ES NO PRIMO
if(q=b) then
begin
writeln('No existen numeros primos entre ', a, ' y ', b, '.');
end;
q:=(q+1);
end;
end
else
begin
if(q=b) then
begin
//i ES PRIMO
writeln(i, ' es primo.');;
q:=1
end;
end;
end;
end
else
begin
while((q>=2) and (b<=10)) do
begin
if(a<>b) then
begin
if((a MOD b)=0) then
begin
//i ES NO PRIMO
if(q=b) then
begin
writeln('No existen numeros primos entre ', a, ' y ', b, '.');
end;
q:=(q+1);
end;
end
else
begin
if(q=b) then
begin
//i ES PRIMO
writeln(i, ' es primo.');
q:=1
end
end;
end;
end;
end;
readln();
end.
I expect the output to be one of the following:
-The console writing every time it finds a prime number between a and b.
-The console saying there isn't any prime number between a and b.
Instead, there's no result. The program just gets stuck and I have to terminate it manually.

You say you are trying to find prime numbers in the range a...b. The problem is that
you are doing this in a quite unnecessarily complicated way and the main reason
your program is jumping all over the place is that you are making it do so because
you are making the value of your q variable jump all over the place.
What I think you should do it to slow down and think what you are trying to do:
You want to iterate the integers in the range a to b. Your for loop does
this fine, with the value of i being what you are testing for primality. I'm going
to refer to this as your outer for loop.
Where you go wrong is to needlessly execute a while loop to test for primality.
Once you have established the i value you are testing, what you want to do is to
test whether there is any factor of i which is other than 1 or i. To do that, you
can use a simple, inner for loop which iterates a value j (so-called to avoid
confusion with your q) to determine whether i is divisible by j with zero remainder using
a mod test:
if it is, there is no point in continuing with the inner, j loop.
The next thing to consider is what should be the upper limit of the
loop for j := ... to ... should be. It is pointless to consider values
of j which are higher than the square root of i for the obvious reason
that if i has an intger factor greater than that, it must also have one
which is lower.
If the j loop completes without finding a factor, i is prime.
Try coding the inner, j loop, and you will see how the result is vastly
simpler (and more predictable) than your existing code, and with very few
opportunities for coding error, and vastly simpler to debug because execution
just "falls" through it for each value of i.
Btw, I completely agree with #TomBrunberg about structured coding as a general proposition, but I think that the two nested for loops you need here don't really need separating into different procedures/functions, though you might try
that once you've got the code working properly.

The causes of your error are a mix between the fact that the problem is highly depending on your input and that you are not treating some cases in else causes.
As an example, I ran your program with a=20 and b=30. The program ran into the for loop, which is correct. However, the (b>2) and (b<=10) condition is false, which made the compiler go to the else branch. However, the first thing it met there was while (q>=2) and (b<=10) which is, from the start, false. There it bounces back as you said in your question.
With a=2 and b=9, your program has a total different behavior.
How to fix it? I guess you first have to clarify what you want. Then, I think, the fix will be obvious.

Your program lacks structure because the whole program is crammed in the main procedure.
As a consequence of the previous, it has too many 'indentation levels' or 'logical chains' to be readable and manageable. With indentation levels or logical chains I mean e.g. your:
'for<condition> - if<condition> - while<condition> - if<condition> - if<condition> - if<condition> - writeln().
Because of the long chains of conditions, it is hard to evaluate what a change will effect, and you might find yourself deeper and deeper in trouble, when you try to fix something.
I suggest you ditch what you have now, and divide your program into procedures / functions according to main operations: for example GetUserEntry(var a, b: integer), function IsPrime(x: integer): boolean, procedure ShowResult() and the main program (GetUserEntry, for a to b if IsPrime then add to outstring, ShowResult(outstring)).
Finally, format your code to be readable using proper indentation.

Related

Debugger error 193 lazarus

I can't debug one of my programs for a year now. When I press the green Run button, the following error message appears:
The full text is:
The GDB command:
"-exec-run"
returned the error:
",msg="Error creating process C:/Users/leven/OneDrive/J\341t\351kpogramok/People/people.exe, (error 193).""
I've read many forums about this error, but my case looks a bit different...
As you can see, the file's path doesn't include any characters that could occur this problem (no spaces, no special letters). I've tried running outside OneDrive, same error.
I've spent a lot of time looking for something in my program that occurs this error and found that if I delete some parts of it (eg. a few procedures or functions, which contains a lot of code though), the program is debuggable again! So the trouble is with some parts of the program, but I still don't know the exact problem.
As I can remember, I've always debugged this program in a 64bit OP.
The one thing that could be problematic is that I probably started writing the program using Windows 7 or 8, and now I want to run it using Windows 10, but I still don't understand, why deleting some parts of the program is a solution...
Thanks in advance for your help!
UPDATE:
I've found, that the line
p[x,y,2,1]:=r;
cannot be debugged by the compiler.
Description:
p: array [1..15000, 1..10000, 1..7, 1..4] of integer;
p[] is a game field. The first two parameters are coordinates, the third and the fourth are not important.
x, y and r are integers.
So, the command seen above writes a number into the game field (p[]) array using the x, y coordinates.
I think we established through a series of queries in comments that the necessary and sufficient condition to provoke the debugger problem you've been getting is to include in your app the declaration of the array p that you've added to your q, that is:
var
p: array [1..15000, 1..10000, 1..7, 1..4] of integer;
For you, it seems that just including this declaration in your code is sufficient to make the debugger throw the error you quote.
For me the debugger starts fine but I get a SIGSEGV error on the assignment to p[] in the following code:
var
p: array [1..15000, 1..10000, 1..7, 1..4] of integer;
x,
y,
r : integer;
begin
x := 100;
y := 100;
r := 666;
p[x, y, 1, 1] := r;
writeln('Press any key ...');
readln;
end.
So, I would try smaller values for the first two bounds of the p array. If that works and you still need the original bounds, I would suggest looking for an FPC library which implements "sparse arrays" and declare p as one of those.
Good luck!

fpc: how to initialize a global variable before it is initialized

I am making a simple text game in pascal (a real beginner one). There is a general routine, that is repeated several times (the cycles variable, representing the levels). In the beginning of the routine there is a part where character`s name is asked. If the general repeat loop is complete or aborted at some level(1-4), the game goes back to the first sort of menu. I want the name to be asked only the first time, but, of course, I get the "variable "cycles" does not seem to be initialized" warning. Is there a way to restructure the code to avoid it?
Thanks.
The code excerpt (unnecessary details left behind):
program rpg_text_game;
var
game_action:char;
name:string;
cycles:1..5;
begin
repeat
writeln('Welcome to the game.');
writeln('To continue press "g",');
writeln('to read the license of this game press "i",');
writeln('and to quit press "q" and "enter": ');
readln(game_action);
case game_action of
'i', 'I':
{shows license}
'g', 'G':
{game begins}
if not (cycles in [2,3,4,5]) then
begin
writeln('Please enter your name: ');
readln(name);
end;
repeat
cycles:=1; //is initialized here
{actual game process - score is calculated based on *cycles* amount, that adds +1 with each tick ("if success then cycles:=cycles+1")}
{cycles - 1,2,3,4,5}
writeln('Do you want to try again, y/n?');
readln(game_action);
until(game_action='n') or (game_action='N');
until (game_action='q') or (game_action='Q');
writeln();
writeln('Press enter to quit');
readln();
end.
So, how to initialize/change the cycles variable (or even any other) to avoid that message and not to cheat by turning off the compiler hint option?
If cycles is a global variable, like in your example code, then simply do, in the main block of the program, before you start anything:
begin
cycles := 1;
game_action := Chr(0);
{ etc... }
...
end.
That is how you generally initialize global variables: in the main begin/end. block. Some versions of Pascal also allow (for global variables):
var
cycles: 1..5 = 1;
{ etc... }
but others don't. I don't know if your Pascal allows it. If it does, you won't have to initialize in the main block anymore. But note that that probably doesn't work for local variables of a function or procedure. There, you will probably have to use the (outer) begin/end; block of the function or procedure.
FWIW, the main block of a program can usually be found at the very end of the program, after all the const, type, var, procedure and function declarations and it ends with a dot (.).
Also note that the comment is right: split your program into separate functions and procedures, each with their own single task, and pass any information necessary to them. Do not write monolithic blocks of code. That is hard to read and hard to maintain. For instance, for each (or most) of your case items, create a separate procedure with the necessary parameters and call those from your case statement. That makes your code much easier to read, also for you.
In the first iteration of the loop in the code as is, cycles is read (by the IF NOT (cycles in [])) before being initialized. The compiler rightfully emits a warning for that.
The solution is simple, initialize it before the first REPEAT, or if you go more object pascal style, like Rudy says.

Sqrt function in Delphi

As an exercise that put myself into, I wanted to design a calculator in Delphi. So, far the sum, subs tract, multiply and divided are pretty much working.
The only function that I have a problem with its with Square Root.
Variables are extended, and I pretty much just convert from String (I'm using two EditBox) to Float
var
Form1: TForm1;
a, b, r: Extended;
procedure TForm1.SqrtClick(Sender: TObject);
begin
a := StrToFloat(Edit1.Text);
r := Sqrt(a);
ShowMessage(FloatToStr(r));
end;
Delphi its returning me
[Error] calc.dpr(72): Missing operator or semicolon.
The problem isn't really visible in your snippet above. But since I got psychic powers, I can still tell what is going on here.
You have a button called sqrt on your form. Hence, when you write sqrt in code, it refers to the button, not to the RTL function.
Solution: Write System.Sqrt instead of Sqrt (=Self.Sqrt, the button), or rename the button.

Can I set a state within a function?

I have this code:
procedure EstablishCommunication;
var
State : TStates;
Attempts : Byte;
procedure IncAttempts;
begin
Inc(Attempts);
end;
begin
State := stReadDeviceID;
Attempts := 0;
while True do
begin
if Attempts >= MAX_ATTEMPTS then
begin
State := stError;
end;
case State of
stReadDeviceID:
begin
// some code
IncAttempts;
end;
stError:
begin
// Error code
end;
...
...
...
I'd like to put the code that set state to stError within of the procedure IncAttempts, resulting:
procedure EstablishCommunication;
var
State : TStates;
Attempts : Byte;
procedure IncAttempts;
begin
Inc(Attempts);
if Attempts >= MAX_ATTEMPTS then
begin
State := stError;
end;
end;
begin
State := stReadDeviceID;
Attempts := 0;
while True do
begin
case State of
stReadDeviceID:
begin
// some code
IncAttempts;
end;
stError:
begin
// Error code
end;
...
...
...
So, can I move the code to IncAttempts?
Is this a code smell?
If yes, Can you advice me a better way?
I would see this as perfect valid code. I ask myself the following questions when declaring a method inside another. Most of the time I don't do it, but sometimes it's results in better code.
Will the internal function ever need to change as in a descendant class?
Can I override External method without calling the internal method and be OK?
Does the internal function have practical application outside of external method?
Is the internal function complex enough that it should be unit tested outside the scope of there external method?
If any of the above apply don't use an Internal Method.
However if if you don't have any of the above, and it can remove repeated code and/or simplify the design then you can consider using a internal function.
No real problem with that, should work just fine. You are already modifying another local variable Attempts so there is no reason why modifying State should smell more.
I do think you should be careful of using inline functions to much. The code often ends up hard to read/understand.
I would say that the new code have some whiff...
It all depends on how many states you manage in current code, and if the number of states could change in the future. Beware of how and when you set the state, and beware of how and when you check the state.
In the two code snippets you show, there is a minor difference:
In the first, original code, the current state is preserved through the iteration, and the new error-state is set in the beginning of the iteration, and it is always checked.
In the second, refactored code, the state is changed in the middle of the iteration, and it is only altered if the state is stReadDeviceID.
Now, if the last line in this while True do-iteration is if State = stError then Break;, then your first code will run the iteration one more time, changing the state to stError in the beginning if the iteration. Your second code will exit at the end of the current iteration, and the code in the stError-section of the case-statement will never be executed...
If you want to go all the way, and study the GoF's State Design Pattern, then take a look at these pages:
http://en.wikipedia.org/wiki/State_pattern (no Delphi code...)
http://sourcemaking.com/design_patterns/state (with Delphi code!)
http://www.dofactory.com/Patterns/PatternState.aspx (no Delphi code...)
http://conferences.embarcadero.com/article/32129#_Toc12157322 (with Delphi code!)

Does a begin-end block affect the performance of a conditional statement?

I am working with Delphi. Does it make any difference in performance if we write if condition in different ways? For example:
if (condition) then
someVar := someVal
else
someVar := someOtherVal;
Or we can write:
if (condition) then begin
someVar := someVal;
end else begin
someVar := someOtherVal;
end;
I prefer the second option just because it looks better than the first one.
No, there is no difference in performance, the code created will be identical.
An aspect that might be more important than that the second option looks nicer, is that it is better for maintainence. If you need to add another statement in the else block, you will not accidentally forget to add the begin and end, which would put the statement outside the if and always be executed.
This will not make a difference in performance.
begin and end tell the compiler where a block of code starts and finishes, but no computation needs to be done there.
Begin and End do not slow down your code, as others have already said. I am writing another answer to encourage you even more explicitly to ALWAYS use begin and end whenever you could use them.
It is good to be liberal with using Begin and End, and not worry about them slowing you down (because they don't).
If you go the other way, and leave out begin and end wherever you can, you get into a different type of trouble.
This has happened to me lots. You can get in trouble when you insert a line into a place where no begin and end statement exist. You then end up scratching your head wondering what you did that broke your code. Begin-end-everywhere, even where not needed, is standard operating procedure for a lot of Delphi coders.
The only thing you should keep in mind about if-elseif-else is to keep the common cases up in your code before edge cases, so that the least possible conditions are evaluated.

Resources