How do i compare 3 numbers in Pascal? - pascal

i'm a newbie
and I need how to compare 3 numbers in Pascal.
Here is what I tried so far
BEGIN
max:=A;
IF B>A THEN max:= B;
IF C>B THEN max:= C;
END;
but when I choose, for example, A = 5 , B=2 , C=4, the output is 4, but it should be 5. Where is the problem?
I want at the end writeln('The Large Number is ',max);

You could do this (you should be comparing with max)
BEGIN
max:=A;
IF B>max THEN max:= B;
IF C>max THEN max:= C;
END;

You must compare with max instead of A or B.
Changing your code in a easy way:
BEGIN
max := A;
IF B > max
THEN
max := B;
IF C > max
THEN
max := C;
END;

Or, in a somewhat recent Pascal, like Delphi or Free Pascal, using the max function from the MATH unit.
result:=max(a,max(b,c));

Using the Max Function of Pascal
PROGRAM MaxProgram;
USES math;
VAR
num1,num2,num3,maxNum : INTEGER;
BEGIN
(* Receive the Values *)
WRITELN('Enter First Number');
READLN(num1);
WRITELN('Enter Second Number');
READLN(num2);
WRITELN('Enter Third Number');
READLN(num3);
(* Using the Max Function *)
maxNum := max(num1,max(num2,num3));
(* Display Result *)
writeln('The Highest number is ', MAXNUM);
END.

Related

My program won't print out correct math numbers

I'm coding a little program but it won't print out the right math answer, it just stays at 993.
The Code:
program _Gul_;
uses crt;
var a: integer;
var b: integer;
begin
writeln('1000 - 7?');
a := 1000;
b := a - 7;
while a > 0 do
begin
writeln (a - 7, ' - 7?');
delay(120);
a := a - 7;
writeln (b)
if a = 6 then
break;
end;
writeln('я гуль')
end
I don't quite know why it is not working. I defined "b" and made a command that prints it out and the output is just:
You never update the value in b. In point of fact, b is not necessary to your program at all. Your printing strategy is also more complicated than it needs to be. Print a minus 7, then do the subtraction and print it. This prevents the program telling you the rest of 6 - 7 is 6.
program _Gul_;
uses
crt;
var
a: integer;
begin
a := 1000;
while a > 0 do
begin
writeln (a, ' - 7?');
delay(120);
a := a - 7;
writeln (a);
if a = 6 then
break;
end;
writeln('я гуль')
end.

How to multiply odd numbers from an array?

I have a program that reads a N number of integers and push them into an array, then I need to multiply the odd numbers from the array.
Program p2;
type
tab = array[1..10] of integer;
var
a, c : tab;
n, i, prod : integer;
begin
writeln('n=');
readln(n);
prod := 1;
writeln('Enter array numbers:');
for i := 1 to n do
read(a[i]);
if (a[i] mod 2 = 1) then
prod := a[i] * prod;
writeln('The produs of the odd numbers is: ',prod);
end.
For example when you enter the n as 5, and the numbers: 1, 2, 3, 4, 5;
The result of multiplication of odd numbers should be 15. Can someone help me to fix it working properly.
First off, whitespace and indentation are not terribly significant in Pascal, but they can be your ally in understanding what your program is doing, so let's make your code easier to read.
Program p2;
type
tab = array[1..10] of integer;
var
a, c : tab;
n, i, prod : integer;
begin
writeln('n=');
readln(n);
prod := 1;
writeln('Enter array numbers:');
for i := 1 to n do
read(a[i]);
if a[i] mod 2 = 1 then
prod := a[i] * prod;
writeln('The produs of the odd numbers is: ', prod);
end.
With indentation, it should be apparent what's happening. Your conditional only runs once, rather than each time through your loop. As suggested in the comments, begin and end neatly solve this problem by creating a block where the conditional is checked each time the loop runs.
program p2;
type
tab = array[1 .. 10] of integer;
var
a, c : tab;
n, i, prod : integer;
begin
writeln('n=');
readln(n);
prod := 1;
writeln('Enter array numbers:');
for i := 1 to n do
begin
read(a[i]);
if a[i] mod 2 = 1 then
prod := a[i] * prod;
end;
writeln('The produs of the odd numbers is: ', prod);
end.

Pascal, reading unknown number of integers

My question is how can I read some number of integers that user enters on standard input, and place them in array.However I don't how many numbers user will enter and i can't ask him that? User enters numbers in one line.
Okay i have just one more answer i would like to add.Thanks all for your help this is code written based on suggestions.I added a line for writting array backwards just for you can see that it has readed well.
program backo;
var niz:array [1..100] of integer;
n, i:integer;
begin
i:=1;
writeln('enter elements of array');
read(niz[i]);
while not eoln do
begin
i:=i+1;
read(niz[i]);
end;
for n:=i downto 1 do
writeln(niz[n]);
end.
Ok, based on comments there is three ways demonstrated:
program readmultiint;
{$mode objfpc}{$H+}
uses
StrUtils;
const
CMaxValues = 3;
var
s: string;
darr: array of Integer;
sarr: array [0..CMaxValues-1] of Integer;
i, cnt: Integer;
begin
// Dynamic array using WordCount
Writeln('Enter values:');
Readln(s);
cnt := WordCount(s, StdWordDelims);
SetLength(darr, cnt); // Allocate room for values
for i := 0 to cnt - 1 do
Val(ExtractWord(i + 1, s, StdWordDelims), darr[i]);
for i in darr do
Writeln(i);
// Dynamic array usin EOLN
SetLength(darr, 0);
Writeln('Enter values:');
while not eoln do
begin
SetLength(darr, Length(darr) + 1); // Expand array for next value
Read(darr[High(darr)]);
end;
Readln; // Read <Enter> itself
for i in darr do
Writeln(i);
// Static array
cnt := 0;
Writeln('Enter values:');
while (not eoln) and (cnt < CMaxValues) do // Reads not more then CMaxValues values
begin
Read(sarr[cnt]);
Inc(cnt);
end;
Readln; // Read <Enter> itself
for i := 0 to cnt-1 do
Writeln(sarr[i]);
end.
Feel free to use one of them or provide your own :)
PS: Some readings:
Dynamic arrays
Val procedure
for-in loop

Comparing sorting types in Pascal

I am going to write a program that asks the user to enter items then choose the sorting type to sort them (bubble, inserion and selection).
After that I have to compare these types by time efficiency.
I wrote code for the first part, but I didn't know how to use a function in Pascal to write the second part (comparison).
This is what I have done:
Begin
writeln('How many Number you would like to sort:');
readln(size);
For m := 1 to size do
Begin
if m=1 then
begin
writeln('');
writeln('Input the first value: ');
readln(IntArray[m]);
end
else if m=size then
begin
writeln('Input the last value: ');
readln(IntArray[m]);
end
else
begin
writeln('Input the next value: ');
readln(IntArray[m]);
End;
End;
writeln('Values Before The Sort: ');
for m:=0 to size-1 do
writeln(IntArray[m+1]);
writeln();
begin
repeat
writeln(' ~*~...~*~ ~*~...~*~ ~*~...~*~ ~*~...~*~');
writeln('1. Insertion Sort.');
writeln('2. Bubble Sort.');
writeln('3. Selection Sort. ');
writeln('4. Exist ');
writeln('');
writeln('Enter your choice number: ');
readln(sort);
case sort of
1: begin {when choice = 1}
writeln('');
writeln(' The sorted numbers by Insertion sort are ~> ');
For i := 2 to size do
Begin
index := intarray[i];
j := i;
While ((j > 1) AND (intarray[j-1] > index)) do
Begin
intarray[j] := intarray[j-1];
j := j - 1;
End;
intarray[j] := index;
End;
for i:= 1 to size do
writeln(intarray[i]);
end;
2: begin {when choice = 2}
writeln('');
writeln(' The sorted numbers by bubble sort are ~> ');
For i := size-1 DownTo 1 do
For j := 2 to i do
If (intArray[j-1] > intarray[j]) then
Begin
temp := intarray[j-1];
intarray[j-1] := intarray[j];
intarray[j] := temp;
End;
for i:= 1 to size do
writeln(intarray[i]);
end;
3: begin {when choice = 3}
writeln('');
writeln(' The sorted numbers by selection sort are ~> ');
for i:=1 to size do
begin
j:= i ;
for index:= i +1 to size do
if intarray[index]<intarray[j] then
j:=index;
temp:=intarray[j];
intarray[j]:=intarray[i];
intarray[i]:=temp;
end;
for i:= 1 to size do
writeln(intarray[i]);
end;
4: begin
writeln('*~...~*~ Thank U For used Our Program We Hope You Enjoyed ~*~...~*~ ');
end;
end;
until sort = 4 ;
end;
end.
I hope that I will find the answer here...
I hope you know the TIME complexity of Bubble, Insertion and Selection sort.
If you know you can just compare like that
if (time_complexity_bub>time_complexity_ins)and(time_complexity_bub>time_complexity_sel) then writeln('Bubble Sort is the WORST !!!');
if (time_complexity_ins>time_complexity_bub)and(time_complexity_ins>time_complexity_sel) then writeln('Insertion Sort is the WORST !!!');
if (time_complexity_sel>time_complexity_ins)and(time_complexity_bub<time_complexity_sel) then writeln('Selection Sort is the WORST !!!');
If you have other questions you can ask me :D ...
Pascal supports >,>=,=,<= and < for comparison, but it seems you already know this:
if intarray[index]<intarray[j] then
So maybe you have to explain your question a bit clearer.
I think author is not sure how to measure time in Pascal.
I don't know what compiler you're using, but overall pattern is like:
var
startTime : TDateTime;
overallTime : TDateTime;
begin
startTime := SomeFunctionToGetCurrentTimeWithMicroseconds;
SomeLongOperation;
overalltime := SomeFunctionToGetCurrentTimeWithMicroseconds() - startTime;
end.

small problem In Pascal , Could you please help?

I write this program with pascal
which ask the user to enter two arrays and constant value which is K
the program muli the K with arrays .
and then save the answer in new array
and do some operation in new array
addition << work well
Subtraction << also work
BUT the problem in Multi << I am trying to ask the user to enter a new array and do Muti but still there is a problem.
ALSO
I want these operation repeated until the user press exit <<< I could not do this options because i am not perfect with pascal .
I would be grateful if you could help me
This is My Code
program BST6;
const maxN=100;maxM=100;
type mat=array[1..maxN,1..maxM]of integer;
var A,B,c:mat;
n,m,l,s,i,j,k:integer;
ch : char;
procedure readMat(var A:mat;var m,n:integer);
begin
for i:=1 to m do
for j:=1 to n do
begin
write('mat[',i,',',j,']=');
readln(A[i,j]);
end;
end;
procedure writeMat(A:mat;m,n:integer);
begin
for i:=1 to m do
begin
for j:=1 to n do
write(a[i,j]:4);
writeln;
end;
end;
function multK(A:mat;k:integer):mat;
begin
for i:=1 to n do
for j:=1 to m do
begin
B[i,j]:= K*A[i,j];
end;
multK:=B;
end;
function minus(A,B:mat):mat;
begin
for i:=1 to m do
for j:=1 to n do
C[i,j]:=A[i,j]-B[i,j];
minus:=C;
end;
function plus(A,B:mat):mat;
begin
for i:=1 to m do
for j:=1 to n do
C[i,j]:=A[i,j]+B[i,j];
plus:=C;
end;
function mult(A,B:mat;m,l,n:integer):mat;
begin
for i:=1 to m do
for j:=1 to n do
for k:=1 to l do
c[i,j]:=c[i,j]+A[i,k]*B[k,j];
mult:=C;
end;
begin
write('input m<=',maxM,'.. m=' );readln(m);
write('input n<=',maxN,'.. n=');readln(n);
readMat(A,m,n);
writeln('input the const K');readln(k);
B:=multK(A,K);
writeln('The matrix A : ');
writeMat(A,m,n);
writeln('The matrix B=K*A : ');
writeMat(B,m,n);
writeln('choose the operation + , - or * ');
readln(ch);
case ch of
'+' : c:=plus(A,B);
'-' : c:=minus(A,B);
'*' : begin
writeln('input m<=',maxM,'input l<=',maxN);readln(m,l);readMat(A,m,l);
writeln('input l<=',maxN);readln(n);readMat(B,l,n);
c:=mult(A,B,m,l,n);
end;
end;
writeMat(c,m,n);
readln;
end.
First of all having global one letter variables which collide with function parameters with the same name is insane.
Why does multK modify the global variable B as a sideeffect?
Why does minus modify the global variable C as a sideeffect?
Why global integers as for index variables?
And mult is even worse: It doesn't only modify C as a sideeffect, but it assumes C contains meaningful values beforehand. I think it needs to initialize C to all zeros beforehand.
My guess is some of your side effects interfere in strange ways. But I don't want to think it through. Refactor your code first. In particular learn how and when to use local variables.

Resources