SlideShare a Scribd company logo
1 of 12
Two dimensional
- Two dimensional arrays are declared
similarly to one dimensional arrays:
char t[20][10]; (and referred to as a
matrix, or as a "table")
The first subscript gives the row number, and
the second subscript specifies column number.
We can also initialize a two-dimensional array
in a declaration statement; E.g.,
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
which specifies the elements in each row of the
array (the interior braces around each row of
values could be omitted). The matrix for this
example is






=
654
321
m
Specification of the number of rows could be
omitted. But other subscript specifications can
not be omitted. So, we could also write the
initialization as
int m[ ][3] = {{1, 2, 3}, {4, 5, 6}};
int a[2][3]= {1,2,3,4,5,6};
specifies 6 integer locations.
Storage for array elements are in contiguous locations in
memory in row major order (unlike column major order in
Fortran), referenced by subscripts(or index) values starting at 0
for both rows and columns.
a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2]
RAM
1 2 3 4 5 6
1. Problem Definition
Assume we have a file “in.dat” (located in the pwd) consisting
of 10 rows of 10 integers per row (100 integers total). Write a
C program which reads from the file and stores the numbers
in a two dimensional array named ‘mat’, computes the largest
of all of these numbers and prints the largest value to the
screen.
 
2. Refine, Generalize, Decompose the problem definition
(i.e., identify sub-problems, I/O, etc.)
Input = from file “in.dat”
Output= Largest of the 100 integers printed to screen.
3. Develop Algorithm
Use Unix redirection to read file “in.dat”.
Use nested for loop to go through two dimensional array to
#include <stdio.h>
void main(void)
{
int row, col, maximum;
/* Declare a 2-D array called ‘mat’ which can hold a 10-by-10 */
/* matrix of integers */
int mat[10][10];
/* Write code here to read from the file ‘in.dat’ */
for(row=0;row<10;++row)
for(col=0;col<10;++col)
scanf("%i",&mat[row][col]);
/* Write code to find the largest value in the array ‘mat’ */
maximum = mat[0][0]; /* why ??? */
for(row=0;row<10;++row) /* How does this work??? */
for(col=0;col<10;++col)
if (mat[row][col] > maximum)
maximum = mat[row][col];
/* Print the largest value to the screen */
printf(" max value in the array = %in",maximum);
}
The typedef mechanism in C allows us to define our own
data types. For example,
typedef double matrix[10][20];
In this example we create a new data-type named matrix.
We can declare a variable of data-type matrix by
matrix a;
and this declaration is equivalent to
double a[10][20];
Other examples of the use of typedef are,
typedef int blah; /* don’t do this !!!!! */
typedef char string[5];
In the first example we give another name or alias to the
data type int .
In the second example we create a new data-type named
string.
To declare a variable of data-type blah ,
blah x;
this declaration is equivalent to
int x;
To declare a variable of data-type string ,
string var ="init"; /* declare and initialize */
scanf("%s",var); /* read in a string of chars */
strcpy(var , "next");
The declaration
string morse[26];
declares morse as an array of 26 elements. Each element has
data-type string.
The declaration and initialization:
string morse[26] =
{".-", "-...", "-.-.", "-..", ".", "..-.",
"--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.", "...",
"-", "..-", "...-", ".--", "-..-", "-.--", "--.. " };
declares morse as an array of 26 elements with
morse[0] = ".-"
morse[1] = "-..." and so on...
Standard (ANSI) C allows up to 12 dimension.
But more computation is required by the system
to locate elements in multi-subscripted arrays.
Therefore, in problems involving intensive
numerical calculations, we should use arrays
with fewer dimension.
We can also use 3, 4, … dimensional arrays,
e.g.,
threeDArray [i][j][k];

More Related Content

What's hot (20)

Linked list
Linked listLinked list
Linked list
 
Queues
QueuesQueues
Queues
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Linked list
Linked listLinked list
Linked list
 
Break and continue
Break and continueBreak and continue
Break and continue
 
Managing I/O in c++
Managing I/O in c++Managing I/O in c++
Managing I/O in c++
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Array
ArrayArray
Array
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
String in c programming
String in c programmingString in c programming
String in c programming
 
Applications of stack
Applications of stackApplications of stack
Applications of stack
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 

Viewers also liked

Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional arrayRajendran
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)EngineerBabu
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : ArraysGagan Deep
 
Array in c language
Array in c languageArray in c language
Array in c languagehome
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]MomenMostafa
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-stringsPrincess Sam
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array pptsandhya yadav
 
Presentation on Logical Operators
Presentation on Logical OperatorsPresentation on Logical Operators
Presentation on Logical OperatorsSanjeev Budha
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentationNeveen Reda
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppteShikshak
 
Applications of matrices in real life
Applications of matrices in real lifeApplications of matrices in real life
Applications of matrices in real lifeSuhaibFaiz
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of GraphAbhishek Pachisia
 
Coordinate systems
Coordinate systemsCoordinate systems
Coordinate systemsSaad Raja
 

Viewers also liked (20)

2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Arrays
ArraysArrays
Arrays
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)Array Presentation (EngineerBaBu.com)
Array Presentation (EngineerBaBu.com)
 
Array in C
Array in CArray in C
Array in C
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Array in c language
Array in c languageArray in c language
Array in c language
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
Enter The Matrix
Enter The MatrixEnter The Matrix
Enter The Matrix
 
C programming & data structure [arrays & pointers]
C programming & data structure   [arrays & pointers]C programming & data structure   [arrays & pointers]
C programming & data structure [arrays & pointers]
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
2D Array
2D Array2D Array
2D Array
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Presentation on Logical Operators
Presentation on Logical OperatorsPresentation on Logical Operators
Presentation on Logical Operators
 
Application of Matrices
Application of MatricesApplication of Matrices
Application of Matrices
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Applications of matrices in real life
Applications of matrices in real lifeApplications of matrices in real life
Applications of matrices in real life
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of Graph
 
Coordinate systems
Coordinate systemsCoordinate systems
Coordinate systems
 

Similar to Two dimensional array

Similar to Two dimensional array (20)

Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Unit 2
Unit 2Unit 2
Unit 2
 
ARRAYS
ARRAYSARRAYS
ARRAYS
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Arrays
ArraysArrays
Arrays
 
3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
 
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
 
C Programming Unit-3
C Programming Unit-3C Programming Unit-3
C Programming Unit-3
 
Array &strings
Array &stringsArray &strings
Array &strings
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
6.array
6.array6.array
6.array
 
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
 
Array
ArrayArray
Array
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Array
ArrayArray
Array
 
Module7
Module7Module7
Module7
 
Lecture 15_Strings and Dynamic Memory Allocation.pptx
Lecture 15_Strings and  Dynamic Memory Allocation.pptxLecture 15_Strings and  Dynamic Memory Allocation.pptx
Lecture 15_Strings and Dynamic Memory Allocation.pptx
 

More from Rajendran

Element distinctness lower bounds
Element distinctness lower boundsElement distinctness lower bounds
Element distinctness lower boundsRajendran
 
Scheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsScheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsRajendran
 
Divide and conquer surfing lower bounds
Divide and conquer  surfing lower boundsDivide and conquer  surfing lower bounds
Divide and conquer surfing lower boundsRajendran
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statisticsRajendran
 
Proof master theorem
Proof master theoremProof master theorem
Proof master theoremRajendran
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree methodRajendran
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theoremRajendran
 
Master method
Master method Master method
Master method Rajendran
 
Master method theorem
Master method theoremMaster method theorem
Master method theoremRajendran
 
Master method theorem
Master method theoremMaster method theorem
Master method theoremRajendran
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithmsRajendran
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisRajendran
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisRajendran
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of QuicksortRajendran
 
Np completeness
Np completenessNp completeness
Np completenessRajendran
 
computer languages
computer languagescomputer languages
computer languagesRajendran
 

More from Rajendran (20)

Element distinctness lower bounds
Element distinctness lower boundsElement distinctness lower bounds
Element distinctness lower bounds
 
Scheduling with Startup and Holding Costs
Scheduling with Startup and Holding CostsScheduling with Startup and Holding Costs
Scheduling with Startup and Holding Costs
 
Divide and conquer surfing lower bounds
Divide and conquer  surfing lower boundsDivide and conquer  surfing lower bounds
Divide and conquer surfing lower bounds
 
Red black tree
Red black treeRed black tree
Red black tree
 
Hash table
Hash tableHash table
Hash table
 
Medians and order statistics
Medians and order statisticsMedians and order statistics
Medians and order statistics
 
Proof master theorem
Proof master theoremProof master theorem
Proof master theorem
 
Recursion tree method
Recursion tree methodRecursion tree method
Recursion tree method
 
Recurrence theorem
Recurrence theoremRecurrence theorem
Recurrence theorem
 
Master method
Master method Master method
Master method
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Hash tables
Hash tablesHash tables
Hash tables
 
Lower bound
Lower boundLower bound
Lower bound
 
Master method theorem
Master method theoremMaster method theorem
Master method theorem
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Longest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm AnalysisLongest common subsequences in Algorithm Analysis
Longest common subsequences in Algorithm Analysis
 
Dynamic programming in Algorithm Analysis
Dynamic programming in Algorithm AnalysisDynamic programming in Algorithm Analysis
Dynamic programming in Algorithm Analysis
 
Average case Analysis of Quicksort
Average case Analysis of QuicksortAverage case Analysis of Quicksort
Average case Analysis of Quicksort
 
Np completeness
Np completenessNp completeness
Np completeness
 
computer languages
computer languagescomputer languages
computer languages
 

Recently uploaded

Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 

Recently uploaded (20)

Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 

Two dimensional array

  • 2. - Two dimensional arrays are declared similarly to one dimensional arrays: char t[20][10]; (and referred to as a matrix, or as a "table") The first subscript gives the row number, and the second subscript specifies column number.
  • 3. We can also initialize a two-dimensional array in a declaration statement; E.g., int m[2][3] = {{1, 2, 3}, {4, 5, 6}}; which specifies the elements in each row of the array (the interior braces around each row of values could be omitted). The matrix for this example is       = 654 321 m
  • 4. Specification of the number of rows could be omitted. But other subscript specifications can not be omitted. So, we could also write the initialization as int m[ ][3] = {{1, 2, 3}, {4, 5, 6}};
  • 5. int a[2][3]= {1,2,3,4,5,6}; specifies 6 integer locations. Storage for array elements are in contiguous locations in memory in row major order (unlike column major order in Fortran), referenced by subscripts(or index) values starting at 0 for both rows and columns. a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] RAM 1 2 3 4 5 6
  • 6. 1. Problem Definition Assume we have a file “in.dat” (located in the pwd) consisting of 10 rows of 10 integers per row (100 integers total). Write a C program which reads from the file and stores the numbers in a two dimensional array named ‘mat’, computes the largest of all of these numbers and prints the largest value to the screen.   2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = from file “in.dat” Output= Largest of the 100 integers printed to screen. 3. Develop Algorithm Use Unix redirection to read file “in.dat”. Use nested for loop to go through two dimensional array to
  • 7. #include <stdio.h> void main(void) { int row, col, maximum; /* Declare a 2-D array called ‘mat’ which can hold a 10-by-10 */ /* matrix of integers */ int mat[10][10]; /* Write code here to read from the file ‘in.dat’ */ for(row=0;row<10;++row) for(col=0;col<10;++col) scanf("%i",&mat[row][col]); /* Write code to find the largest value in the array ‘mat’ */ maximum = mat[0][0]; /* why ??? */ for(row=0;row<10;++row) /* How does this work??? */ for(col=0;col<10;++col) if (mat[row][col] > maximum) maximum = mat[row][col]; /* Print the largest value to the screen */ printf(" max value in the array = %in",maximum); }
  • 8. The typedef mechanism in C allows us to define our own data types. For example, typedef double matrix[10][20]; In this example we create a new data-type named matrix. We can declare a variable of data-type matrix by matrix a; and this declaration is equivalent to double a[10][20];
  • 9. Other examples of the use of typedef are, typedef int blah; /* don’t do this !!!!! */ typedef char string[5]; In the first example we give another name or alias to the data type int . In the second example we create a new data-type named string.
  • 10. To declare a variable of data-type blah , blah x; this declaration is equivalent to int x; To declare a variable of data-type string , string var ="init"; /* declare and initialize */ scanf("%s",var); /* read in a string of chars */ strcpy(var , "next"); The declaration string morse[26]; declares morse as an array of 26 elements. Each element has data-type string.
  • 11. The declaration and initialization: string morse[26] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.. " }; declares morse as an array of 26 elements with morse[0] = ".-" morse[1] = "-..." and so on...
  • 12. Standard (ANSI) C allows up to 12 dimension. But more computation is required by the system to locate elements in multi-subscripted arrays. Therefore, in problems involving intensive numerical calculations, we should use arrays with fewer dimension. We can also use 3, 4, … dimensional arrays, e.g., threeDArray [i][j][k];