Array of Two Dimension
Array of Two Dimension
Array of Two Dimension
1 2 3
4 5 6
#include<iostream.h>
void main ( )
{
int a [ 3 ] [ 4 ];
int b [ 12 ];
int i , j, k = 0;
for ( i = 0 ; i < 3; i++ )
for ( j = 0 ; j < 4; j++ )
cin >> a [ i ] [ j ];
for ( i = 0 ; i < 3; i++ )
for ( j = 0 ; j < 4; j++ )
{
b [ k ] = a [ i ] [ j ];
k++;
}
for ( i = 0 ; i < k; i++ )
cout << b [ i ];
}
Example 7
Ex7:-Write C++ program, to replace each element in the main
diameter (diagonal) with zero?
#include<iostream.h>
void main ( )
{
int a [ 3 ] [ 3 ];
int i , j;
for ( i = 0 ; i < 3; i++ )
for ( j = 0 ; j < 3; j++ )
cin >> a [ i ] [ j ];
for ( i = 0 ; i < 3; i++ )
for ( j = 0 ; j < 3; j++ )
if ( i == j ) a [ i ] [ j ] = 0;
for ( i = 0 ; i < 3; i++ )
{
for ( j = 0 ; j < 3; j++ )
cout << a [ i ] [ j ];
cout << endl;
}
}
Example
Ex8:- Write C++ program, print the square root of an array?
#include<iostream.h>
void main ( )
{
int a [ 3 ] [ 3 ] , b [ 3 ] [ 3 ];
int i , j;
for ( i = 0 ; i < 3; i++ ) {
for ( j = 0 ; j < 3; j++ ) {
b[ i ][ j ]= sqrt(a[ i ][ j ]);
cout << b [ i ] [ j ];
}} }
Example 9
Ex9:- Write C++ program, to read 3*3 2D-array, then find the summation of the
main diagonal and its secondary diagonal of the array elements, finally print
these elements:
#include<iostream.h>
void main ( )
{
int a [ 3 ] [ 3 ];
int i , j, x , y;
for ( i = 0 ; i < 3; i++ ) {
for ( j = 0 ; j < 3; j++ ) {
cin >> a [ i ] [ j ];
if ( i == j )
x=x+a[ i ][ j ];
if ( i + j =4)
y=y+a[ i ][ j ];
}}
cout << “summation of diagonal is: “ << x << endl;
cout << “summation of inverse diagonal is: “ << y << endl;
}
WORK SHEET (6)
Arrays
Q1: Write a C++ program, using function, to find if the array’s elements are
in order or not.
Q2: Write a C++ program, using function, to compute the number of zeros
in the array.
Q3: Write a C++ program, using function, to find the value of array C from
add array A and array B. C[ i ] = A [ i ] + B [ i ];
Q4: Write a C++ program, using function, to multiply the array elements
by2. A[ i ] = A [ i ] * 2;
Q5: Write a C++ program, using function, to reads temperatures over the
30 days and calculate the average of them.
Q6: Write a C++ program, using function, to merge two arrays in one array.
Q7: Write C++ program, to read 3*4 2D-array, then find the summation of
each col.
Q8: Write C++ program, to replace each element in the second diameter
(diagonal) with zero.
Q9: Write C++ program, to replace the elements of the main diameter with
the elements of the second diameter.
Q10: Write C++ program, to find the summation of odd numbers in 2D-array.
Q11: Write C++ program, to find (search) X value in 2D-array, and return
The index of it’s location.
Q12: Write C++ program, to convert 1D-array that size [16] to 2D-array
That size of [4] [4].
Q17: Write C++ program, to exchange row1 and row3 in 4*3 array.
Q18: Write C++ program, to exchange row0 with col3 in 4*4 array.
Q19: Write C++ program, to find the greatest number in the second
diagonal, in 3*3 array.
Q20: Write C++ program, to read X[ n ], and rotate the elements to the
left by one position.
i.e:
Q21: Write C++ program, to read A[ n ] and a location Z then delete the
number at location Z from the array, and print the new array after
deletion.
Q22: Write C++ program to order the array in ascending and descending
order.
Q23: Write C++ program to read (n) no.s and find the average of the even
no. on it.