SlideShare a Scribd company logo
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 …
Ad

Recommended

PDF
C Language Lecture 11
Shahzaib Ajmal
 
PPT
Chapter 9 ds
Hanif Durad
 
PPTX
Array,MULTI ARRAY, IN C
naveed jamali
 
PPTX
Introduction to Array ppt
sandhya yadav
 
PDF
Day 2 repeats.pptx
Adrien Melquiond
 
PDF
11 1. multi-dimensional array eng
웅식 전
 
PPTX
Basic array in c programming
Sajid Hasan
 
PPTX
R intro 20140716-advance
Kevin Chun-Hsien Hsu
 
PDF
C Language Lecture 20
Shahzaib Ajmal
 
PPT
Arrays in c
vampugani
 
PDF
Day 2b i/o.pptx
Adrien Melquiond
 
PPT
Multidimensional array in C
Smit Parikh
 
PPTX
Array
Patel Raj
 
PDF
Scikit-learn Cheatsheet-Python
Dr. Volkan OBAN
 
PPTX
Arrays In C Language
Surbhi Yadav
 
PPTX
Array in c programming
Manojkumar C
 
PPT
Arrays searching-sorting
Ajharul Abedeen
 
PDF
Lecture17 arrays.ppt
eShikshak
 
PPTX
Array in c programming
Mazharul Islam
 
PPTX
Arrays in c
Jeeva Nanthini
 
PDF
Tree representation in map reduce world
Yu Liu
 
PPSX
C Programming : Arrays
Gagan Deep
 
PPTX
Array
Allah Ditta
 
PPTX
Arrays in c
CHANDAN KUMAR
 
PDF
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
PDF
Pandas Cheat Sheet
ACASH1011
 
PDF
Numpy python cheat_sheet
Nishant Upadhyay
 
PPTX
Array in c
AnIsh Kumar
 
PPT
Array i imp
Vivek Kumar
 
PPT
Arrays 06.ppt
ahtishamtariq511
 

More Related Content

What's hot (20)

PDF
C Language Lecture 20
Shahzaib Ajmal
 
PPT
Arrays in c
vampugani
 
PDF
Day 2b i/o.pptx
Adrien Melquiond
 
PPT
Multidimensional array in C
Smit Parikh
 
PPTX
Array
Patel Raj
 
PDF
Scikit-learn Cheatsheet-Python
Dr. Volkan OBAN
 
PPTX
Arrays In C Language
Surbhi Yadav
 
PPTX
Array in c programming
Manojkumar C
 
PPT
Arrays searching-sorting
Ajharul Abedeen
 
PDF
Lecture17 arrays.ppt
eShikshak
 
PPTX
Array in c programming
Mazharul Islam
 
PPTX
Arrays in c
Jeeva Nanthini
 
PDF
Tree representation in map reduce world
Yu Liu
 
PPSX
C Programming : Arrays
Gagan Deep
 
PPTX
Array
Allah Ditta
 
PPTX
Arrays in c
CHANDAN KUMAR
 
PDF
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
PDF
Pandas Cheat Sheet
ACASH1011
 
PDF
Numpy python cheat_sheet
Nishant Upadhyay
 
PPTX
Array in c
AnIsh Kumar
 
C Language Lecture 20
Shahzaib Ajmal
 
Arrays in c
vampugani
 
Day 2b i/o.pptx
Adrien Melquiond
 
Multidimensional array in C
Smit Parikh
 
Array
Patel Raj
 
Scikit-learn Cheatsheet-Python
Dr. Volkan OBAN
 
Arrays In C Language
Surbhi Yadav
 
Array in c programming
Manojkumar C
 
Arrays searching-sorting
Ajharul Abedeen
 
Lecture17 arrays.ppt
eShikshak
 
Array in c programming
Mazharul Islam
 
Arrays in c
Jeeva Nanthini
 
Tree representation in map reduce world
Yu Liu
 
C Programming : Arrays
Gagan Deep
 
Array
Allah Ditta
 
Arrays in c
CHANDAN KUMAR
 
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
Pandas Cheat Sheet
ACASH1011
 
Numpy python cheat_sheet
Nishant Upadhyay
 
Array in c
AnIsh Kumar
 

Similar to C Language Lecture 10 (20)

PPT
Array i imp
Vivek Kumar
 
PPT
Arrays 06.ppt
ahtishamtariq511
 
PPT
arrays
teach4uin
 
PPTX
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
PPT
Multi dimensional arrays
Aseelhalees
 
PPTX
Arrays
Chirag vasava
 
PPTX
2D-Array
ALI RAZA
 
PPTX
Array definition and uses in computer.pptx
naushigrdcs
 
PPTX
Arrays basics
sudhirvegad
 
PPTX
Abir ppt3
abir96
 
PDF
C Language Lecture 9
Shahzaib Ajmal
 
PPT
CHAPTER-5.ppt
Tekle12
 
PPT
Chapter 3 ds
Hanif Durad
 
PPT
Arrays
Saranya saran
 
PPTX
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
PDF
Data structure and algorithm notes
suman khadka
 
PPT
Basics of Data structure using C describing basics concepts
shanthidl1
 
PPT
Unit4 Slides
Anurag University Hyderabad
 
PDF
Data Structure Lecture Array and Recursion.pdf
okokji4201
 
Array i imp
Vivek Kumar
 
Arrays 06.ppt
ahtishamtariq511
 
arrays
teach4uin
 
C (PPS)Programming for problem solving.pptx
rohinitalekar1
 
Multi dimensional arrays
Aseelhalees
 
Arrays
Chirag vasava
 
2D-Array
ALI RAZA
 
Array definition and uses in computer.pptx
naushigrdcs
 
Arrays basics
sudhirvegad
 
Abir ppt3
abir96
 
C Language Lecture 9
Shahzaib Ajmal
 
CHAPTER-5.ppt
Tekle12
 
Chapter 3 ds
Hanif Durad
 
Arrays
Saranya saran
 
Arrays 1D and 2D , and multi dimensional
Appili Vamsi Krishna
 
Data structure and algorithm notes
suman khadka
 
Basics of Data structure using C describing basics concepts
shanthidl1
 
Data Structure Lecture Array and Recursion.pdf
okokji4201
 
Ad

More from Shahzaib Ajmal (18)

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

Recently uploaded (20)

PPTX
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
PPTX
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
PPT
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
PPTX
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
PPTX
Photo chemistry Power Point Presentation
mprpgcwa2024
 
PPTX
How payment terms are configured in Odoo 18
Celine George
 
PPTX
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
PPTX
Peer Teaching Observations During School Internship
AjayaMohanty7
 
PPTX
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
PPTX
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
PPTX
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
PPTX
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
PPTX
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
PPTX
List View Components in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Customize Quotation Layouts in Odoo 18
Celine George
 
PPTX
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
PDF
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
PPTX
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
PPTX
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
PDF
VCE Literature Section A Exam Response Guide
jpinnuck
 
ENGLISH-5 Q1 Lesson 1.pptx - Story Elements
Mayvel Nadal
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
M&A5 Q1 1 differentiate evolving early Philippine conventional and contempora...
ErlizaRosete
 
IIT KGP Quiz Week 2024 Sports Quiz (Prelims + Finals)
IIT Kharagpur Quiz Club
 
Photo chemistry Power Point Presentation
mprpgcwa2024
 
How payment terms are configured in Odoo 18
Celine George
 
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
Peer Teaching Observations During School Internship
AjayaMohanty7
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
June 2025 Progress Update With Board Call_In process.pptx
International Society of Service Innovation Professionals
 
Values Education 10 Quarter 1 Module .pptx
JBPafin
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
Tanja Vujicic - PISA for Schools contact Info
EduSkills OECD
 
List View Components in Odoo 18 - Odoo Slides
Celine George
 
How to Customize Quotation Layouts in Odoo 18
Celine George
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
VCE Literature Section A Exam Response Guide
jpinnuck
 

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 …