Recent

Author Topic: A program with three two-dimensional arrays  (Read 4142 times)

arnaudoff

  • Newbie
  • Posts: 1
A program with three two-dimensional arrays
« on: April 19, 2015, 11:11:53 pm »
Hello all there!

A friend of mine asked me for help but I can write only in C++. I've tried to look some Pascal tutorials but its syntax is very strange, in my opinion. So, here's the simple task:

- Write a program that works with three two-dimensional arrays : P (10x12), Q (14x16), R (15x19) which have to be initialized with integers from 0 to 999.

* Initialize the arays;
* Print the arrays;
* Save the positive elements above the main diagonal of each of the three arrays in a new array (for example, we can make three new two-dimensional arrays of integers: new_p, new_q, new_r).
   Count the negative elements below the main diagonal of each of the three arrays (for example, we can use three integer variables counter_p, counter_q, counter_r).
* Print the results (new_p, new_q, new_r; counter_p, counter_q, counter_r).

If you could help me, thank you a lot in advance!

Kind regards,

Georgi
* Print the results.

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: A program with three two-dimensional arrays
« Reply #1 on: April 19, 2015, 11:26:53 pm »
Ah another school assignment.
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: A program with three two-dimensional arrays
« Reply #2 on: April 19, 2015, 11:32:14 pm »
Hi, declaration of arrays is:
Code: [Select]
P: array[0..9, 0..11] of Integer;
Q: array[0..13, 0..15] of Integer;
R: array[0..14, 0..18] of Integer;
and work with arrays is like this (this code clears array P):
Code: [Select]
var i, j: Integer;
begin
  for i:=0 to 9 do
    for j:=0 to 11 do
      P[i, j]:=0;   
end;

If you know C++ then programming in Pascal is same (imperative language)
begin end instead of {}
:= instead of =
= instead of ==
and everything must be declared before it is used.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Nitorami

  • Hero Member
  • *****
  • Posts: 605
Re: A program with three two-dimensional arrays
« Reply #3 on: April 19, 2015, 11:45:57 pm »
Pascal is not "strange", the syntax is much clearer than C. But your description is not clear, e.g. "the positive elements above the main diagonal" - integers between 0 and 999 are always positive.

Anyway, here is a starting point using dynamic arrays.

Code: [Select]
var m,n: integer;

type TNumber = 0..999;
     TMyArray = array of array of TNumber;

var P,Q,R: TMyArray;


procedure Init (var A: TMyArray);
var m,n: integer;
begin
  for m := 0 to high (A) do begin
    for n := 0 to high (A[0]) do begin
      A[m,n] := random(high(TNumber)+1);
    end;
  end;
end;


procedure Print (A: TMyArray);
var m,n: integer;
begin
  for m := 0 to high (A) do begin
    for n := 0 to high (A[0]) do begin
      write (A[m,n]:4);
    end;
    writeln;
  end;
  writeln;
end;


begin
  SetLength (P, 10, 12);
  SetLength (Q, 14, 16);
  SetLength (R, 15, 19);

  Init (P);
  Init (Q);
  Init (R);

  Print (P);
  Print (Q);
  Print (R);

end.

BitBangerUSA

  • Full Member
  • ***
  • Posts: 183
Re: A program with three two-dimensional arrays
« Reply #4 on: April 20, 2015, 12:35:19 am »
it's quite saddening that someone who can write in C++ thinks Pascal syntax is 'strange'.
but then, so is the thought that integers between 0 and 999 might be negative.
Lazarus Ver 2.2.6 FPC Ver 3.2.2
Windows 10 Pro 64-bit

Leledumbo

  • Hero Member
  • *****
  • Posts: 8836
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: A program with three two-dimensional arrays
« Reply #5 on: April 20, 2015, 02:53:14 am »
Syntactically for this kind of program there won't be much difference between C++ and Pascal. If you can write C++, you should be able to write the Pascal version simply by reading and understanding the syntax difference (how a concept such as array initialization is implemented in either language). It's clearly an assignment language, so I won't bother giving any code before you show any effort.

 

TinyPortal © 2005-2018