Beruflich Dokumente
Kultur Dokumente
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 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):
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.
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;
}
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
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
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
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
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.
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
int ctr = 0;
for (i= 0; i < 4; i++)
{
for(j= 0; j<5; j++)
{ if( A[i][j] !=0)
ctr = ctr + 1;
}
}
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
/* The definition for function AddRows should contain the following program
segment :
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
/* 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);
system("pause");
return 0;
// FUNCTION DEFINITIONS :
{ int i, j,k = 1;
X[i][j] = k++;
return;
// END OF PROGRAM
// OUTPUT OF ARRAY_6_2D
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
0 2 0 4 0
6 0 8 0 10
0 12 0 14 0
16 0 18 0 20
2 1 3 4 5
7 6 8 9 10
12 11 13 14 15
17 16 18 19 20
7 9 11 13 15
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
<- - - - - - - - - - - - -END OF OUTPUT 4 - - - - - - - - - - - - ->
1 2 3 4 5
0 1 1 1 1
0 0 1 1 1
0 0 0 1 1
1 2 3 4 5
6 7 8 9 10
16 17 18 19 20