You are on page 1of 32

COMPUTING

FUNDAMENTALS LAB
Engr. Muhammad Usman
INTRODUCTION TO
MULTIDIMENSIONAL
ARRAYS
Engr. Muhammad Usman
OBJECTIVES

 To get a basic understanding of multidimensional arrays in C++.


 To understand the working of multidimensional arrays.
 To get hands-on experience working with multidimensional arrays .
 To declare, initialize, and access multidimensional array elements in
C++ programming with the help of examples.
 To write C++ programs using multidimensional arrays.
MULTIDIMENSIONAL ARRAYS IN C++

• In C/C++, we can define multidimensional arrays in simple words as an array of arrays.

• Data in multidimensional arrays are stored in tabular form (in row-column order).

• C++ allows for arrays of two or more dimensions.

• A two-dimensional (2D) array is an array of arrays.


MULTIDIMENSIONAL ARRAYS IN C++

• 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 general form of declaring a Multi dimensional


array is:
datatype array name[size1][size2]….[sizeN]
Datatype: Type of data to be stored in the array.
Array name: Name of the array.
Size1,Size2..SizeN: Sizes of Dimensions.
DECLARING MULTIDIMENSIONAL ARRAYS

•The declaration of each multidimensional array is discussed below:


1.One-dimensional array:
• int one_d_array [2];
2.Two-dimensional array:
• int two_d_array [2][2];
3.Three-dimensional array:
• int three_d_array [2][2][2];
VISUALIZING MULTIDIMENSIONAL ARRAYS
VISUALIZING MULTIDIMENSIONAL ARRAYS
VISUALIZING MULTIDIMENSIONAL ARRAYS
CALCULATING THE TOTAL NUMBER OF ELEMENTS IN
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

• There are two ways to store elements in 2D array


• First Method:
•int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
•The above array has 3 rows and 4 columns. The elements in the braces from left
to right are stored in the table also from left to right. The elements will be filled in
the array in order, the first 4 elements from the left in the first row, the next 4
elements in the second row, and last four elements in the last row.
STORING AND PRINTING ELEMENTS IN 2D ARRAY

• There are two ways to store elements in 2D array


• Second Method:

•int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};


•This type of initialization makes use of nested braces.
•Each set of inner braces represents one row.
•In the above example, there is a total of three rows so there are three sets of inner
braces.
ACCESSING ELEMENTS OF TWO-DIMENSIONAL ARRAYS

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

Write a Program in C++ to print the values of a 2-D array consisting of 3


rows and 2 Columns.
Hint: int x[3][2]
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

// output each array element's value


for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 2; j++)
{
cout << "Element at x[" << i<< "][" << j << "]: ";
CLASS ASSIGNMENT

cout << x[i][j]<<endl;


}
}

return 0;

}
CLASS ASSIGNMENT

Write a Program in C++ to store integer values in a 2D-Array consisting of 2


Rows and 4 Columns.
Hint int arr[2][4]
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

Write a Program in C++ That initializes a 2D array of 3 rows and 4 columns


and than displays the minimum and maximum number from the array.
Hint int arr[3][4]
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

cout<<"Maximum Value is="<<max<<endl;


cout<<"Minimum Value is="<<min<<endl;
}
HOME 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.

Date of Submission: 22-Feb-2023


CLASS ASSIGNMENT

Write a Program in C++ to print the values of a 3-D array consisting of 2


rows 3 Columns and 2 Arrays.
Hint: int x[2][3][2]
CLASS ASSIGNMENT

#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

• for (int i = 0; i < 2; ++i)


• {
• for (int j = 0; j < 3; ++j)
• {
• for (int k = 0; k < 2; ++k)

CLASS ASSIGNMENT

•{
• cout << "Element at x[" << i << "][" << j << "][" << k << "] = " << x[i][j][k]<<
endl;
• }
• }
• }
• return 0;

• }

You might also like