Lecture 8
Lecture 8
Lecture 8
FUNDAMENTALS LAB
Engr. Muhammad Usman
INTRODUCTION TO
MULTIDIMENSIONAL
ARRAYS
Engr. Muhammad Usman
OBJECTIVES
• Data in multidimensional arrays are stored in tabular form (in row-column order).
• A three-dimensional (3D) array is an array of arrays of arrays or Array of two 2-D arrays.
• In C++ programming an array can have two, three, or even ten or more dimensions.
• More dimensions in an array means more data to be held, and also means greater difficulty
in managing and understanding arrays.
DECLARING MULTIDIMENSIONAL ARRAYS
• The arrays can be used in various dimensions depending upon the need such as a 1D
array, 2D array, or 3D array.
• The more dimensions an array has, the more complex it becomes to deal with the
programming.
• Here we discuss the difference between the multidimensional array types.
1.One-dimensional array:
• int one_d_array [2];
• 2 Elements
CALCULATING THE TOTAL NUMBER OF ELEMENTS IN
MULTIDIMENSIONAL ARRAYS
2. Two-dimensional array:
• int one_d_array [2][3];
• 2x3=6 Elements
3. Three-dimensional array:
• int one_d_array [2][3][2];
• 2x3x2=12 Elements
STORING AND PRINTING ELEMENTS IN 2D ARRAY
Elements in Two-Dimensional arrays are accessed using the row indexes and
column indexes.
Example:
int x[2][1];
The above example represents the element present in the third row and second
column.
CLASS ASSIGNMENT
#include <iostream>
using namespace std;
int main()
{
// an array with 3 rows and 2 columns.
int x[3][2] = {{0,1}, {2,3}, {4,5}};
CLASS ASSIGNMENT
return 0;
}
CLASS ASSIGNMENT
#include <iostream>
using namespace std;
int main()
{
int arr[2][4],i,j;
for (i=0;i<2;i++)
for (j=0;j<4;j++)
CLASS ASSIGNMENT
{
cout<<"Enter an Integer=";
cin>>arr[i][j];
}
for (i=0;i<2;i++)
CLASS ASSIGNMENT
{
for (j=0;j<4;j++)
cout<<arr[i][j]<<"\t";
cout<<endl;
}
}
CLASS ASSIGNMENT
#include <iostream>
using namespace std;
int main()
{
int arr[3][4]={{15,21,90,85},{12,22,32,56},{11,21,77,15}};
int i,j,max,min;
max=min=arr[0][0];
CLASS ASSIGNMENT
for (i=0;i<3;i++)
for (j=0;j<4;j++)
{
if (arr[i][j]>max)
max=arr[i][j];
if (arr[i][j]<min)
min=arr[i][j];
}
CLASS ASSIGNMENT
Write a Program in C++ That inputs integer values in 4*4 Matrix and
displays the sum of the diagonal Elements of the Matrix.
#include <iostream>
using namespace std;
int main()
{
int x[2][3][2] =
{
{ {0,1}, {2,3}, {4,5} },
{ {6,7}, {8,9}, {10,11} }
};
CLASS ASSIGNMENT
•{
• cout << "Element at x[" << i << "][" << j << "][" << k << "] = " << x[i][j][k]<<
endl;
• }
• }
• }
• return 0;
• }