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 …

More Related Content

What's hot

C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
Shahzaib Ajmal
 
Arrays in c
Arrays in cArrays in c
Arrays in c
vampugani
 
Day 2b i/o.pptx
Day 2b   i/o.pptxDay 2b   i/o.pptx
Day 2b i/o.pptx
Adrien Melquiond
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
Smit Parikh
 
Array
ArrayArray
Array
Patel Raj
 
Scikit-learn Cheatsheet-Python
Scikit-learn Cheatsheet-PythonScikit-learn Cheatsheet-Python
Scikit-learn Cheatsheet-Python
Dr. Volkan OBAN
 
Arrays In C Language
Arrays In C LanguageArrays In C Language
Arrays In C Language
Surbhi Yadav
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
Manojkumar C
 
Arrays searching-sorting
Arrays searching-sortingArrays searching-sorting
Arrays searching-sorting
Ajharul Abedeen
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
eShikshak
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
Mazharul Islam
 
Arrays in c
Arrays in cArrays in c
Arrays in c
Jeeva Nanthini
 
Tree representation in map reduce world
Tree representation  in map reduce worldTree representation  in map reduce world
Tree representation in map reduce world
Yu Liu
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
Gagan Deep
 
Array
ArrayArray
Arrays in c
Arrays in cArrays in c
Arrays in c
CHANDAN KUMAR
 
Python_ 3 CheatSheet
Python_ 3 CheatSheetPython_ 3 CheatSheet
Python_ 3 CheatSheet
Dr. Volkan OBAN
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
ACASH1011
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
Nishant Upadhyay
 
Array in c
Array in cArray in c
Array in c
AnIsh Kumar
 

What's hot (20)

C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Day 2b i/o.pptx
Day 2b   i/o.pptxDay 2b   i/o.pptx
Day 2b i/o.pptx
 
Multidimensional array in C
Multidimensional array in CMultidimensional array in C
Multidimensional array in C
 
Array
ArrayArray
Array
 
Scikit-learn Cheatsheet-Python
Scikit-learn Cheatsheet-PythonScikit-learn Cheatsheet-Python
Scikit-learn Cheatsheet-Python
 
Arrays In C Language
Arrays In C LanguageArrays In C Language
Arrays In C Language
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Arrays searching-sorting
Arrays searching-sortingArrays searching-sorting
Arrays searching-sorting
 
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
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Tree representation in map reduce world
Tree representation  in map reduce worldTree representation  in map reduce world
Tree representation in map reduce world
 
C Programming : Arrays
C Programming : ArraysC Programming : Arrays
C Programming : Arrays
 
Array
ArrayArray
Array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Python_ 3 CheatSheet
Python_ 3 CheatSheetPython_ 3 CheatSheet
Python_ 3 CheatSheet
 
Pandas Cheat Sheet
Pandas Cheat SheetPandas Cheat Sheet
Pandas Cheat Sheet
 
Numpy python cheat_sheet
Numpy python cheat_sheetNumpy python cheat_sheet
Numpy python cheat_sheet
 
Array in c
Array in cArray in c
Array in c
 

Similar to C Language Lecture 10

Array i imp
Array  i impArray  i imp
Array i imp
Vivek Kumar
 
Arrays 06.ppt
Arrays 06.pptArrays 06.ppt
Arrays 06.ppt
ahtishamtariq511
 
arrays
arraysarrays
arrays
teach4uin
 
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
rohinitalekar1
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
Aseelhalees
 
Arrays
ArraysArrays
2D-Array
2D-Array 2D-Array
2D-Array
ALI RAZA
 
Arrays
ArraysArrays
Arrays basics
Arrays basicsArrays basics
Arrays basics
sudhirvegad
 
Abir ppt3
Abir ppt3Abir ppt3
Abir ppt3
abir96
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
Shahzaib Ajmal
 
CHAPTER-5.ppt
CHAPTER-5.pptCHAPTER-5.ppt
CHAPTER-5.ppt
Tekle12
 
Chapter 3 ds
Chapter 3 dsChapter 3 ds
Chapter 3 ds
Hanif Durad
 
Arrays
ArraysArrays
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
Appili Vamsi Krishna
 
Data structure and algorithm notes
Data structure and algorithm notesData structure and algorithm notes
Data structure and algorithm notes
suman khadka
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 
L10 array
L10 arrayL10 array
L10 array
teach4uin
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
Angshuman Saha
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
MURADSANJOUM
 

Similar to C Language Lecture 10 (20)

Array i imp
Array  i impArray  i imp
Array i imp
 
Arrays 06.ppt
Arrays 06.pptArrays 06.ppt
Arrays 06.ppt
 
arrays
arraysarrays
arrays
 
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
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Arrays
ArraysArrays
Arrays
 
2D-Array
2D-Array 2D-Array
2D-Array
 
Arrays
ArraysArrays
Arrays
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
Abir ppt3
Abir ppt3Abir ppt3
Abir ppt3
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
CHAPTER-5.ppt
CHAPTER-5.pptCHAPTER-5.ppt
CHAPTER-5.ppt
 
Chapter 3 ds
Chapter 3 dsChapter 3 ds
Chapter 3 ds
 
Arrays
ArraysArrays
Arrays
 
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
 
Data structure and algorithm notes
Data structure and algorithm notesData structure and algorithm notes
Data structure and algorithm notes
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
L10 array
L10 arrayL10 array
L10 array
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 

More from Shahzaib Ajmal

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

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

বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
NgcHiNguyn25
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 

Recently uploaded (20)

বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Life upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for studentLife upper-Intermediate B2 Workbook for student
Life upper-Intermediate B2 Workbook for student
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 

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 …