Sie sind auf Seite 1von 15

TWO-DIMENSIONAL ARRAYS

I. DECLARATION SYNTAX : type ArrayName[RowSize][ColSize];


Ex. int X[3][4];
float ave[50][2];
double Record[50][5];

There are 5 ways to initialize to zero a 2x4 integer array as follows :


1.) int A[2][4] = { 0,0,0,0,0,0,0,0};
2.) int A[2][4] = { {0,0,0,0} , {0,0,0,0} };
3.) int A[2][4] = { {0,0,0,0} ,
{0,0,0,0}
};
4.) int A[2][4] = { {0} , {0} } ;
5.) int A[2][4] = { 0} ;

NOTE : Microsoft Visual C++ automatically initializes to zero all the uninitialized
array elements if you provide at least one value ( zero or non-zero) in the
statement that declares the array.
Example declaration and initialization of a 3x4 integer array :

int X[3][4] = { { 1,2,3,4 },


{5,6,7,8, },
{9,10,11,12}
};
It can also be declared as : int X[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12} ;
REPRESENTATION OF THIS 2-DIMENSIONAL ARRAY :
0 1 2 3
0 1 2 3 4
1
5 6 7 8
2
9 10 11 12
The first subscript or index of the first element is 0,0.
Ex . cout<<x[0][2]; // 3
cout<<x[1][1]; // 6
cout<< x[2][0]; //9
cout<<x[1][3];
cout<<x[1][2];
cout<<x[2][3];

II. LOADING ARRAY VALUES FROM THE KEYBOARD

int A[3][4], x, y, N= 1;
for ( x = 0 ; x <= 2; x ++)
{
for (y =0; y <= 3; y++)
{
cout<<”Enter value for A[“<<x<<”][”<<y<<”]:”
cin>> A[x][y];
}
}
III. LOADING ARRAY VALUES USING NESTED LOOP x y N A[x][y]
0 0 1 1
int A[3][4], x, y, N= 1; 1 3 3
2 5 5
for ( x = 0 ; x <= 2; x ++) 3 7 7
{ 1 0 9 9
for (y =0; y <= 3; y++) 1 11 11
{ A[x][y] = N; 2 13 13
N = N + 2; 3 15 15
} 2 0 17 17
} 1 19 19
2 21 21
3 23 23
IV. PRINTING THE ARRAY VALUES (in section III):

for ( x = 0 ; x <= 2; x ++)


OUTPUT :
{
1 3 5 7
for (y =0; y <= 3; y++)
9 11 13 15
cout<<A[x][y]<<”\t”;
17 19 21 23
cout<<endl;

V.PASSING AN ARRAY ELEMENT TO A FUNCTION


When a single array element is to be passed to a function :
1.) The receiving formal parameter of the function must be of the same data type
as the array element.
2.) The double-subscripted array element is passed to the function during the
function call. Only a copy of the value of the array element is manipulated by
the function. If the function changes its value, the actual value of the array
element in the calling program is not changed by the function’s action.
(This is called “Pass-By-Value” )

Ex.1.) #include<iostream>
using namespace std;
int change (int x)
{ x = 2 * x:
return x ; }
int main ( )
{ int y, A[2][3] = { (3,4,5),
{6,7,8 }
};
y = change( A[1][0]);
cout <<”y = “<<y<<endl;
cout<<”A[1][0] = “<<A[1][0]<<endl;
A[1][0]= change( A[1][0]);
cout<<”A[1][0] = “<<A1][0] <<endl;

system(“pause”);
return 0; }
VI. PASSING THE ENTIRE ARRAY TO THE FUNCTION

1.) The receiving formal parameter of the function must be an Array Parameter of
the same data type as the array that it will receive. An Array Parameter is an array
declared between the open and close parenthesis of the function, only the 1st
Dimension Size of the Array Parameter can be declared as Dimensionless,
(although it can also be declared with Dimension size). If a Dimension Size is
provided inside the square brackets, it will only be ignored by compiler, so the 1st
dimension size of the array should be included as a separate argument( or formal
parameter) of the function.
2.) During the function call, only the name of the array (without any subscript) is
passed to the function When the name of the array is passed, what is actually
passed is the memory address of the first element of the array. When the
memory address of the array is passed to the function, it is known as “CALL-BY-
REFERENCE”. Any changes made by the function to the value of the array
parameter element also changes the actual value of the array element in the
calling program.

Ex. 1.) #include<iostream>


using namespace std;
void LoadArray( int X[ ][3], int SIZE, char Name);
void PrintArray( int X[ ][3], int Size) ;
int main ( )
{
int B[2][3] = {0}, C[3][3] = {0};
cout<<”Enter 6 elements for array B : “<<endl;
LoadArray( B, 2 , ‘B’ ) ;
cout<<”Enter 9 elements for array C : “<<endl;
LoadArray( C, 3 , ‘C’ ) ;
cout<<” The contents of Array B are :”<<endl;
PrintArray( B, 2) ;
cout<<” The contents of Array C are :”<<endl;
PrintArray( C, 3) ;
cout<<”Change the first row elements of array B :”;
LoadArray( B, 1 , ‘B’ ) ;
PrintArray( B, 2) ;
cout<<”Change the last row elements of array C :”;
LoadArray( &C[2][0], 1 , ‘C’ ); // &C[2][0]]- means memory address of
st
the 1 element in row
// 3 of array C
PrintArray( C, 3) ;
system(“pause”);
return 0;
}
//--------------- Function definition for function LoadArray--------------------
void LoadArray( int X[ ][3], int SIZE, char Name)
{
Int I, j ;
for ( i = 0; i < SIZE; i++ )
{
for( j = 0 ; j < 3 ; j++)
{ cout<<”\nEnter “<<Name<<”[“<<i<<”][”<< j <<”] :”;
cin>>X[i][j];
}
}
return;
}
// --------------- Function definition for function PrintArray--------------------
void PrintArray( int X[ ][3], int Size)
{
{
Int I, j ;
for ( i = 0; i < SIZE; i++ )
{
for( j = 0 ; j < 3 ; j++)
cout<<X[i][j]<<”\t”;
cout<<endl;
}
return;
}

PROBLEM 1: (Filename : ARRAY_1_2D) Write a program that will use 2 two-


dimensional arrays named : Record[50][2] and Grades[50][4]. The student
numbers of 50 students will be entered on the first column of Record Array,
while their corresponding average grades will be stored on the 2nd column of
this Record Array. The ave grade is to be computed by function AVE that has 4
arguments : Q1, Q2,PE and FE using the equation :
AVE = (Q1 + Q2 + 2 *PE + 2 * FE)/ 6
The grades Q1, Q2, PE and FE of these 50 students will be entered first on a
50x 4 Grades Array.
//filename : ARRAY_1_2D
#include<iostream>
using namespace std;
#include<iomanip>
double AVE( int a, int b, int c, int d);
int main( )
{ int i, j,N;
double Record[50][2] ={ 0 };
int Gr[50][4] = {0};

// Statements to enter the 4 grades to array grade :


for( i = 0, N= 1; i <50; i ++)
{ cout<<”Enter Student No. Q1,Q2,PE and FE: “;
cin>>Record[i][0]>>Gr[i][0]>>Gr[i][1]>>Gr[i][2]>>Gr[i][3];
// Record[i][1] = ( Gr[i][0] + Gr[i][1] + 2* Gr[i][2] +2* Gr[i][3])/6.0 ;
Record[i][1] = AVE( Gr[i][0], Gr[i][1], Gr[i][2], Gr[i][3]) ;
N = N+ 1;
}
// statements to print the content of the Record array
cout<<”STUDENT NO. \t AVE \n “;
for( i = 0; i <50; i++)
{
for( j= 0; j < 2; j ++)
cout<<” “<<left<<setw(5)<<Record[i][j];
cout<<endl;
}

system(“pause”);
return 0;
}
// <<----------------FUNCTION DEFINITION OF FUNCTION AVE----------------->>
double AVE( int a, int b, int c, int d)
{ double A;
A = ( a + b +2*c + 2 *d)/6.0 );
return A;
}

PROBLEM 2 : (Filename : Array_2_2D) Modify Problem1 (Array_1_2D) so that the


AVE column of the Record Array is sorted in descending order, then, print the
content of this Record Array with the header : STUDENT NO. AVERAGE. Make
another print out of the Record Array, but this time it is the student no. data that
is sorted in ascending order, with the same header of : STUDENT NO. AVERAGE.

PROBLEM 3 (Filename : ARRAY_3_2D) Modify Problem1 (Array_1_2D) to use


only 1 two-dimensional array, named, Record[50][6], where the Student No. will
be stored on the 1st column element of the Record array and the average grade
will be stored on the last column element of the array. Then, print the content of
the array with the header : STUDENT NO. Q1 Q2 PE FE AVE.
PROBLEM 4: Write a complete program (Filename : ARRAY_4_2D ) that will load a
Two-Dimensional array A[5][5] with the first 25 numbers, then print the
following :
a.) the content of the original array
b.) the content of the array with the elements of the 2nd row and 4th row
interchanged.
c.) the content of the array with the elements of the 3rd col and 5th col
interchanged.
d.) the content of the array with the 2 diagonal elements interchanged.
SAMPLE OUTPUT :

<- - - - - OUTPUT NO.1 : ORIGINAL ARRAY ELEMENTS - - - - - >

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

<- - - - - OUTPUT NO.2 : INTERCHANGE ROW 2 WITH ROW 4 ELEMENTS - - - - - >

1 2 3 4 5

16 17 18 19 20

11 12 13 14 15

6 7 8 9 10

21 22 23 24 25

<- - - - - OUTPUT NO.3 : INTERCHANGE COL 3 WITH COL 5 ELEMENTS - - - - - >

1 2 5 4 3

6 7 10 9 8

11 12 15 14 13

16 17 20 19 18

21 22 25 24 23

<- - - - - OUTPUT NO.4 : INTERCHANGE THE DIAGONAL ELEMENTS - - - - - >

5 2 3 4 1

6 9 8 7 10

11 12 13 14 15

16 19 18 17 20

25 22 23 24 21
X
b.) ANALYSIS FOR INTERCHANGING THE 2ND ROW AND 4TH ROW ELEMENTS :

0 1 2 3 4 SUBSCRIPTS FOR :
0 ROW 2 ELEMENTS ROW 3 ELEMENTS
1 X X X X X [1 ][ 0 ] [3 ][ 0 ]
2 [1 ][ 1 ] [3 ][ 1 ]
3 0 0 0 0 0 [1 ][ 2 ] [3 ][ 2 ]
4 [1 ][ 3 ] [3 ][ 3 ]
[1 ][ 4 ] [3 ][ 4 ]
for( j = 0; j<= 4; j++ )
{ temp = A[1][j] ; A[1 ][ j ] A[3][ j ]
A[1][j] = A[3][ j ];
A[3][ j ] = temp; where : j = 0 to 4
}

c.) ANALYSIS FOR INTERCHANGING THE 3RD AND 5TH COLUMN ELEMENTS :

0 1 2 3 4
0 X 0 SUBSCRIPTS FOR :
1 X 0 COL 3 ELEMENTS COL 5 ELEMENTS
2 X 0 [0 ][ 2 ] [0 ][ 4 ]
3 X 0 [1 ][ 2 ] [1 ][ 4 ]
4 X 0 [2 ][ 2 ] [2 ][ 4 ]
[3 ][ 2 ] [3 ][ 4 ]
for( i = 0; i<= 4; i++ ) [4 ][ 2 ] [4 ][ 4 ]
{ temp = A[i][2] ;
A[i][2] = A[i][ 4 ]; A[i ][ 2 ] A[i][ 4 ]
A[i][ 4 ] = temp;
} where : i = 0 to 4

d.) ANALYSIS FOR INTERCHANGING THE 2 DIAGONAL ELEMENTS :

0 1 2 3 4
0 X 0 SUBSCRIPTS FOR :
X DIAGONAL 0 DIAGONAL
1 X 0 ELEMENTS ELEMENTS
2 X [0 ][ 0 ] [0 ][ 4 ]
3 0 X [1 ][ 1 ] [1 ][ 3 ]
4 0 X [2 ][ 2 ] [2 ][ 2 ]
[3 ][ 3 ] [3 ][ 1 ]
for( i = 0, k=4; i<= 4; i++, k-- ) [4 ][ 4 ] [4 ][ 0 ]
{ temp = A[i][i] ;
A[i][i] = A[i][k ]; A[i ][ j ] A[i][ k ]
A[i][k ] = temp;
} where : i = 0 to 4
j = 0 to 4 i=j
k = 4 to 0
PROBLEM 5: (Filename: Array_5_2D) Write a complete program to determine the
resultant of N, number of forces, where N is to be entered from the keyboard.
The magnitude and direction of each force should be entered from the keyboard.
Create a 2-Dimensional array, F[50][4], to store the magnitudes of force on the 1st
col, the angles on the 2nd column , X-components on the 3rd column and y
components of the force on the 4th column of this array. Print the content of this
array in 4 columns with the header :” FORCE DIRECTION Fx Fy “ .Below
the Table, print the sum of all Fx and the sum of all Fy, and the computed value of
the resultant and its direction.

PROBLEM 6 : (FILENAME : ARRAY_6_2D )


Complete the program below by inserting program segment inside the
rectangular block provided. The action to be done by your program segment
should be in accordance with the instructions (line starts with // )provided at the
top of, or before the rectangular block.
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - - *
* MANIPULATING TWO-DIMENSIONAL ARRAYS *
* FILENAME : ARRAY_6_2D *
* BY : A. MORSIQUILLO *
* - - - - - - - - - - - - - - - - - - - - - - - - - -- - - - - - - - - - -- - - - - - - - - - - - */
#include<iostream>
using namespace std;
void LoadArray(int X[][5], int ROW);
void PrintArray(int X[][5], int ROW); void ReplaceOdds(int X[][5], int ROW);
int CountNonZeros(int X[][5], int ROW);
void Interchange(int X[][5], int ROW, int COL1,int COL2);
void AddRows(int X[][5], int ROWi,int ROWk);
void DivideRow(int X[][5], int ROW);
void EliminateColumn(int X[][5], int ROWi,int ROWk);

int main()
{
int A[4][5] = {0}, sum=0;
LoadArray(A,4);
cout<<"\n<- - - - - OUTPUT NO.1 : ORIGINAL ARRAY ELEMENTS - - - - - >\n";
PrintArray(A,4);
cout<<"\n<- - - - - - - - - END OF OUTPUT 1 - - - - - - - - - - - ->\n";
cout<<"\n<- - - OUTPUT NO.2 : REPLACE ODD ELEMENTS WITH ZERO- - - >\n\n";
// call function ReplaceOdds to replace all odd //elements with zero
/* The function definition for ReplaceOdds should contain the following program
segment :
int i, j;
for (i= 0; i < 4; i++)
{
for(j= 0; j<5; j++)
{ if( (A[i][j] %2) !=0)
A[i][j] = 0;
}
}
*/

PrintArray(A,4);
// Count all the the non-zero elements :
// call function CountNonZeros and store the return // value to ctr

/* The function definition for CountNonzeros should contain the following


program segment :

int ctr = 0;
for (i= 0; i < 4; i++)
{
for(j= 0; j<5; j++)
{ if( A[i][j] !=0)
ctr = ctr + 1;
}
}

cout<<"Total number of non-zero elements = "<<ctr<<endl;


cout<<"\n<
*/ - - - - - - - - - END OF OUTPUT 2 - - - - - - - - - - ->\n";

cout<<"\n<- - - OUTPUT NO.3 : INTERCHANGE COL 0 WITH COL 1 ELEMENTS -


>\n\n";
LoadArray(A,4); // restore the original array elements

// call function Interchange to INTERCHANGE COL 0


// WITH COL 1 ELEMENTS

/* The definition for function Interchange should contain the following


program segment :

int temp;
for (i= 0; i < 4; i++)
{
temp = A[i][0];
A[i][0] = A[i][1];
A[i][1] = temp;
}
*/
PrintArray(A,4);
cout<<"\n<- - - - - - - - - - -END OF OUTPUT 3 - -- - - - - - - - ->\n";
cout<<"\n<- - OUTPUT NO.4 : ADD ROW 1 ELEMENTS TO ROW 0 ELEMENTS - -
>\n\n";
LoadArray(A,4); // restore the original array elements

// call function AddRows to ADD ROW 1 ELEMENTS TO ROW // 0 ELEMENTS

/* The definition for function AddRows should contain the following program
segment :

for (j= 0; j < 5; j++)


{
A[0][j] += A[1][j];
}
*/

PrintArray(A,4);
cout<<"\n<- - - - - - - - - --END OF OUTPUT 4 - - - - - - - - - - - ->\n";
cout<<"\n<- - OUTPUT NO. 5 : DIVIDE ALL ROW ELEMENTS BY A[i][i] - >\n\n";
LoadArray(A,4); // restore the original array elements

// call function DivideRow to DIVIDE ALL ROW


// ELEMENTS BY A[i][i], that is all elements of the 1st // row to be divided by
A[0][0]. All elements of the 2nd // row to be divided by A[1][1], all elements of the
3rd // row to be divided by A[2][2] and so on…

/* The definition for the function DivideRow should contain the following
program segment :

int p;
for (i= 0; i < 4; i++)
{
p = A[i][i];
for (j= 0; j < 5; j++)
{
A[i][j] = A[i][j]/p;
}
}
*/
PrintArray(A,4);
cout<<"\n<- - - - - - - - - -END OF OUTPUT 5 - - - - - - - - - - - ->\n";
cout<<"\n<- - - OUTPUT NO.6: ELIMINATE COL 0 ELEMENT OF ROW 2 - - >\n\n";
LoadArray(A,4);
// call function EliminateColumn to ELIMINATE COL 0

// ELEMENT OF ROW 2

/* The definition for function EliminateColumn should contain the following program
segment :

p = -1 * A[2][0];
for (j= 0; j < 5; j++)
{
A[2][j] += A[0][j] * p;
}
*/

PrintArray(A,4);

cout<<"\n<- - - - - - - - - - END OF OUTPUT 6 : - - - - - - - - - >\n";

system("pause");

return 0;

// FUNCTION DEFINITIONS :

void LoadArray(int X[][5], int ROW)

{ int i, j,k = 1;

for (i= 0; i < ROW; i++)

for(j= 0; j<5; j++)

X[i][j] = k++;

return;

void PrintArray(int X[][5], int ROW)


{ int i, j,k = 1;
for (i= 0; i < ROW; i++)
{ for(j= 0; j<5; j++)
cout<<X[i][j]<<"\t";
cout<<endl;
}
cout<<endl<<endl;
return;
}
// Wite the function definitions for the following :
void ReplaceOdds(int X[][5], int ROW)

int CountNonZeros(int X[][5], int ROW)

void Interchange(int X[][5], int ROW, int COL1,int COL2)

void AddRows(int X[][5], int ROWi,int ROWk)

void DivideRow(int X[][5], int ROW)


void EliminateColumn(int X[][5], int ROWi,int ROWk)

// END OF PROGRAM
// OUTPUT OF ARRAY_6_2D

<- - - - - OUTPUT NO.1 : ORIGINAL ARRAY ELEMENTS - - - - - >

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

<- - - - - - - - - - - - - END OF OUTPUT 1 - - - - - - - - - - - - ->

<- - - - - OUTPUT NO.2 : REPLACE ODD ELEMENTS WITH ZERO- - - - - >

0 2 0 4 0

6 0 8 0 10

0 12 0 14 0

16 0 18 0 20

Total number of non-zero elements = 10

< - - - - - - - - - - - - END OF OUTPUT 2 - - - - - - - - - - - ->

<- - - OUTPUT NO.3 : INTERCHANGE COL 0 WITH COL 1 ELEMENTS - - - >

2 1 3 4 5

7 6 8 9 10

12 11 13 14 15

17 16 18 19 20

<- - - - - - - - - - - - -END OF OUTPUT 3 - - - - - - - - - - - - ->

<- - - OUTPUT NO.4 : ADD ROW 1 ELEMENTS TO ROW 0 ELEMENTS - - - >

7 9 11 13 15

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20
<- - - - - - - - - - - - -END OF OUTPUT 4 - - - - - - - - - - - - ->

<- - - OUTPUT NO. 5 : DIVIDE ALL ROW ELEMENTS BY A[i][Ii] - - >

1 2 3 4 5

0 1 1 1 1

0 0 1 1 1

0 0 0 1 1

<- - - - - - - - - - - - -END OF OUTPUT 5 - - - - - - - - - - - - ->

<- - - OUTPUT NO.6: ELIMINATE COL 0 ELEMENT OF ROW 2 - - >

1 2 3 4 5

6 7 8 9 10

0 -10 -20 -30 -40

16 17 18 19 20

<- - - - - - - - - - - - - END OF OUTPUT 6 : - - - - - - - - - - - - >

Press any key to continue . . .

Das könnte Ihnen auch gefallen