SlideShare a Scribd company logo
1 of 18
Download to read offline
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
Deputy Chief Engineer, PIEAS
Dr. Yousaf, PIEAS
How to store these values?
1 2 3
4 5 6
7 8 9
10 11 12
How to print these values?
How to Store and Print a matrix?
2-D Arrays
2-D Arrays
Nesting in Loops
Dr. Yousaf, PIEAS
Multidimensional Arrays
• Arrays in C can have virtually as many dimensions as
you want.
• Definition is accomplished by adding additional
subscripts when it is defined.
• For example:
– int a [4] [3] ; // 4 Rows, 3 Columns
– defines a two dimensional array
a[0][0] a[0][1] a[0][2]
a[1][0] a[1][1] a[1][2]
a[2][0] a[2][1] a[2][2]
a[3][0] a[3][1] a[3][2]
Dr. Yousaf, PIEAS
2-D Arrays
Dr. Yousaf, PIEAS
How to store these values?
1 2 3
4 5 6
7 8 9
10 11 12
How to print these values?
#include<stdio.h>
int main()
{
int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} };
int row, col;
for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(“%d", a[row][col]);
}
}
getchar(); return 0; }
Dr. Yousaf, PIEAS
How to Print 2-D Arrays?
123456789101112
We want output in Matrix Form
Dr. Yousaf, PIEAS
Output of the Program
for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(“%dt",a[row][col]);
}
}
Output:
1 2 3 4 5 6 7 8 9
10 11 12
Dr. Yousaf, PIEAS
Output with Tabs
for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(“%dt", a[row][col]);
}
printf(“n”);
}
Dr. Yousaf, PIEAS
Output in Matrix Form
for (row = 0; row <=3; row++)
{ for (col = 0; col <=2; col++)
{
printf(“%dt", a[row][col]);
}
printf(“n”);
}
Dr. Yousaf, PIEAS
Output in Matrix Form
Output:
1 2 3
4 5 6
7 8 9
10 11 12
#include<stdio.h>
int main()
{
int a[4] [3];
int row, col;
for (row = 0; row <=3; row++)
{
printf("Enter 3 elements of row %dn", row + 1);
for (col = 0; col <=2; col++)
{
scanf("%d",&a[row][col]);
}
}
//Rest of the code goes here
Dr. Yousaf, PIEAS
How to scan 2-D Arrays?
Initializing Multidimensional Arrays
• The following initializes a[4][3]:
int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} };
• Also can be done by:
int a[4] [3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
– is equivalent to
a[0][0] = 1;
a[0][1] = 2;
a[0][2] = 3;
a[1][0] = 4;
...
a[3][2] = 12;
Dr. Yousaf, PIEAS
Multiple-Subscripted Arrays
• Multiple subscripted arrays
– Tables with rows and columns (m by n array)
– Like matrices: specify row, then column
Row 0
Row 1
Row 2
Column 0 Column 1 Column 2 Column 3
a[ 0 ][ 0 ]
a[ 1 ][ 0 ]
a[ 2 ][ 0 ]
a[ 0 ][ 1 ]
a[ 1 ][ 1 ]
a[ 2 ][ 1 ]
a[ 0 ][ 2 ]
a[ 1 ][ 2 ]
a[ 2 ][ 2 ]
a[ 0 ][ 3 ]
a[ 1 ][ 3 ]
a[ 2 ][ 3 ]
Row subscript
Array name
Column subscript
Dr. Yousaf, PIEAS
Multiple-Subscripted Arrays
• Initialization
– int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } };
– Initializers grouped by row in braces
– If not enough, unspecified elements set to zero
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
• Referencing elements
– Specify row, then column
printf( "%d", b[ 0 ][ 1 ] );
1 2
3 4
1 0
3 4
Dr. Yousaf, PIEAS
Multidimensional Arrays
• Array declarations read right-to-left
• int a[10][3][2];
• “a is array of ten arrays of three arrays of two (type
ints)”. In memory
2 2 2
3
2 2 2
3
2 2 2
3
...
10
Dr. Yousaf, PIEAS
Some Examples
Dr. Yousaf, PIEAS
Addition of Two Matrices
#include <stdio.h>
int main()
{
int X[2][2] = { {1,2},{3,4} }, Y[2][2] =
{ {5,6},{7,8} };
int add[2][2];
int i, j;
printf("ntAddition of two matrices
is");
for (i = 0; i<2; i++)
{
for (j = 0; j<2; j++)
{
add[i][j] = X[i][j] + Y[i][j];
printf("%dt", add[i][j]);
}
printf("n");
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Multiplication of Two Matrices
#include <stdio.h>
int main()
{
int X[2][2] = { {1,2},{3,4} },
Y[2][2] = { {5,6},{7,8} };
int add[2][2], mul[2][2];
int i, j, k, sum = 0;
printf("nntMultiplications
of two matrices is");
for (i = 0; i<2; i++)
{
for (j = 0; j<2; j++)
{
for (k=0; k<2; k++)
{
sum = sum + (X[i][k]*Y[k][j]);
}
mul[i][j] = sum;
printf("%dt", mul[i][j]);
sum = 0;
}
printf("n");
} getchar(); return 0; }
Dr. Yousaf, PIEAS

More Related Content

What's hot

What's hot (20)

Day 1c access, select ordering copy.pptx
Day 1c   access, select   ordering copy.pptxDay 1c   access, select   ordering copy.pptx
Day 1c access, select ordering copy.pptx
 
7.basic array
7.basic array7.basic array
7.basic array
 
Day 2b i/o.pptx
Day 2b   i/o.pptxDay 2b   i/o.pptx
Day 2b i/o.pptx
 
Searching in Arrays
Searching in ArraysSearching in Arrays
Searching in Arrays
 
Array
ArrayArray
Array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Day 1d R structures & objects: matrices and data frames.pptx
Day 1d   R structures & objects: matrices and data frames.pptxDay 1d   R structures & objects: matrices and data frames.pptx
Day 1d R structures & objects: matrices and data frames.pptx
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Array in c
Array in cArray in c
Array in c
 
Day 1b R structures objects.pptx
Day 1b   R structures   objects.pptxDay 1b   R structures   objects.pptx
Day 1b R structures objects.pptx
 
Array in c
Array in cArray in c
Array in c
 
Array in c
Array in cArray in c
Array in c
 
Array in C
Array in CArray in C
Array in C
 
Chapter 9 ds
Chapter 9 dsChapter 9 ds
Chapter 9 ds
 
Arrays
ArraysArrays
Arrays
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 

Similar to C Language Lecture 11

Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 

Similar to C Language Lecture 11 (20)

Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
2D-Array
2D-Array 2D-Array
2D-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
 
Arrays
ArraysArrays
Arrays
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
arrays
arraysarrays
arrays
 
Array i imp
Array  i impArray  i imp
Array i imp
 
Arrays 06.ppt
Arrays 06.pptArrays 06.ppt
Arrays 06.ppt
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
Abir ppt3
Abir ppt3Abir ppt3
Abir ppt3
 
Array
ArrayArray
Array
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Unit 3
Unit 3 Unit 3
Unit 3
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
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
 
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
 
Arrays
ArraysArrays
Arrays
 

More from Shahzaib Ajmal (18)

C Language Lecture 22
C Language Lecture 22C Language Lecture 22
C Language Lecture 22
 
C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
 
C Language Lecture 19
C Language Lecture 19C Language Lecture 19
C Language Lecture 19
 
C Language Lecture 18
C Language Lecture 18C Language Lecture 18
C Language Lecture 18
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
 
C Language Lecture 13
C Language Lecture 13C Language Lecture 13
C Language Lecture 13
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
C Language Lecture 8
C Language Lecture 8C Language Lecture 8
C Language Lecture 8
 
C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
 
C Language Lecture 5
C Language Lecture  5C Language Lecture  5
C Language Lecture 5
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
 

Recently uploaded

21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
rahulmanepalli02
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
MaherOthman7
 
Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
BalamuruganV28
 

Recently uploaded (20)

SLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptxSLIDESHARE PPT-DECISION MAKING METHODS.pptx
SLIDESHARE PPT-DECISION MAKING METHODS.pptx
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and ToolsMaximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
Maximizing Incident Investigation Efficacy in Oil & Gas: Techniques and Tools
 
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas SachpazisSeismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
Seismic Hazard Assessment Software in Python by Prof. Dr. Costas Sachpazis
 
Software Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdfSoftware Engineering Practical File Front Pages.pdf
Software Engineering Practical File Front Pages.pdf
 
Adsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) pptAdsorption (mass transfer operations 2) ppt
Adsorption (mass transfer operations 2) ppt
 
Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...Fuzzy logic method-based stress detector with blood pressure and body tempera...
Fuzzy logic method-based stress detector with blood pressure and body tempera...
 
21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx21P35A0312 Internship eccccccReport.docx
21P35A0312 Internship eccccccReport.docx
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
Diploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdfDiploma Engineering Drawing Qp-2024 Ece .pdf
Diploma Engineering Drawing Qp-2024 Ece .pdf
 
Circuit Breakers for Engineering Students
Circuit Breakers for Engineering StudentsCircuit Breakers for Engineering Students
Circuit Breakers for Engineering Students
 
Interfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdfInterfacing Analog to Digital Data Converters ee3404.pdf
Interfacing Analog to Digital Data Converters ee3404.pdf
 
Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1Research Methodolgy & Intellectual Property Rights Series 1
Research Methodolgy & Intellectual Property Rights Series 1
 
21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university21scheme vtu syllabus of visveraya technological university
21scheme vtu syllabus of visveraya technological university
 
Independent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging StationIndependent Solar-Powered Electric Vehicle Charging Station
Independent Solar-Powered Electric Vehicle Charging Station
 
Artificial Intelligence in due diligence
Artificial Intelligence in due diligenceArtificial Intelligence in due diligence
Artificial Intelligence in due diligence
 
Autodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptxAutodesk Construction Cloud (Autodesk Build).pptx
Autodesk Construction Cloud (Autodesk Build).pptx
 
Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..Maher Othman Interior Design Portfolio..
Maher Othman Interior Design Portfolio..
 
Final DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manualFinal DBMS Manual (2).pdf final lab manual
Final DBMS Manual (2).pdf final lab manual
 
Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)Theory of Time 2024 (Universal Theory for Everything)
Theory of Time 2024 (Universal Theory for Everything)
 

C Language Lecture 11

  • 1. Computing Fundamentals Dr. Muhammad Yousaf Hamza Deputy Chief Engineer, PIEAS
  • 2. Dr. Yousaf, PIEAS How to store these values? 1 2 3 4 5 6 7 8 9 10 11 12 How to print these values? How to Store and Print a matrix? 2-D Arrays
  • 3. 2-D Arrays Nesting in Loops Dr. Yousaf, PIEAS
  • 4. Multidimensional Arrays • Arrays in C can have virtually as many dimensions as you want. • Definition is accomplished by adding additional subscripts when it is defined. • For example: – int a [4] [3] ; // 4 Rows, 3 Columns – defines a two dimensional array a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] a[2][0] a[2][1] a[2][2] a[3][0] a[3][1] a[3][2] Dr. Yousaf, PIEAS
  • 5. 2-D Arrays Dr. Yousaf, PIEAS How to store these values? 1 2 3 4 5 6 7 8 9 10 11 12 How to print these values?
  • 6. #include<stdio.h> int main() { int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} }; int row, col; for (row = 0; row <=3; row++) { for (col = 0; col <=2; col++) { printf(“%d", a[row][col]); } } getchar(); return 0; } Dr. Yousaf, PIEAS How to Print 2-D Arrays?
  • 7. 123456789101112 We want output in Matrix Form Dr. Yousaf, PIEAS Output of the Program
  • 8. for (row = 0; row <=3; row++) { for (col = 0; col <=2; col++) { printf(“%dt",a[row][col]); } } Output: 1 2 3 4 5 6 7 8 9 10 11 12 Dr. Yousaf, PIEAS Output with Tabs
  • 9. for (row = 0; row <=3; row++) { for (col = 0; col <=2; col++) { printf(“%dt", a[row][col]); } printf(“n”); } Dr. Yousaf, PIEAS Output in Matrix Form
  • 10. for (row = 0; row <=3; row++) { for (col = 0; col <=2; col++) { printf(“%dt", a[row][col]); } printf(“n”); } Dr. Yousaf, PIEAS Output in Matrix Form Output: 1 2 3 4 5 6 7 8 9 10 11 12
  • 11. #include<stdio.h> int main() { int a[4] [3]; int row, col; for (row = 0; row <=3; row++) { printf("Enter 3 elements of row %dn", row + 1); for (col = 0; col <=2; col++) { scanf("%d",&a[row][col]); } } //Rest of the code goes here Dr. Yousaf, PIEAS How to scan 2-D Arrays?
  • 12. Initializing Multidimensional Arrays • The following initializes a[4][3]: int a[4] [3] = { {1, 2, 3} , { 4, 5, 6} , {7, 8, 9} , {10, 11, 12} }; • Also can be done by: int a[4] [3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; – is equivalent to a[0][0] = 1; a[0][1] = 2; a[0][2] = 3; a[1][0] = 4; ... a[3][2] = 12; Dr. Yousaf, PIEAS
  • 13. Multiple-Subscripted Arrays • Multiple subscripted arrays – Tables with rows and columns (m by n array) – Like matrices: specify row, then column Row 0 Row 1 Row 2 Column 0 Column 1 Column 2 Column 3 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Row subscript Array name Column subscript Dr. Yousaf, PIEAS
  • 14. Multiple-Subscripted Arrays • Initialization – int b[ 2 ][ 2 ] = { { 1, 2 }, { 3, 4 } }; – Initializers grouped by row in braces – If not enough, unspecified elements set to zero int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; • Referencing elements – Specify row, then column printf( "%d", b[ 0 ][ 1 ] ); 1 2 3 4 1 0 3 4 Dr. Yousaf, PIEAS
  • 15. Multidimensional Arrays • Array declarations read right-to-left • int a[10][3][2]; • “a is array of ten arrays of three arrays of two (type ints)”. In memory 2 2 2 3 2 2 2 3 2 2 2 3 ... 10 Dr. Yousaf, PIEAS
  • 17. Addition of Two Matrices #include <stdio.h> int main() { int X[2][2] = { {1,2},{3,4} }, Y[2][2] = { {5,6},{7,8} }; int add[2][2]; int i, j; printf("ntAddition of two matrices is"); for (i = 0; i<2; i++) { for (j = 0; j<2; j++) { add[i][j] = X[i][j] + Y[i][j]; printf("%dt", add[i][j]); } printf("n"); } getchar(); return 0; } Dr. Yousaf, PIEAS
  • 18. Multiplication of Two Matrices #include <stdio.h> int main() { int X[2][2] = { {1,2},{3,4} }, Y[2][2] = { {5,6},{7,8} }; int add[2][2], mul[2][2]; int i, j, k, sum = 0; printf("nntMultiplications of two matrices is"); for (i = 0; i<2; i++) { for (j = 0; j<2; j++) { for (k=0; k<2; k++) { sum = sum + (X[i][k]*Y[k][j]); } mul[i][j] = sum; printf("%dt", mul[i][j]); sum = 0; } printf("n"); } getchar(); return 0; } Dr. Yousaf, PIEAS