
2D Arrays in C++ Programming
Master the concepts of two-dimensional arrays in C++ with practical examples like reading arrays, finding the largest number, exchanging columns, switching rows, and converting 2D arrays to 1D. Enhance your understanding of array manipulation and traversal techniques.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
ARRAYS : TWO DIMENSION (2-D) IN C++ LANGAUGE Setting By : A. L. Waleed Rasheed Twelfth Lecture
Arrays : two dimension (2-D) int Arr[3][4]; int arr[3][4] = { {26, 34, 22, 17}, {24, 32, 19, 13}, {28, 38, 25, 20} }; 26 24 28 34 32 38 22 19 25 17 13 20 for ( i = 0; i < n; ++i) for ( j = 0; j < m; ++j) Cin>>arr[i][j]; for ( i = 0; i < n; ++i) { for ( j = 0; j < m; ++j) cout<<arr[i][j]<<"\t"; cout<<"\n"; }
EX//W.P. to read integer array[3,3] and find largest No.? #include<iostream.h> main() { const int n = 3; int arr[n][n]; //read array 2_D for ( i = 0; i < n; ++i) for ( j = 0; j < n; ++j) cin>>arr[i][j]; //Find largest No. in array larg=arr[0][0]; for ( i = 0; i < n; ++i) for ( j = 0; j < n; ++j) if (arr[i][j]>larg) cout<<"Largest No.= "<<larg; } int i,j,larg; larg=arr[i][j];
EX// W. P. to exchange 2nd column with 5th column of array s[5,7]? #include <iostream.h> main() { int s[5][7],i,j,t; for(i=0;i<5;i++) // Read array 2_D, s[5,7] for(j=0;j<7;j++) cin>>s[i][j]; 2 5 for(i=0;i<5;i++) // exchange operation { t=s[i][1]; s[i][1]=s[i][4]; s[i][4]=t; } for(i=0;i<5;i++) // print array s[5,7] after exchange { for(j=0;j<7;j++) cout<<" "<<s[i][j]; cout<<"\n"; } }
Q / write program section to switch the second column place the third row in the matrix A[5,5]? for (i=0;i<5;i++) for (j=0;j<5;j++) if (i==j) { t=a[i][1]; a[i][1]=a[2][j]; a[2][j]=t; } j 0 1 2 3 4 i 0 1 2 3 4
Q/W.P. to convert array 2-D a[3][3] to 1-D? #include<iostream.h> void main() { const int n=3; int a[n][n]={{1,2,3},{4,5,6},{7,8,9}}; int b[9]; int i,j,k=0; for(i=0;i<n;i++) for(j=0;j<n;j++) { b[k]=a[i][j]; k++;} for(i=0;i<9;i++) cout<<" "<<b[i]; }
Homework Q1 / write program section to switch the second row place fourth row in the matrix A[5,5]? Q2/W.P. to convert array 1-D a[9] into array 2-D? Q3/W.P. to find largest No. to each row in array a[3][3]? Q4/W.P. to calculate summation to each row in array a[3][3]?