Computing Fundamentals
Dr. Muhammad Yousaf Hamza
Deputy Chief Engineer, PIEAS
Revision of Arrays
• Array
– Structures of related data items
– Group of consecutive memory locations
– Same name and type
– e.g int c[12];
• To refer to an element, specify
– Array name
– Position number
• Format:
arrayname[ position number ]
– First element at position 0
– n element array named c:
• c[ 0 ], c[ 1 ]...c[ n – 1 ]
Name of array (Note
that all elements of this
array have the same
name, c)
Position number of
the element within
array c
c[6]
c[0]
c[1]
c[2]
c[3]
c[11]
c[10]
c[9]
c[8]
c[7]
c[5]
c[4]
Dr. Yousaf, PIEAS
Revisions of Arrays
Dr. Yousaf, PIEAS
How to declare array?
typename variablename[size]
int marks[6]={36,78,29,36,7,99};
To print an array, we need loop.
for (i = 0; i <6;i++)
printf("Marks are %dn",marks[i]);
To take the elements of arrays from a user (scanf), we
need loop.
for (i = 0; i <6;i++)
{
printf(“Please enter the marks of studentsn”);
scanf(“ %d",&marks[i]);
}
Revisions of Arrays
Dr. Yousaf, PIEAS
Int a[4] = {2, 4, 3, 10};
We can use a[0]=10;
x=a[2];
a[3]=a[2]; etc.
printf( "%d", a[ 0 ] );
We can declare more than one array
in single line as:
int b[ 100 ], x[ 27 ];
If not enough initializers, rightmost elements become
0
int n[ 5 ] = { 1 } // All other elements would be 0
C arrays have no bounds checking
How to store and print a single value
Dr. Yousaf, PIEAS
#include <stdio.h>
int main( )
{
int x;
x = 5;
printf(“%d", x);
getchar();
return 0;
}
#include<stdio.h>
int main()
{
int marks[6]={36,78,29,89,7,99};
int i;
for (i = 0; i <6;i++)
printf("Marks are %dn",marks[i]);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
How to Store and Print an Array?
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
2-D 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];
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 mul[2][2];
int i, j, k, sum = 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 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
Try to write the program for the followings
Dr. Yousaf, PIEAS
(1)Accept 2x2 matrix. Determine its adjoint.
List goes on …

More Related Content

PDF
C Language Lecture 11
PPT
Chapter 9 ds
PPTX
Array,MULTI ARRAY, IN C
PPTX
Introduction to Array ppt
PDF
Day 2 repeats.pptx
PDF
11 1. multi-dimensional array eng
PPTX
Basic array in c programming
PPTX
R intro 20140716-advance
C Language Lecture 11
Chapter 9 ds
Array,MULTI ARRAY, IN C
Introduction to Array ppt
Day 2 repeats.pptx
11 1. multi-dimensional array eng
Basic array in c programming
R intro 20140716-advance

What's hot (20)

PDF
C Language Lecture 20
PPT
Arrays in c
PDF
Day 2b i/o.pptx
PPT
Multidimensional array in C
PPTX
Array
PDF
Scikit-learn Cheatsheet-Python
PPTX
Arrays In C Language
PPTX
Array in c programming
PPT
Arrays searching-sorting
PDF
Lecture17 arrays.ppt
PPTX
Array in c programming
PPTX
Arrays in c
PDF
Tree representation in map reduce world
PPSX
C Programming : Arrays
PPTX
PPTX
Arrays in c
PDF
Python_ 3 CheatSheet
PDF
Pandas Cheat Sheet
PDF
Numpy python cheat_sheet
PPTX
Array in c
C Language Lecture 20
Arrays in c
Day 2b i/o.pptx
Multidimensional array in C
Array
Scikit-learn Cheatsheet-Python
Arrays In C Language
Array in c programming
Arrays searching-sorting
Lecture17 arrays.ppt
Array in c programming
Arrays in c
Tree representation in map reduce world
C Programming : Arrays
Arrays in c
Python_ 3 CheatSheet
Pandas Cheat Sheet
Numpy python cheat_sheet
Array in c
Ad

Similar to C Language Lecture 10 (20)

PPT
Array i imp
PPT
Arrays 06.ppt
PPT
arrays
PPTX
Arrays with the help of Computer Programming
PPTX
C (PPS)Programming for problem solving.pptx
PPT
Multi dimensional arrays
PPTX
PPTX
2D-Array
PPTX
Array definition and uses in computer.pptx
PPTX
Arrays basics
PPTX
Abir ppt3
PDF
C Language Lecture 9
PPT
UNIT 1 - ARRAYS AAAAAAAAAAAAAAAAAAAAAAAAA
PPT
CHAPTER-5.ppt
PPT
Chapter 3 ds
PPT
PPTX
Arrays 1D and 2D , and multi dimensional
PDF
Data structure and algorithm notes
PPT
Basics of Data structure using C describing basics concepts
Array i imp
Arrays 06.ppt
arrays
Arrays with the help of Computer Programming
C (PPS)Programming for problem solving.pptx
Multi dimensional arrays
2D-Array
Array definition and uses in computer.pptx
Arrays basics
Abir ppt3
C Language Lecture 9
UNIT 1 - ARRAYS AAAAAAAAAAAAAAAAAAAAAAAAA
CHAPTER-5.ppt
Chapter 3 ds
Arrays 1D and 2D , and multi dimensional
Data structure and algorithm notes
Basics of Data structure using C describing basics concepts
Ad

More from Shahzaib Ajmal (19)

PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PDF
C Language Lecture 22
PDF
C Language Lecture 21
PDF
C Language Lecture 19
PDF
C Language Lecture 18
PDF
C Language Lecture 17
PDF
C Language Lecture 16
PDF
C Language Lecture 15
PDF
C Language Lecture 14
PDF
C Language Lecture 13
PDF
C Language Lecture 12
PDF
C Language Lecture 8
PDF
C Language Lecture 7
PDF
C Language Lecture 6
PDF
C Language Lecture 5
PDF
C Language Lecture 4
PDF
C Language Lecture 3
PDF
C Language Lecture 2
PDF
C Language Lecture 1
Galatica Smart Energy Infrastructure Startup Pitch Deck
C Language Lecture 22
C Language Lecture 21
C Language Lecture 19
C Language Lecture 18
C Language Lecture 17
C Language Lecture 16
C Language Lecture 15
C Language Lecture 14
C Language Lecture 13
C Language Lecture 12
C Language Lecture 8
C Language Lecture 7
C Language Lecture 6
C Language Lecture 5
C Language Lecture 4
C Language Lecture 3
C Language Lecture 2
C Language Lecture 1

Recently uploaded (20)

DOCX
HELMET DETECTION AND BIOMETRIC BASED VEHICLESECURITY USING MACHINE LEARNING.docx
PDF
Jana Ojana 2025 Prelims - School Quiz by Pragya - UEMK Quiz Club
PDF
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
PDF
IDA Textbook Grade 10 .pdf download link if 1st link isn't working so hard to...
PDF
Global strategy and action plan on oral health 2023 - 2030.pdf
PPTX
Entrepreneurship Management and Finance - Module 1 - PPT
PDF
3-Elementary-Education-Prototype-Syllabi-Compendium.pdf
PDF
BA-1ST(Education)-Education and Society.pdf
PPTX
INTRODUCTION TO PHILOSOPHY FULL SEM - COMPLETE.pptxINTRODUCTION TO PHILOSOPHY...
PDF
New_Round_Up_6_SB.pdf download for free, easy to learn
PPTX
CHF refers to the condition wherein heart unable to pump a sufficient amount ...
PDF
IS1343_2012...........................pdf
PPTX
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
PDF
English 2nd semesteNotesh biology biopsy results from the other day and I jus...
PPTX
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
PPTX
macro complete discussion with given activities
PDF
Bacterial Diversity and Evolution Bacterial Taxonomy Lecture (4)_.pdf
PDF
17649-Learning By Doing_text-tailieu.pdf
PDF
BP303T PHARMACEUTICALMICROBIOLOGY UNIT 1
PDF
Physical pharmaceutics two in b pharmacy
HELMET DETECTION AND BIOMETRIC BASED VEHICLESECURITY USING MACHINE LEARNING.docx
Jana Ojana 2025 Prelims - School Quiz by Pragya - UEMK Quiz Club
GIÁO ÁN TIẾNG ANH 7 GLOBAL SUCCESS (CẢ NĂM) THEO CÔNG VĂN 5512 (2 CỘT) NĂM HỌ...
IDA Textbook Grade 10 .pdf download link if 1st link isn't working so hard to...
Global strategy and action plan on oral health 2023 - 2030.pdf
Entrepreneurship Management and Finance - Module 1 - PPT
3-Elementary-Education-Prototype-Syllabi-Compendium.pdf
BA-1ST(Education)-Education and Society.pdf
INTRODUCTION TO PHILOSOPHY FULL SEM - COMPLETE.pptxINTRODUCTION TO PHILOSOPHY...
New_Round_Up_6_SB.pdf download for free, easy to learn
CHF refers to the condition wherein heart unable to pump a sufficient amount ...
IS1343_2012...........................pdf
ENGlishGrade8_Quarter2_WEEK1_LESSON1.pptx
English 2nd semesteNotesh biology biopsy results from the other day and I jus...
Unit1_Kumod_deeplearning.pptx DEEP LEARNING
macro complete discussion with given activities
Bacterial Diversity and Evolution Bacterial Taxonomy Lecture (4)_.pdf
17649-Learning By Doing_text-tailieu.pdf
BP303T PHARMACEUTICALMICROBIOLOGY UNIT 1
Physical pharmaceutics two in b pharmacy

C Language Lecture 10

  • 1. Computing Fundamentals Dr. Muhammad Yousaf Hamza Deputy Chief Engineer, PIEAS
  • 2. Revision of Arrays • Array – Structures of related data items – Group of consecutive memory locations – Same name and type – e.g int c[12]; • To refer to an element, specify – Array name – Position number • Format: arrayname[ position number ] – First element at position 0 – n element array named c: • c[ 0 ], c[ 1 ]...c[ n – 1 ] Name of array (Note that all elements of this array have the same name, c) Position number of the element within array c c[6] c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Dr. Yousaf, PIEAS
  • 3. Revisions of Arrays Dr. Yousaf, PIEAS How to declare array? typename variablename[size] int marks[6]={36,78,29,36,7,99}; To print an array, we need loop. for (i = 0; i <6;i++) printf("Marks are %dn",marks[i]); To take the elements of arrays from a user (scanf), we need loop. for (i = 0; i <6;i++) { printf(“Please enter the marks of studentsn”); scanf(“ %d",&marks[i]); }
  • 4. Revisions of Arrays Dr. Yousaf, PIEAS Int a[4] = {2, 4, 3, 10}; We can use a[0]=10; x=a[2]; a[3]=a[2]; etc. printf( "%d", a[ 0 ] ); We can declare more than one array in single line as: int b[ 100 ], x[ 27 ]; If not enough initializers, rightmost elements become 0 int n[ 5 ] = { 1 } // All other elements would be 0 C arrays have no bounds checking
  • 5. How to store and print a single value Dr. Yousaf, PIEAS #include <stdio.h> int main( ) { int x; x = 5; printf(“%d", x); getchar(); return 0; }
  • 6. #include<stdio.h> int main() { int marks[6]={36,78,29,89,7,99}; int i; for (i = 0; i <6;i++) printf("Marks are %dn",marks[i]); getchar(); return 0; } Dr. Yousaf, PIEAS How to Store and Print an Array?
  • 7. 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
  • 8. 2-D Arrays Nesting in Loops Dr. Yousaf, PIEAS
  • 9. 2-D 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
  • 10. 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?
  • 11. #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?
  • 12. 123456789101112 We want output in Matrix Form Dr. Yousaf, PIEAS Output of the Program
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. #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?
  • 17. 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
  • 18. 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
  • 19. 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
  • 20. 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
  • 22. 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]; Dr. Yousaf, PIEAS
  • 23. 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
  • 24. 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 mul[2][2]; int i, j, k, sum = 0; Dr. Yousaf, PIEAS
  • 25. 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 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
  • 26. Try to write the program for the followings Dr. Yousaf, PIEAS (1)Accept 2x2 matrix. Determine its adjoint. List goes on …