2. Practice Problems
Write a C++ program to find the sum and average of one
dimensional integer array.
Write a C++ program to swap first and last element of an
integer array.
Write a C++ program to reverse the element of an integer
array.
Write a C++ program to find the largest and smallest element
of an array.
3. Practice Problems
Write a C++ program to find the largest and second largest
element of an array.
Write a C++ program to find Even and Odd elements in an
array.
Write a C++ program to determine whether elements of an
array are prime numbers.
Item_Code is one-dimensional array of integers. Write a C++
function to efficiently search for a data Val from Item_Code.
If Val is present in the array then the function should return
value true/1 and false/0 otherwise.
4. Multidimensional Arrays
Arrays can be multidimensional.
A two dimensional array consists of Rows and
Columns as we have them in a matrix.
To declare a two-dimensional array of size x, y:
data_type arrayName [ x ] [ y ];
Where data_type can be any valid C++ data type
and arrayName will be a valid C++ identifier.
x represents number of Rows and y represents
number of Columns
5. Multidimensional Arrays
A two-dimensional array can be think as a table, which
will have x number of rows and y number of columns. A
2-dimensional array a of type float, which contains
three rows and four columns can be shown as below:
float a [ 3 ] [ 4 ];
6. Multidimensional Arrays
Every element in array a is identified by an element
name of the form a[ i ][ j ], where a is the name of the
array, and i and j are the subscripts that uniquely identify
each element in a.
Initializing Two-Dimensional Arrays
7. Multidimensional Arrays
Multi-dimensioned arrays may be initialized by specifying
bracketed values for each row. Following are some examples
for declaration and initialization.
int val[3][4] = {
{0, 1, 2, 3} , //initializers for row indexed by 0
{4, 5, 6, 7} , //initializers for row indexed by 1
{8, 9, 10, 11} //initializers for row indexed by 2
};
int b[4][4] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int c[2][5] = {0}; //initialize all elements with 0
8. Example:
int main()
{
int rows=4, cols=2;
float array[rows][cols] = { {17.0, 25.5},
{23.95,41.25}, {37.25,60.5},{40.0,81.35}};
// output each array element's value
for ( int i = 0; i < rows; i++ )
for ( int j = 0; j < cols; j++ ) {
cout << "array[" << i << "][" << j << "]: ";
cout << array[i][j]<< endl;
}
}