How to make loop in Pascal - pascal

I just started learning Pascal and have issue.
Need to create a program that outputs the following result:
Input real number and press enter!: 4
****
***
**
*
I've come this far, but not figure out what to do next.
program project1;
var
i, x:byte;
y:char;
begin
write('Ievadiet veselu skaitli: ');
readln(x);
y:='*';
for i:=x downto 1 do writeln(y:3);
readln;
end.

Two loops do have to be involved, but breaking out that inner loop functionality into a separate procedure can make it much easier to understand what's going on.
We can also declare y as const.
program project1;
const
y = '*';
var
i, x : byte;
procedure writeln_n_times(x : char; n : byte);
var
i : byte;
begin
for i := 1 to n do
write(x);
writeln();
end;
begin
write('Ievadiet veselu skaitli: ');
readln(x);
for i := x downto 1 do
writeln_n_times(y, i);
end.

use two loops:
program project1;
var
i, j,x:byte;
y:char;
begin
write('Ievadiet veselu skaitli: ');
readln(x);
y:='*';
for i:=x downto 1 do
begin
for j:=i-1 downto 1 do
begin
write(y);
end;
writeln();
end;
end.
output:
Ievadiet veselu skaitli: 7
******
*****
****
***
**
*

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.

Turbo Pascal result is not visible

can you say to me, where is the error. I wanna write in console the content of the a[i] in every for run. The output could be 6 digits. There is compiler Alert:
/usr/bin/ld.bfd: warning: link.res contains output sections; did you forget -T?
...Program finished with exit code 0
program Hello;
type aha = array [1..6] of integer;
var
a: aha;
n,x,i: integer;
begin
a[1]:=2;
a[2]:=6;
a[3]:=4;
a[4]:=2;
a[5]:=4;
a[6]:=3;
n:=6;
x:=a[1];
for i:=2 to n do
begin
{
if (a[i-1]>= x) then
begin
a[i]:=a[i] - x div 2;
end;
else
begin
a[i]:=a[i] + x;
x:= x + mod x(a[i] + 1);
end;
writeln (a[i]);
}
end;
end.

Pascal error: left side cannot be assigned when trying to compile

I got this error when trying to compile.
left side cannot be assign to, ./number 21,22 in pastebin
here is my code
Program urut;
Uses Wincrt;
Const N = 5;
data: Array [1..N] Of Integer = (2,4,5,3,1);
Var
j,k,temp : Integer;
Begin
Clrscr;
Writeln ('Data sebelum diurutkan');
For j:=1 To N Do
Begin
Writeln('data[' ,j, ']= ',data [j]);
End;
For j:=1 To N-1 Do
Begin
For k :=N Downto j+1 Do
Begin
If data[k] < data[k-1] Then
Begin
temp := data[k];
data[k] := data[k-1]; //left side cannot be assigned to
data[k-1] := temp; //left side cannot be assigned to
End;
End;
End;
Writeln;
Writeln ('Data setelah diurutkan ');
For j:=1 To N Do
Begin
Writeln ('data[' ,j, '] = ',data[j]);
End;
Writeln;
End.
sorry for uncorrectly pattern post
, thank you so much.
Like tom Tom Brunberg says, my array is const, it can't be changed. Therefore I need to remove that const.
So it should be
data: Array [1..5] Of Integer = (value);
without const, and put it under var with another variable

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

How to make cross shaped pattern corresponding with user input in Pascal?

How can I make this pattern ?
you'll see there is a cross pattern below (printed in space)
********
* ****** *
** **** **
*** ** ***
**** ****
**** ****
*** ** ***
** **** **
* ****** *
********
Here's what I've tried. I put the user input value with 10 for testing purpose.
uses crt;
var a,b,n,space1,space2 : integer;
begin
clrscr;
n:= 10;
space1:=1;
space2:=n;
for a:=1 to n do
begin
for b:=1 to n do
begin
if(b=space1) OR (b=space2-1) then
begin
write(' ');
space1:=space1+1;
space2:=space2-1;
end else
write('*');
end;
writeln;
end;
readkey;
I'm having trouble to print the space character.
I think the problem is in the if condition.
What's the best condition for this situation?
The function you're looking for is
StringOfChar(c: char, i: SizeInt): AnsiString
Example: Create 5 *
var s: string;
..
s := StringOfChar('*', 5);
..
For further help you should provide some example code with your work.
One way would be:
uses crt;
var a,b,n,space1,space2 : integer;
s: string;
begin
clrscr;
n:= 10;
// first half of cross
for a:=0 to n div 2 -1 do begin
s := StringOfChar('*', n-2);
Insert(' ', s, a+1); // first space
Insert(' ', s, n-a); // last space
writeln(s);
end;
// second half of cross
for a:= (n div 2 -1) downto 0 do begin
s := StringOfChar('*', n-2);
Insert(' ', s, a+1); // first space
Insert(' ', s, n-a); // last space
writeln(s);
end;
writeln;
readkey;
end.
You can solve this problem also with just one loop.
I leave it to you as a learning exercise :-)
You need to change your "if" to the following:
if (a = b) or (b = N - a + 1) then
write(' ')
else
write('*');

Resources