0% found this document useful (0 votes)
61 views

Topic-Two Dim Arrays

This document discusses two-dimensional arrays, including: 1) Defining two-dimensional arrays and their input/output. Arrays can be defined as arrays of arrays. 2) An example that calculates the sum of positive elements in each row of a two-dimensional array and counts the number of zero elements. 3) Methods for searching within specific areas of a two-dimensional array, such as above or below the main diagonal.

Uploaded by

mikemylo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Topic-Two Dim Arrays

This document discusses two-dimensional arrays, including: 1) Defining two-dimensional arrays and their input/output. Arrays can be defined as arrays of arrays. 2) An example that calculates the sum of positive elements in each row of a two-dimensional array and counts the number of zero elements. 3) Methods for searching within specific areas of a two-dimensional array, such as above or below the main diagonal.

Uploaded by

mikemylo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Topic

Two dimensional arrays processing


1.
2.
3.
4.

Definition.
Input/output
Example of processing
Search in selected arears

1. Definition.

An*m =

a11 a12 a1m


a21 a22 a2m

A- the array name


n number of rows, m- number of columns

..
an1 an2 anm

In Pascal A[i,j] the array element, i index of row, j index of column


We can present matrix (or two-dimension array) as array of arrays
a1

If A=

...
an

wrehe ai one-dimensional array, then matrix one dim. array of one dim. arrays.

Input: n, m, {Ai,j}i=1 to n, j=1 to n

The description in pascal program


1.1
The most simple form:
Var
<array mane>:array [<lower boundary 1>..<upper boundary 1>,< lower boundary 2 >..< upper
boundary 2 >] of <type of element>;
Example:
Var
A:array[1..10,1..20] of real;
1.2 Using Type
Const
Nmax=10;
Mmax=20;
Type
Massiv:array[1..nmax,1..mmax] of real;
Var A,B : massiv;

1.3 Two-dim array as one-dim array of one-dim arrays


Const
Nmax=10;
Mmax=20;
Type
Vect=array[1..mmax] of real; {type for rows}
Massiv=array[1..nmax] of vect;
Var
X,Y : vect;
A,B : massiv;

2.

Input/Output operations.
2.1 Input form: (row by row in column form)

<n> <m>
<a11>
<a12>
1-st row
.
<a1m>
<a21>
2-nd row
<a22>
.
<a2n>
n-st row
<a1m>
.
<anm>
Readln(f1,n,m);
For i:=1 to n do
For j:=1 to m do
Readln(f1, A[i,j]);
2.2 Input form (row by row in table form - better than previous):
<n> <m>
<a11> <a12> <a1m>
<a21> <a22> <a2m>
..
<an1> <an2> <anm>

Readln(f1, n, m);
For i:=1 to n do
Begin
For j:=1 to m do
Read(f1, A[i,j]);
Readln(f1);
End;
2.3 Output form (table form):
N=<n> m=<m>
A[1,1]=<A11> A[1,2]=<A12> A[1,m]=<A1m>
A[2,1]=<A21>

A[n,1]=<An1> A[n,2]=<An2> A[n,m]=<Anm>

Writeln(f2,n=,n:2, ,m=,m:2);
For i:=1 to n do
Begin
For j:=1 to m do
Write(f2,A[,i,,,j,]=,A[i,j]:6:2);
Writeln(f2);
End;

3. Example of two-dim array processing


Is given: nm two dimensional array of n-rows, m-columns.
To find: the sum of positive elements in earch row, and the number of zero
elements in whole matrix.
Execute the task in different ways:
1. with the help of simple variables: the sum of positive elements in earch row;
2. with the help of one-dim array of n-elements, where bi the sum of positive
elements in i- row .
Input form
<n> <m>
<A11> <A1m>

<An1> <Anm>

Output form

Inp.dat file name

Task with Two dimensional array A


The given two-dim array
N=<n> m=<m>
A[1,1]=<A11> A[1,2]=<A12> A[1,m]=<A1m>
A[2,1]=<A21>

A[n,1]=<An1> A[n,2]=<An2> A[n,m]=<Anm>


The sums of positive elements in rows:
1-st row - <sum>
.
<n>- row - <sum>
The number of zero elements =<kol>

Tests
N of the
test
1

Input data

Expected results

n=3, m=4

Sum of positive:
Sum=16
Sum=2
Sum=0
Kol=3
Sum of positive:
Sum=0
Sum=0
Sum=0
Kol=0
Sum of positive:
Sum=16
Sum=2
Sum=0
Kol=0

1 3 5 7
A = -4 0 2 -4
-2 0 -12 0
n=3, m=3
-1 -3 -5
A = -4 -1 -2
-2 -12 -3
n=3, m=3
16 -3 -5
A = -4 -1 2
-2 -12 -3

Data table specification.

Out.dat file name

The algorithm for case 1.

Input: n, m, {Ai,j}i=1 to n, j=1 to n

Kol:=0

No

For i:=1 to n
do
Yes

kol
Sum=0

No

For j:=1 to m
do

The

End

sum
No

Yes

A[i,j]>
0
Sum:=sum+A[i,j]

No

Yes

A[i,j]=
0

Kol:=kol+1

Pascal program:

Program twodimarr(f1,f2);
Var
A:array[1..100,1..200] of real;
Sum:real;
Kol,i,j:integer;
f1,f2:text;

Begin {assign &open files}


Assign(f1,d:\a-11-10\inp.dat);
Assign(f2,d:\a-11-10\out.dat);
Reset(f1);
Rewrite(f2);
{ input operations}
Readln(f1, n, m);
For i:=1 to n do
Begin
For j:=1 to m do
Read(f1, A[i,j]);
Readln(f1);
End;
{output title for program&given array}
Writeln(f2, Task with Two dimensional array A);
Writeln(f2,The given two-dim array);
Writeln(f2,n=,n:2, ,m=,m:2);
For i:=1 to n do
Begin
For j:=1 to m do
Write(f2,A[,i,,,j,]=,A[i,j]:6:2);
Writeln(f2);
End;
Writeln(f2,The sums of positive elements in rows:);
{array processing}
Kol:=0;
For i:=1 to n do
Begin
Sum:=0;
For j:=1 to m do
Begin
If A[I,j]>0 then Sum:=sum+A[i,j];
If A[I,j]=0 then kol:=kol+1;
End;
Writeln(f2,i, row ,sum:6:2);
End;
Writeln(f2,The number of zero elements =, kol);
Close(f1);
Close(f2);

End.

4. Search in the allocated areas.


Frequently there is a necessity to carry out processing in some part of matrixes:
only on the main diagonal, above main diagonal, below main diagonal. For
processing the multiple cycles are used.
1. Above main diagonal, not including its elements:
i:=1 to n-1
j:=I+1 to n

2. Above main diagonal, including its elements:


i:=1 to n
j:=I to n
3. Below main diagonal, not including its elements:
j:=1 to n-1
i:=j+1 to n
4. Below main diagonal, including its elements:
j:=1 to n
i:=j to n

The input / conclusion of a matrix on lines, when number of elements in a line is


more than some given S.
<A11> <A12> <A1s>
<A1s+1>.. <A12s>
<A12s+1>. <A1m>
<A21>. <A2s>
<A22s+1>. <A22s>
<A22s+1>. <A2m>

1-st row

2-nd row

For i:=1 to n do
For j:=1 to m do
Begin
Read(f1,A[i,j]);
If (j mods)=0 then
Readln(f1);
End;

The conclusion of the large matrix on lines is similarly carried out.

You might also like