Arrays
Multidimensional Arrays
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.
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.
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
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 ];
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
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
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;
}
}
Output:
Multidimensional Arrays
int main() {
const int ROW = 4;
const int COLUMN = 3;
int matrix[ROW][COLUMN];
for(int a = 0; a < ROW; a++)
{
for(int b = 0; b < COLUMN; b++)
{
cout << "Enter ROW " << a+1 << " COLUMN "<<b+1<<" ";
cin>>matrix[a][b];
}
}
for(int a = 0; a < ROW; a++) //display array contents
{
for(int b = 0; b < COLUMN; b++)
cout << matrix[a][b] << "t";
cout << endl;
}
}
Multidimensional Arrays
Output
Enter ROW 1 COLUMN 1 1
Enter ROW 1 COLUMN 2 2
Enter ROW 1 COLUMN 3 3
Enter ROW 2 COLUMN 1 4
Enter ROW 2 COLUMN 2 5
Enter ROW 2 COLUMN 3 6
Enter ROW 3 COLUMN 1 7
Enter ROW 3 COLUMN 2 8
Enter ROW 3 COLUMN 3 9
Enter ROW 4 COLUMN 1 10
Enter ROW 4 COLUMN 2 11
Enter ROW 4 COLUMN 3 12
1 2 3
4 5 6
7 8 9
10 11 12

19-Lec - Multidimensional Arrays.ppt

  • 1.
  • 2.
    Practice Problems  Writea 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  Writea 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  Arrayscan 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  Atwo-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  Everyelement 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-dimensionedarrays 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; } }
  • 9.
  • 10.
    Multidimensional Arrays int main(){ const int ROW = 4; const int COLUMN = 3; int matrix[ROW][COLUMN]; for(int a = 0; a < ROW; a++) { for(int b = 0; b < COLUMN; b++) { cout << "Enter ROW " << a+1 << " COLUMN "<<b+1<<" "; cin>>matrix[a][b]; } } for(int a = 0; a < ROW; a++) //display array contents { for(int b = 0; b < COLUMN; b++) cout << matrix[a][b] << "t"; cout << endl; } }
  • 11.
    Multidimensional Arrays Output Enter ROW1 COLUMN 1 1 Enter ROW 1 COLUMN 2 2 Enter ROW 1 COLUMN 3 3 Enter ROW 2 COLUMN 1 4 Enter ROW 2 COLUMN 2 5 Enter ROW 2 COLUMN 3 6 Enter ROW 3 COLUMN 1 7 Enter ROW 3 COLUMN 2 8 Enter ROW 3 COLUMN 3 9 Enter ROW 4 COLUMN 1 10 Enter ROW 4 COLUMN 2 11 Enter ROW 4 COLUMN 3 12 1 2 3 4 5 6 7 8 9 10 11 12