The document discusses two-dimensional arrays in C++. It defines two-dimensional arrays as lists of one-dimensional arrays that can represent tabular data. Two-dimensional arrays have rows and columns, with each element accessed using two indices like array[row][column]. The document provides examples of declaring, initializing, and accessing elements of two-dimensional arrays. It also demonstrates programs to input and output 2D array values and find the maximum/minimum element in a 2D array.
1. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming FundamentalsLecture 11: Programming Fundamentals
Lecture 10
2D-Array
1
2. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Introduction
• Two-dimensional arrays, the most common
multidimensional arrays, are used to store
information that we normally represent in table
form.
• Two-dimensional arrays, like one-dimensional
arrays, are homogeneous.
• This means that all of the data in a two-
dimensional array is of the same type.
• Example:
– a seating plan for a room (organized by rows and columns).
– a grade book where rows might correspond to individual students
and columns to student scores.
2
3. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Introduction
• The simplest form of the multidimensional array is the two-dimensional
array.
• A two-dimensional array is, in essence, a list of one-dimensional arrays.
• To declare a two-dimensional integer array of size x,y you would write
something as follows:
type arrayName [ x ][ y ];
• A two dimensional array can be think as a table which will have x number
of rows and y number of columns.
• A 2-dimentional array a which contains three rows and four columns can
be shown as below:
• Thus, 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.
3
4. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
• A two dimensional array can be think as a table which will have x
number of rows and y number of columns.
• A 2-dimentional array a which contains three rows and four
columns can be shown as below:
• Thus, 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.
4
5. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Initializing 2D-Array
• Multi dimensioned arrays may be initialized by
specifying bracketed values for each row.
Following is an array with 3 rows and each row
have 4 columns.
• For Example:
» int a[3][4] = { {0, 1, 2, 3} , {4, 5, 6, 7} ,{8, 9, 10, 11} };
• The nested braces, which indicate the intended
row, are optional.
• The following initialization is equivalent to
previous example:
» int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
5
6. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Write a program that initializes 2D array
having two rows and three columns and then
displays its values.
6
7. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
#include<iostream.h>
#include<conio.h>
main()
{
int i, j;
int array[2][4]={{10,21,9,84},{33,72,48,17}};
for(i=0; i<2; i++)
for(j=0; j<4; j++)
{
cout<<"Position "<<i<<" "<<j<<" = "<<array[i][j]<<endl;
}
getch();
}
7
8. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Write a program that initialize a 2D array of 4
rows and 2 columns and then displays the
maximum and minimum number in array.
8
9. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
main()
{
int i, j, max, min;
int array[2][4]={{10,21,9,84},{33,72,48,17}};
max=min=array[0][0];
for(i=0; i<2; i++)
for(j=0; j<4; j++)
{
if(array[i][j]>max)
max=array[i][j];
if(array[i][j]<min)
min=array[i][j];
}
cout<<"Maximum valu in array is "<<max<<endl;
cout<<"Minimum valu in array is "<<min;
getch();
}
9
10. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example
#include <iostream.h>
#include<conio.h>
main()
{
int i, j;
int sqrs[10][2] = { {1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}, {6, 36},{7, 49},{8, 64},{9, 81},{10, 100} };
cout << "Enter a number between 1 and 10: ";
cin >> i;
for(j=0; j<10; j++)
{
if(sqrs[j][0]==i) break;
}
cout << "The square of " << i << " is ";
cout << sqrs[j][1];
getch();
}
10
11. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Declaring 2D-Array
• Two-dimensional arrays are declared by
specifying the number of rows then the
number of columns.
• For Example:
» int a[30][10]; // declares an integer array of 30 rows and
10 columns.
» char table[3][3]; // three rows and three columns of
characters.
11
12. University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming Fundamentals
Example
Program to stores values in an array of 2 Rows and four Columns.
int i,j;
int array[2][4];
for(i=0; i<2; i++)
for(j=0; j<4; j++)
{
cout<<"Enter an Integer ";
cin>>array[i][j];
}
for(i=0; i<2; i++)
for(j=0; j<4; j++)
{
cout<<array[i][j]<<"t";
}
getch();
}
12