-3

I need help for this code. I dont know pascal. But i must write a code in pascal. I tried. But there ara some errors.Can anybody help me?

    Program Arrayexamp(output); 

  Var 
  counter,index,input: Integer; 
  A: Array[1..15] of Integer;
   B: Array[1..15] of Integer;

begin
  For index := 1 to 15 do
  begin  
    read(input);
    A[index] := input;
    index++
  end

    For counter := 1 to 15 do
    begin
    if (counter mod 2 = 0) Then 
      B[counter] = A[counter] * 3 
    Else B[counter]=A[counter]-5; 
    end
end.

The errores are :

source.pas(14,3) Error: Illegal expression

source.pas(16,5) Error: Illegal expression

source.pas(16,5) Fatal: Syntax error, ";" expected but "FOR" found

nil haty
  • 23
  • 1
  • 7
  • 4
    *there are some errors* is useless unless you tell us what those errors are exactly. They're on the screen, right in front of you. There is no reason for you not to include them in your post so we have them, too. Also, the issue is very clear if you properly format your code. – Ken White Apr 19 '17 at 16:18
  • Possible duplicate of [Proper structure syntax for Pascal if then begin end and ; (in Inno Setup)](http://stackoverflow.com/questions/28221394/proper-structure-syntax-for-pascal-if-then-begin-end-and-in-inno-setup) – nil haty Apr 19 '17 at 17:06
  • @nil: No, it's not. There are other issues as well. Read my answer. (I wrote the answer to the duplicate you suggested, and linked it in my post. It's not a duplicate.) – Ken White Apr 19 '17 at 17:11

1 Answers1

3

The issue is really clear, if you learn to properly indent (format) your code. This answer is based on the original code you posted, before you added more and removed some to change it. The information in the answer is still relevant to the problem, however.

What you posted:

Program Arrayexamp(output); Var counter,index,input: Integer; A : Array[1..15] of Integer;

begin

For index := 1 to 15 do
 begin  read(input);
A[index] := input;
    index++
end; 

begin 
For index := 1 to 15 do

If (counter mod 2 = 0) Then B[counter]=A[counter]*3 Else B[counter]=A[counter]-5; end.

How it looks properly formatted:

Program Arrayexamp(output); 

Var 
  counter,index,input: Integer; 
  A : Array[1..15] of Integer;

begin
  For index := 1 to 15 do 
  begin  
    read(input);
    A[index] := input;
    index++
  end; 

  begin 
    For index := 1 to 15 do
      If (counter mod 2 = 0) Then 
        B[counter] = A[counter] * 3 
      Else B[counter]=A[counter]-5; 
end.

The problem is clear: You have a begin without a matching end; in the lower block. In fact, the begin is totally unnecessary, and can be removed.

The second problem is that the for loop itself will increase the loop variable, so it's illegal to modify that counter inside the loop. Remove the index++; line. (See the next paragraph, too.)

A third problem is that Pascal does not support pre-increment or post-increment operators, so index++ is invalid syntax. Use index := index + 1; or Inc(index); instead.

The code written more properly:

Program Arrayexamp(output); 

Var 
  counter,index,input: Integer; 
  A: Array[1..15] of Integer;

begin
  For index := 1 to 15 do
  begin  
    read(input);
    A[index] := input;
  end; 

  For index := 1 to 15 do
    if (counter mod 2 = 0) Then 
      B[counter] = A[counter] * 3 
    Else B[counter]=A[counter] - 5; 
end.

For more information regarding the syntax and use of begin..end in Pascal, see this answer I wrote for Proper structure syntax for Pascal if then begin end

Community
  • 1
  • 1
Ken White
  • 123,280
  • 14
  • 225
  • 444