P
P
pashulke2014-10-26 12:21:27
Pascal
pashulke, 2014-10-26 12:21:27

How to assign a value to a constant?

There is a problem:
Given a sequence of integers. Sort it. Input data format. The first line contains an integer n(1<=n<=1000) -
the number of elements in the sequence. The second line contains the elements themselves --- integers not exceeding 10^9 in absolute value. Output data format. Print n integers --- the elements of the given sequence in non-decreasing order.

I don’t understand how I can write it at all if you can’t assign a value to a variable to a constant.
var a: array[1..n] of integer;
And n should be a constant value, but how can I do this if I take the value of n from a file?
readln (g, n);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mintormo, 2014-10-26
@Mintormo

If we are talking about standard Pascal, then there are no arrays of variable length. You can use a fixed array of a sufficiently large length, hoping that the real data will be smaller.

S
SilentFl, 2014-10-27
@SilentFl

1) set the array of the maximum possible length, in this case 1000
2) in the first line you set the number of elements, read into a variable
3) sort the elements 1..n
4) output the elements 1..n Like
this:

const M = 1000;
var a: array [1..M] of integer;
  n, i: integer;
function sort(count: integer)
var i,j, tmp: integer;
begin
  for i:=1 to count-1 do
    for j:=i to count-1 do
      if a[j]>a[j+1] then
        tmp:=a[j]; a[j]:=a[j+1]; a[j+1]:=tmp;
      end;
end;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  sort(1,n);
  for i:=1 to n do write(a[i], ' ');
end.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question