0

I have a problem in pascal : Generate first n numbers in the next sequence: 1,1,2,2,2,3,3,3,3,4,4,4,4,4,... I wrote a code for these, but the problem is that it doesn't works how i want to.

var
s,c: string;
n,i,j: integer;
begin
 s:='';
 readln(n);
 begin
  for i := 1 to n do
   begin
    if ( length(s)/2 -1 >= n) then
    begin
     Break;
    end;
    for j := 1 to i + 1 do
    begin
     str(i,c);
     s:= s + c+',';
    end;

 end;
end; 

4 Answers 4

2

Here's a version that is shorter, and also removes the extra comma at the end: It's a full console program (and adds a prompt for the number of elements):

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  i, j: Integer;
  s: string;
  nItems: Integer;
begin
  s := '';
  Write('Enter # of elements: ');
  ReadLn(nItems);
  for i := 1 to nItems do
    for j := 1 to i + 1 do
    begin
      s := s + IntToStr(i) + ',';
    end;
  System.Delete(s, Length(s), 1); { Remove final comma )
  WriteLn(s);
  ReadLn;
end.
Sign up to request clarification or add additional context in comments.

Comments

0

I've solved my question, here is the correct code, it would be nice if you could improve it.

var
s,c: string;
n,i,j: integer;
begin
 s:='';
 readln(n);
 begin
  for i := 1 to n do
   begin

    for j := 1 to i + 1 do
    begin
     if ( length(s) > n*2  ) then
    begin
     Break;
    end;
     str(i,c);
     s:= s + c+',';
    end;

   end;
  end;
 Delete(s,length(s)-2,length(s));
 write(s);
 readln;
 end.

Comments

0

I'd change the way your outer loop works, so i indicates at which position you are, and introduce a second number element, that represents the current element. Then increment element whenever the inner loop has run its course.

s:='';
readln(n);
element:=1;
begin
  for i := 1 to n do
  begin
    for j := 1 to i + 1 do
    begin
     str(element,c);
     s:= s + c+',';
    end;
    element:=element+1;
  end;
end;

Comments

0

You could change your inner loop:

    for j := 1 to i + 1 do
    begin
     if ( length(s) > n*2  ) then
    begin
     Break;
    end;
     str(i,c);
     s:= s + c+',';
    end;

Firstly, I'd advise you to always indent for clarity, so

    for j := 1 to i + 1 do
    begin
     if ( length(s) > n*2  ) then
       begin
        Break;
       end;
     str(i,c);
     s:= s + c+',';
    end;

secondly, you only need begin and end to turn multiple statements into a single statement, so since you've only got break in there, they're redundant - just use break directly:

    for j := 1 to i + 1 do
    begin
     if ( length(s) > n*2  ) then Break;
     str(i,c);
     s:= s + c+',';
    end;

Next, notice that you're recalculating that c an awful lot:

    str(i,c);
    suffix = c + ',';
    for j := 1 to i + 1 do
    begin
     if ( length(s) > n*2  ) then Break;
     s:= s + suffix;
    end;

I'm not all that keen on using break because it feels suspiciously like a goto, so maybe you could change this into a repeat ... until block:

    str(i,c);
    suffix = c + ',';
    j := 1;
    repeat 
      s := s + suffix;
      j := j + 1;
    until (j > i+1) or (length (s) > n*2);

(Notice that repeat and until come with an implicit begin and end so you don't need to type them.)
Now you might argue that I've reduced the clarity by making it less obvious that the loop just increments j. I don't feel strongly in this case whatever you prefer, but if you're using break a lot, consider whether while or repeat are better for you.

Your condition about length (s) > n*2 puzzles me (and no-one else has put it in their version of your code). You seem to want to cut it short for some reason. Are you sure? I'm not. Below is a table of the output you get without the limit, then the first n*2 items of the output, and the first n*2 characters of the string, so you can check. What was the break statement designed to do?

n:      1
output: 1,1
length: 2
n*2:    2
first n*2 numbers:    1,1
first n*2 characters: 1,

n:      2
output: 1,1,2,2,2
length: 5
n*2:    4
first n*2 numbers:    1,1,2,2
first n*2 characters: 1,1,

n:      3
output: 1,1,2,2,2,3,3,3,3
length: 9
n*2:    6
first n*2 numbers:    1,1,2,2,2,3
first n*2 characters: 1,1,2,

n:      4
output: 1,1,2,2,2,3,3,3,3,4,4,4,4,4
length: 14
n*2:    8
first n*2 numbers:    1,1,2,2,2,3,3,3
first n*2 characters: 1,1,2,2,

n:      5
output: 1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5
length: 20
n*2:    10
first n*2 numbers:    1,1,2,2,2,3,3,3,3,4
first n*2 characters: 1,1,2,2,2,

n:      6
output: 1,1,2,2,2,3,3,3,3,4,4,4,4,4,5,5,5,5,5,5,6,6,6,6,6,6,6
length: 27
n*2:    12
first n*2 numbers:    1,1,2,2,2,3,3,3,3,4,4,4
first n*2 characters: 1,1,2,2,2,3,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.