SlideShare a Scribd company logo
1 of 12
University Institute of Information Technology, PMAS-AAUR
Lecture 10: Programming FundamentalsLecture 11: Programming Fundamentals
Lecture 10
2D-Array
1
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
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
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
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
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
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
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
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
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
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
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

More Related Content

What's hot

Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
Swarup Kumar Boro
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
topu93
 

What's hot (20)

Arrays in c
Arrays in cArrays in c
Arrays in c
 
Array in (C) programing
Array in (C) programing Array in (C) programing
Array in (C) programing
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
concept of Array, 1D & 2D array
concept of Array, 1D & 2D arrayconcept of Array, 1D & 2D array
concept of Array, 1D & 2D array
 
Array in C
Array in CArray in C
Array in C
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Lecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional ArrayLecture 16 - Multi dimensional Array
Lecture 16 - Multi dimensional Array
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Array
ArrayArray
Array
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
Array in c language
Array in c languageArray in c language
Array in c language
 
SPL 12 | Multi-dimensional Array in C
SPL 12 | Multi-dimensional Array in CSPL 12 | Multi-dimensional Array in C
SPL 12 | Multi-dimensional Array in C
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 

Similar to 2D-Array

array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
HEMAHEMS5
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 

Similar to 2D-Array (20)

Arrays
ArraysArrays
Arrays
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Array Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional arrayArray Introduction One-dimensional array Multidimensional array
Array Introduction One-dimensional array Multidimensional array
 
array-191103180006.pdf
array-191103180006.pdfarray-191103180006.pdf
array-191103180006.pdf
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Array
ArrayArray
Array
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016) Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016)
 
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
Arrays in C++ in Tamil - TNSCERT SYLLABUS PPT
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
C (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptxC (PPS)Programming for problem solving.pptx
C (PPS)Programming for problem solving.pptx
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 
Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional Arrays 1D and 2D , and multi dimensional
Arrays 1D and 2D , and multi dimensional
 
Lesson 18-20.pptx
Lesson 18-20.pptxLesson 18-20.pptx
Lesson 18-20.pptx
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
 
02 arrays
02 arrays02 arrays
02 arrays
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 

More from ALI RAZA

Programming Fundamentals using C++
Programming Fundamentals using C++Programming Fundamentals using C++
Programming Fundamentals using C++
ALI RAZA
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
ALI RAZA
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
ALI RAZA
 
Dil hua kirchi kirchi by mohammad iqbal shams
Dil hua kirchi kirchi by mohammad iqbal shamsDil hua kirchi kirchi by mohammad iqbal shams
Dil hua kirchi kirchi by mohammad iqbal shams
ALI RAZA
 
Pathar kar-do-ankh-mein-ansu-complete
Pathar kar-do-ankh-mein-ansu-completePathar kar-do-ankh-mein-ansu-complete
Pathar kar-do-ankh-mein-ansu-complete
ALI RAZA
 

More from ALI RAZA (20)

Structure
StructureStructure
Structure
 
Recursion
RecursionRecursion
Recursion
 
pseudocode and Flowchart
pseudocode and Flowchartpseudocode and Flowchart
pseudocode and Flowchart
 
Algorithm Development
Algorithm DevelopmentAlgorithm Development
Algorithm Development
 
Programming Fundamentals using C++
Programming Fundamentals using C++Programming Fundamentals using C++
Programming Fundamentals using C++
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
 
Introduction to Programming
Introduction to ProgrammingIntroduction to Programming
Introduction to Programming
 
Array sorting
Array sortingArray sorting
Array sorting
 
Array programs
Array programsArray programs
Array programs
 
Quiz game documentary
Quiz game documentaryQuiz game documentary
Quiz game documentary
 
Function pass by value,function pass by reference
Function pass by value,function pass by reference Function pass by value,function pass by reference
Function pass by value,function pass by reference
 
Drug Addiction 39 Slides
Drug Addiction 39 SlidesDrug Addiction 39 Slides
Drug Addiction 39 Slides
 
Drug Addiction Original 51 Slides
Drug Addiction Original 51 SlidesDrug Addiction Original 51 Slides
Drug Addiction Original 51 Slides
 
Passing stuctures to function
Passing stuctures to functionPassing stuctures to function
Passing stuctures to function
 
Basic general knowledge
Basic general knowledgeBasic general knowledge
Basic general knowledge
 
Dil hua kirchi kirchi by mohammad iqbal shams
Dil hua kirchi kirchi by mohammad iqbal shamsDil hua kirchi kirchi by mohammad iqbal shams
Dil hua kirchi kirchi by mohammad iqbal shams
 
Pathar kar-do-ankh-mein-ansu-complete
Pathar kar-do-ankh-mein-ansu-completePathar kar-do-ankh-mein-ansu-complete
Pathar kar-do-ankh-mein-ansu-complete
 
Husne akhlaq
Husne akhlaqHusne akhlaq
Husne akhlaq
 
Parts of speech sticky note definitions and examples
Parts of speech sticky note definitions and examplesParts of speech sticky note definitions and examples
Parts of speech sticky note definitions and examples
 
Quik tips
Quik tipsQuik tips
Quik tips
 

Recently uploaded

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 

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