0

Does anyone know how to create Matrix using array function in Pascal?

Output will be something like this:

00100
01110
11111
01110
00100

Thanks.

Edit:

This is my Code sofar:

program borlpasc;
var a:array[1..100,1..100] of integer;
    i,j,n:integer;
begin write('Enter the Number N='); {Example: 5}
   readln(n);
   for i:=1 to n do
       for j:=1 to n do
           begin a[i,j]:=0;
           if n mod 2 = 1 then begin
           a[n div 2 + 1, j] := 1;
           a[i, n div 2 + 1] := 1;
           end;
           end;
    for i:=1 to n do
        begin for j:=1 to n do write(a[i,j]:2);
            writeln
        end;
     readln
end.

but only get this:

00100
00100
11111
00100
00100
0

1 Answer 1

1

You don't need arrays for this, just two nested FOR loops. Here is an example which writes a grid of 1s - see if you can modify this to give the output that you need (hint: you need to add an IF statement).

program Grid;

  procedure DrawGrid(nx: integer; ny: integer);

    var
      x, y: integer;

    begin
      for y := 1 to ny do
        begin
          for x := 1 to nx do
            begin
              write('1');
            end;
          writeln;
        end;
    end;

begin
  DrawGrid(5, 5);
end.        
Sign up to request clarification or add additional context in comments.

4 Comments

You could still add a few notes about how to actually create a 2D array, and show how one can then print out the 2D array explicitly. This answer as it stands is rather unfulfilling in my opinion since the OP talks about matrices and for most matrix operations you still need to store the matrix somewhere convenient (standard output is not particularly convenient).
I think the OP has already worked this part out in the code in his question - his two problems appear to be that (a) he is using an array unnecessarily and (b) his logic for 1 versus 0 output needs a little more work. Since it's homework I didn't think it wise to provide a complete solution, just guidance.
Hi Paul, Thanks!. Any hint how to fix my code? I've tried for night and still no luck :(
If you want to keep your code with the redundant array then you just need to fix the logic in the IF statement - think about what determines whether a cell is a 1 or a 0.

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.