SlideShare a Scribd company logo
1 of 21
Array
02/14/19PPSTheory
Er. Anupam Sharma
Assistant Professor
anupamcgctc@gmail.com
Contents
02/14/19PPSTheory
1 Array
2 Declaration and Initialization
3 Array Index
4 Accessing Array Elements and
Printing
5 Insertion
6 Insertion and Printing
7 Deleting / Removing of an element
8 Multi-dimensional Array
9 Printing 2D Array
10 Printing 3D Array
Textbook and Reference
Books
02/14/19PPSTheory
Textbook:
•Let Us C, by Yashavant Kanetkar, 13th Edition, Published by BPB Publications.
Reference books:
•Teach Yourself C, by Herbert Schildt, 4th Edition, Published by Osborne.
•Sams Teach Yourself C, by Bradley L. Jones & Peter Aitken, 6th Edition.
•The C Programming Language, by Brian W. Kernighan & Dennis M. Ritchie, 2nd
Edition, Published by Prentice Hall.
Array
02/14/19PPSTheory
An array is a collection of data storage locations, each storing the same
type of data and having the same name.
intnum ber[5]; floatmarks[10]; doublesalary[10]; charalphabet[5];
–Each storage location in an array is called an array
element.
Array (Cont’d)
02/14/19PPSTheory
–An array is a collection of variables of same data types.
–All elements of array are stored in the contiguous memory locations.
–The size of array must be a constant integral value.
–Individual elements in an array can be accessed by the name of the array and
an integer enclosed in square bracket called subscript/index variable like
number [10].
–The first element in an array is at index 0, whereas the last element is at index
[ArraySize - 1].
Array Declaration and Initialization
02/14/19PPSTheory
Array Declaration:
intnum ber[5]; floatmarks[5]; doublesalary[5];
Array Initialization:
marks[0] = 5;
marks[1] = 2;
marks[2] = 9;
marks[3] = 1;
marks[4] = 1;
Array Declaration and Initialization:
intnum ber[5] = {1 ,2 ,3 ,4 ,500};
floatscore[10] = {10 ,8 ,7 ,9 ,4.5 ,6 ,7 ,9 ,8.5 ,10};
doublesalary[5] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 3 0 0 0 0 .0 0 };
doublesalary[] = {10000.00 , 15000.00 , 20500.500 , 5050.50 , 3 0 0 0 0 .0 0 };
charalphabet[5] = { ’A ’ , ’B ’, ’C ’, ’D ’ , ’E ’};
Array Index
02/14/19PPSTheory
-Array index is the location of an element in an array
-You can access elements of an array by indices
-For example, Let’s get the following salary array elements using index
numbers -
1 doublesalary[5] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 3 0 0 0 0 .0 0 };
2 doublesalary1=salary[0];
3 doublesalary2=salary[1];
4 doublesalary3=salary[2];
5 doublesalary4=salary[3];
6 doublesalary5=salary[4];
Accessing elements using index and printing with loops
02/14/19PPSTheory
1 #include< stdio .h >
2 voidmain() {
3 charalphabet[5]={ ’A ’, ’B ’, ’C ’,’D ’,’E ’};
4
5 /* Let ’s p rin t all a lp h a b ets one by one */
6 inti;
7 for(i=0;i<5;i++) {
8 printf( " c " ,alph abet[i]) ;
9 }
10 }
1 #include< stdio .h >
2 voidmain() {
3 doublesalary[] = {10000.00 , 15000.00 , 20500.50 ,
5050.50 , 3 0 0 0 0 .0 0 };
4
5 /* Let ’s p rin t all sa la ry one by one */
6 inti;
7 for(i=0;i<5;i++) {
8 printf( " S alary [ d ]: .2 lf  n " ,i,salary[i]) ;
9 }
10 }
Accessing elements using index and printing with loops
02/14/19PPSTheory
1 #include< stdio .h >
2 voidmain() {
3 charalphabet[5]={ ’A ’, ’B ’, ’C ’,’D ’,’E ’};
4
5 /* Let ’s p rin t all a lp h a b ets one by one */
6 inti;
7 for(i=0;i<5;i++) {
8 printf( " c " ,alph abet[i]) ;
9 }
10 }
1 #include< stdio .h >
2 voidmain() {
3 doublesalary[] = {10000.00 , 15000.00 , 20500.50 ,
5050.50 , 3 0 0 0 0 .0 0 };
4
5 /* Let ’s p rin t all sa la ry one by one */
6 inti;
7 for(i=0;i<5;i++) {
8 printf( " S alary [ d ]: .2 lf  n " ,i,salary[i]) ;
9 }
10 }
Program Output:
A B C D E
Program Output:
S alary [0]: 10 00 0.0 0
S alary [1]: 15 00 0.0 0
S alary [2]: 20 50 0.5 0
S alary [3]: 5050.50
S alary [4]: 30 00 0.0 0
Insertion: Insert elements in an array
02/14/19PPSTheory
1 #include< stdio .h >
2 voidmain() {
3 intsize= 5;
4 floatmarks[size];
5
6 /* In sertin g m arks into m a rks a rra y */
7 inti;
8 for(i= 0;i<=size;i++) {
9 printf( " Enter a mark : ");
10 scanf(" f" , &marks[i]) ;
11 }
12
13}
Insertion: Insert elements in an array
02/14/19PPSTheory
1 #include< stdio .h >
2 voidmain() {
3 intsize= 5;
4 floatmarks[size];
5
6 /* In sertin g m arks into m a rks a rra y */
7 inti;
8 for(i= 0;i<=size;i++) {
9 printf( " Enter a mark : ");
10 scanf(" f" , &marks[i]) ;
11 }
12
13 }
Program Output:
Enter a mark : 50.5 Enter a mark :
40 Enter a mark : 30.5 Enter a
mark : 75.5 Enter a mark : 80 Enter
a mark : 90
Insertion and Printing
02/14/19PPSTheory
1 #include< stdio .h >
2 Void m ain () {
3 intsize= 5;
4 floatmarks[size];
5
6 /* In sertin g m arks into m a rks a rra y */
7 inti;
8 for(i= 0;i<=size;i++) {
9 printf( " Enter a marks : ");
10 scanf(" f" , &marks[i]) ;
11 }
12
13 /* Let ’s p rin t all m arks in the m a rks arra y */
14
15 printf( " n P rin tin g all marks : ");
16
17 intj;
18 for(j= 0;j<=size;j++) {
19 printf( " n . f " ,marks[j]) ;
20 }
21
22 }
Insertion and Printing
02/14/19PPSTheory
1 #include< stdio .h >
2 Void main() {
3 intsize= 5;
4 floatmarks[size];
5
6 /* In sertin g m arks into m a rks a rra y */
7 inti;
8 for(i= 0;i<=size;i++) {
9 printf( " Enter a marks : ");
10 scanf(" f" , &marks[i]) ;
11 }
12
13 /* Let ’s p rin t all m arks in the m a rks arra y */
14
15 printf( " n P rin tin g all marks : ");
16
17 intj;
18 for(j= 0;j<=size;j++) {
19 printf( " n .1 f " ,marks[j]) ;
20 }
21
22 }
Program Output:
Enter a marks : 80 Enter a
marks : 85.5 Enter a marks :
75 Enter a marks : 63.5
Enter a marks : 90 Enter a
marks : 11.0
P rin tin g all marks :
80.0
85.5
75.0
63.5
90.0
Deleting / Removing an element
02/14/19PPSTheory
1 #include< stdio .h >
2 voidmain() {
3 int n_ array[5] = {11 , 12 , 13 , 14 , 15};
4
5 // let ’s p rin t all elem en ts in the n _ a rra y
6 printf( " Array before d eletion :  n ");
7 inti;
8 for(i= 0;i< 5;i++) {
9 printf( " [ d ] d  n " ,i, n_ array[i]) ;
10 }
11
12 printf( " let ’s delete elem ent at index 3  n ");
13 intindex;
14 for(index= 3 ;index< 5 - 1 ;index++) {
15 n _ array[index] = n _ array[index
+1];
16 }
17
18 printf( " Array after d eletion :  n ");
19 for(i= 0;i< 5 - 1;i++) {
20 printf( " [ d ] d  n " ,i, n _ array[i]) ;
21 }
22 }
Deleting / Removing an element
02/14/19PPSTheory
1 #include< stdio .h >
2 voidmain() {
3 int n_array[5] = {11 , 12 , 13 , 14 , 15};
4
5 // let ’s p rin t all elem en ts in the n _ a rra y
6 printf( " Array before d eletion :  n ");
7 inti;
8 for(i= 0;i< 5;i++) {
9 printf( " [ d ] d  n " ,i, n_ array[i]) ;
10 }
11
12 printf( " let ’s delete elem ent at index 3  n ");
13 intindex;
14 for(index= 3 ;index< 5 - 1 ;index++) {
15 n _ array[index] =n _ array[index
+1];
16 }
17
18 printf( " Array after d eletion :  n ");
19 for(i= 0;i< 5 - 1;i++) {
20 printf( " [ d ] d  n " ,i,n_ array[i]) ;
21 }
22 }
Program Output:
Array before d eletion : [0] 11
[1] 12
[2] 13
[3] 14
[4] 15
let ’ s delete elem ent at index 3
Array after deletio n :
[0] 11
[1] 12
[2] 13
[3] 15
Multi-dimensional Array
02/14/19PPSTheory
Declaration:
inttable[2][3]; int th ree_ d _ array[3 ][3 ][3 ];
Declaration and Initialization:
inttable1[2][3] = {{1 ,2 ,3} ,{4 ,5 ,6}};
intgrid[][3] = {{1 ,2 ,3} ,{4 ,5 ,6}};
inttable2[2][3] = {1 ,2 ,3 ,4 ,5 ,6};
intth ree_ d _ array[3 ][3 ][3 ] = {
{{10 , 20 , 30} ,{40 , 50 , 60} ,{70 , 80 , 90}} ,
{{11 , 21 , 31} ,{41 , 51 , 61} ,{71 , 81 , 91}} ,
{{33 , 34 , 35} ,{36 , 37 , 38} ,{39 , 40 , 41}}
};
Printing 2D Array
02/14/19PPSTheory
1 #include< stdio .h >
2 voidmain() {
3 introw= 3;
4 intcol= 4;
5 inttable[3][4] =
6 {
7 {1 , 2 , 3 , 4} ,
8 {10 , 11 , 12 , 13} ,
9 {20 , 21 , 22 , 23}
10 };
11 // p rin tin g 2 d a rra y
12 inti,j;
13 for(i= 0;i<row; ++i)
14 {
15 for(j= 0;j<col; ++j)
16 {
17 printf( " d " ,table[i][j]) ;
18 }
19 printf( " n ") ;
20 }
21
22 }
Printing 2D Array
02/14/19PPSTheory
1 #include< stdio .h >
2 voidmain() {
3 introw= 3;
4 intcol= 4;
5 inttable[3][4] =
6 {
7 {1 , 2 , 3 , 4} ,
8 {10 , 11 , 12 , 13} ,
9 {20 , 21 , 22 , 23}
10 };
11 // p rin tin g 2 d a rra y
12 inti,j;
13 for(i= 0;i<row; ++i)
14 {
15 for(j= 0;j<col; ++j)
16 {
17 printf( " d " ,table[i][j]) ;
18 }
19 printf( " n ") ;
20 }
21
22 }
Program Output:
1 2 3 4
10 11 12 13
20 21 22 23
Printing 3D Array
// p rin tin g 3 d a rra y inti,j,k; for(i=0;i<x;i++)
{ for(j=0;j<y;j++) {
for(k=0;k<z;k++) {
printf( " d  t " ,th ree_ d _ array[i][j][k]) ;
}
printf( " n ") ;
}
printf( " n ") ;
}
02/14/19PPSTheory
1#include< stdio .h >
2voidmain() {
3 intx= 3;inty= 3;intz= 3;
4 intth ree_ d _ array[3 ][3 ][3 ] = {
5 {{10 , 20 , 30} ,{40 , 50 , 60} ,{70 , 80 , 90}} ,
6 {{11 , 21 , 31} ,{41 , 51 , 61} ,{71 , 81 , 91}} ,
7 {{33 , 34 , 35} ,{36 , 37 , 38} ,{39 , 40 , 41}}
8 };
9
10
11
12
13
14
15
16
17
18
19
20
21
}
Printing 3D Array
// p rin tin g 3 d a rra y
inti,j,k; for(i=0;i<x;i++) { for(j=0;j<y;j++) {
for(k=0;k<z;k++) {
printf( " d  t " ,th ree_ d _ array[i][j][k]) ;
}
printf( " n ") ;
}
printf( " n ") ;
}
02/14/19PPSTheory
1 #include< stdio .h >
2 voidmain() {
3 intx= 3;inty= 3;intz= 3;
4 intth ree_ d _ array[3 ][3 ][3 ] = {
5 {{10 , 20 , 30} ,{40 , 50 , 60} ,{70 , 80 , 90}} ,
6 {{11 , 21 , 31} ,{41 , 51 , 61} ,{71 , 81 , 91}} ,
7 {{33 , 34 , 35} ,{36 , 37 , 38} ,{39 , 40 , 41}}
8 };
9
10
11
12
13
14
15
16
17
18
19
20
21
}
Program Output:
10 20 30
40 50 60
70 80 90
11 21 31
41 51 61
71 81 91
33 34 35
36 37 38
39 40 41
02/14/19PPSTheory
Thanks for your time and attention!
By: Anupam Sharma
sharmaanupam99@gmail.com

More Related Content

What's hot (20)

Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
1 D Arrays in C++
1 D Arrays in C++1 D Arrays in C++
1 D Arrays in C++
 
Arrays in java
Arrays in javaArrays in java
Arrays in java
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Array lecture
Array lectureArray lecture
Array lecture
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
Arrays
ArraysArrays
Arrays
 
Chap09
Chap09Chap09
Chap09
 
Array in Java
Array in JavaArray in Java
Array in Java
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
Arrays
ArraysArrays
Arrays
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 

Similar to C arrays (20)

Arrays in c
Arrays in cArrays in c
Arrays in c
 
Vcs16
Vcs16Vcs16
Vcs16
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
pointers 1
pointers 1pointers 1
pointers 1
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
CS.3.Arrays.pdf
CS.3.Arrays.pdfCS.3.Arrays.pdf
CS.3.Arrays.pdf
 
Array
ArrayArray
Array
 
arrays
arraysarrays
arrays
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
array2d.ppt
array2d.pptarray2d.ppt
array2d.ppt
 
Ds lab manual by s.k.rath
Ds lab manual by s.k.rathDs lab manual by s.k.rath
Ds lab manual by s.k.rath
 
Data struture lab
Data struture labData struture lab
Data struture lab
 
C_Arrays.pptx
C_Arrays.pptxC_Arrays.pptx
C_Arrays.pptx
 
Arrays
ArraysArrays
Arrays
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Array notes
Array notesArray notes
Array notes
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
Csphtp1 07
Csphtp1 07Csphtp1 07
Csphtp1 07
 

More from CGC Technical campus,Mohali

Data processing and Report writing in Research(Section E)
Data processing and Report writing in Research(Section E)Data processing and Report writing in Research(Section E)
Data processing and Report writing in Research(Section E)CGC Technical campus,Mohali
 
data analysis and report wring in research (Section d)
data analysis and report wring  in research (Section d)data analysis and report wring  in research (Section d)
data analysis and report wring in research (Section d)CGC Technical campus,Mohali
 
Section C(Analytical and descriptive surveys... )
Section C(Analytical and descriptive surveys... )Section C(Analytical and descriptive surveys... )
Section C(Analytical and descriptive surveys... )CGC Technical campus,Mohali
 

More from CGC Technical campus,Mohali (20)

Gender Issues CS.pptx
Gender Issues CS.pptxGender Issues CS.pptx
Gender Issues CS.pptx
 
Intellectual Property Rights.pptx
Intellectual Property Rights.pptxIntellectual Property Rights.pptx
Intellectual Property Rights.pptx
 
Cyber Safety ppt.pptx
Cyber Safety ppt.pptxCyber Safety ppt.pptx
Cyber Safety ppt.pptx
 
Python Modules .pptx
Python Modules .pptxPython Modules .pptx
Python Modules .pptx
 
Dynamic allocation
Dynamic allocationDynamic allocation
Dynamic allocation
 
Control statments in c
Control statments in cControl statments in c
Control statments in c
 
Operators in c by anupam
Operators in c by anupamOperators in c by anupam
Operators in c by anupam
 
Datatypes in c
Datatypes in cDatatypes in c
Datatypes in c
 
Fundamentals of-computer
Fundamentals of-computerFundamentals of-computer
Fundamentals of-computer
 
Searching
Searching Searching
Searching
 
File handling-c
File handling-cFile handling-c
File handling-c
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Intro
IntroIntro
Intro
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Function in c
Function in cFunction in c
Function in c
 
string in C
string in Cstring in C
string in C
 
Data processing and Report writing in Research(Section E)
Data processing and Report writing in Research(Section E)Data processing and Report writing in Research(Section E)
Data processing and Report writing in Research(Section E)
 
data analysis and report wring in research (Section d)
data analysis and report wring  in research (Section d)data analysis and report wring  in research (Section d)
data analysis and report wring in research (Section d)
 
Section C(Analytical and descriptive surveys... )
Section C(Analytical and descriptive surveys... )Section C(Analytical and descriptive surveys... )
Section C(Analytical and descriptive surveys... )
 
Researchmethodology
ResearchmethodologyResearchmethodology
Researchmethodology
 

Recently uploaded

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 

Recently uploaded (20)

Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 

C arrays

  • 1. Array 02/14/19PPSTheory Er. Anupam Sharma Assistant Professor anupamcgctc@gmail.com
  • 2. Contents 02/14/19PPSTheory 1 Array 2 Declaration and Initialization 3 Array Index 4 Accessing Array Elements and Printing 5 Insertion 6 Insertion and Printing 7 Deleting / Removing of an element 8 Multi-dimensional Array 9 Printing 2D Array 10 Printing 3D Array
  • 3. Textbook and Reference Books 02/14/19PPSTheory Textbook: •Let Us C, by Yashavant Kanetkar, 13th Edition, Published by BPB Publications. Reference books: •Teach Yourself C, by Herbert Schildt, 4th Edition, Published by Osborne. •Sams Teach Yourself C, by Bradley L. Jones & Peter Aitken, 6th Edition. •The C Programming Language, by Brian W. Kernighan & Dennis M. Ritchie, 2nd Edition, Published by Prentice Hall.
  • 4. Array 02/14/19PPSTheory An array is a collection of data storage locations, each storing the same type of data and having the same name. intnum ber[5]; floatmarks[10]; doublesalary[10]; charalphabet[5]; –Each storage location in an array is called an array element.
  • 5. Array (Cont’d) 02/14/19PPSTheory –An array is a collection of variables of same data types. –All elements of array are stored in the contiguous memory locations. –The size of array must be a constant integral value. –Individual elements in an array can be accessed by the name of the array and an integer enclosed in square bracket called subscript/index variable like number [10]. –The first element in an array is at index 0, whereas the last element is at index [ArraySize - 1].
  • 6. Array Declaration and Initialization 02/14/19PPSTheory Array Declaration: intnum ber[5]; floatmarks[5]; doublesalary[5]; Array Initialization: marks[0] = 5; marks[1] = 2; marks[2] = 9; marks[3] = 1; marks[4] = 1; Array Declaration and Initialization: intnum ber[5] = {1 ,2 ,3 ,4 ,500}; floatscore[10] = {10 ,8 ,7 ,9 ,4.5 ,6 ,7 ,9 ,8.5 ,10}; doublesalary[5] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 3 0 0 0 0 .0 0 }; doublesalary[] = {10000.00 , 15000.00 , 20500.500 , 5050.50 , 3 0 0 0 0 .0 0 }; charalphabet[5] = { ’A ’ , ’B ’, ’C ’, ’D ’ , ’E ’};
  • 7. Array Index 02/14/19PPSTheory -Array index is the location of an element in an array -You can access elements of an array by indices -For example, Let’s get the following salary array elements using index numbers - 1 doublesalary[5] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 3 0 0 0 0 .0 0 }; 2 doublesalary1=salary[0]; 3 doublesalary2=salary[1]; 4 doublesalary3=salary[2]; 5 doublesalary4=salary[3]; 6 doublesalary5=salary[4];
  • 8. Accessing elements using index and printing with loops 02/14/19PPSTheory 1 #include< stdio .h > 2 voidmain() { 3 charalphabet[5]={ ’A ’, ’B ’, ’C ’,’D ’,’E ’}; 4 5 /* Let ’s p rin t all a lp h a b ets one by one */ 6 inti; 7 for(i=0;i<5;i++) { 8 printf( " c " ,alph abet[i]) ; 9 } 10 } 1 #include< stdio .h > 2 voidmain() { 3 doublesalary[] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 3 0 0 0 0 .0 0 }; 4 5 /* Let ’s p rin t all sa la ry one by one */ 6 inti; 7 for(i=0;i<5;i++) { 8 printf( " S alary [ d ]: .2 lf n " ,i,salary[i]) ; 9 } 10 }
  • 9. Accessing elements using index and printing with loops 02/14/19PPSTheory 1 #include< stdio .h > 2 voidmain() { 3 charalphabet[5]={ ’A ’, ’B ’, ’C ’,’D ’,’E ’}; 4 5 /* Let ’s p rin t all a lp h a b ets one by one */ 6 inti; 7 for(i=0;i<5;i++) { 8 printf( " c " ,alph abet[i]) ; 9 } 10 } 1 #include< stdio .h > 2 voidmain() { 3 doublesalary[] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 3 0 0 0 0 .0 0 }; 4 5 /* Let ’s p rin t all sa la ry one by one */ 6 inti; 7 for(i=0;i<5;i++) { 8 printf( " S alary [ d ]: .2 lf n " ,i,salary[i]) ; 9 } 10 } Program Output: A B C D E Program Output: S alary [0]: 10 00 0.0 0 S alary [1]: 15 00 0.0 0 S alary [2]: 20 50 0.5 0 S alary [3]: 5050.50 S alary [4]: 30 00 0.0 0
  • 10. Insertion: Insert elements in an array 02/14/19PPSTheory 1 #include< stdio .h > 2 voidmain() { 3 intsize= 5; 4 floatmarks[size]; 5 6 /* In sertin g m arks into m a rks a rra y */ 7 inti; 8 for(i= 0;i<=size;i++) { 9 printf( " Enter a mark : "); 10 scanf(" f" , &marks[i]) ; 11 } 12 13}
  • 11. Insertion: Insert elements in an array 02/14/19PPSTheory 1 #include< stdio .h > 2 voidmain() { 3 intsize= 5; 4 floatmarks[size]; 5 6 /* In sertin g m arks into m a rks a rra y */ 7 inti; 8 for(i= 0;i<=size;i++) { 9 printf( " Enter a mark : "); 10 scanf(" f" , &marks[i]) ; 11 } 12 13 } Program Output: Enter a mark : 50.5 Enter a mark : 40 Enter a mark : 30.5 Enter a mark : 75.5 Enter a mark : 80 Enter a mark : 90
  • 12. Insertion and Printing 02/14/19PPSTheory 1 #include< stdio .h > 2 Void m ain () { 3 intsize= 5; 4 floatmarks[size]; 5 6 /* In sertin g m arks into m a rks a rra y */ 7 inti; 8 for(i= 0;i<=size;i++) { 9 printf( " Enter a marks : "); 10 scanf(" f" , &marks[i]) ; 11 } 12 13 /* Let ’s p rin t all m arks in the m a rks arra y */ 14 15 printf( " n P rin tin g all marks : "); 16 17 intj; 18 for(j= 0;j<=size;j++) { 19 printf( " n . f " ,marks[j]) ; 20 } 21 22 }
  • 13. Insertion and Printing 02/14/19PPSTheory 1 #include< stdio .h > 2 Void main() { 3 intsize= 5; 4 floatmarks[size]; 5 6 /* In sertin g m arks into m a rks a rra y */ 7 inti; 8 for(i= 0;i<=size;i++) { 9 printf( " Enter a marks : "); 10 scanf(" f" , &marks[i]) ; 11 } 12 13 /* Let ’s p rin t all m arks in the m a rks arra y */ 14 15 printf( " n P rin tin g all marks : "); 16 17 intj; 18 for(j= 0;j<=size;j++) { 19 printf( " n .1 f " ,marks[j]) ; 20 } 21 22 } Program Output: Enter a marks : 80 Enter a marks : 85.5 Enter a marks : 75 Enter a marks : 63.5 Enter a marks : 90 Enter a marks : 11.0 P rin tin g all marks : 80.0 85.5 75.0 63.5 90.0
  • 14. Deleting / Removing an element 02/14/19PPSTheory 1 #include< stdio .h > 2 voidmain() { 3 int n_ array[5] = {11 , 12 , 13 , 14 , 15}; 4 5 // let ’s p rin t all elem en ts in the n _ a rra y 6 printf( " Array before d eletion : n "); 7 inti; 8 for(i= 0;i< 5;i++) { 9 printf( " [ d ] d n " ,i, n_ array[i]) ; 10 } 11 12 printf( " let ’s delete elem ent at index 3 n "); 13 intindex; 14 for(index= 3 ;index< 5 - 1 ;index++) { 15 n _ array[index] = n _ array[index +1]; 16 } 17 18 printf( " Array after d eletion : n "); 19 for(i= 0;i< 5 - 1;i++) { 20 printf( " [ d ] d n " ,i, n _ array[i]) ; 21 } 22 }
  • 15. Deleting / Removing an element 02/14/19PPSTheory 1 #include< stdio .h > 2 voidmain() { 3 int n_array[5] = {11 , 12 , 13 , 14 , 15}; 4 5 // let ’s p rin t all elem en ts in the n _ a rra y 6 printf( " Array before d eletion : n "); 7 inti; 8 for(i= 0;i< 5;i++) { 9 printf( " [ d ] d n " ,i, n_ array[i]) ; 10 } 11 12 printf( " let ’s delete elem ent at index 3 n "); 13 intindex; 14 for(index= 3 ;index< 5 - 1 ;index++) { 15 n _ array[index] =n _ array[index +1]; 16 } 17 18 printf( " Array after d eletion : n "); 19 for(i= 0;i< 5 - 1;i++) { 20 printf( " [ d ] d n " ,i,n_ array[i]) ; 21 } 22 } Program Output: Array before d eletion : [0] 11 [1] 12 [2] 13 [3] 14 [4] 15 let ’ s delete elem ent at index 3 Array after deletio n : [0] 11 [1] 12 [2] 13 [3] 15
  • 16. Multi-dimensional Array 02/14/19PPSTheory Declaration: inttable[2][3]; int th ree_ d _ array[3 ][3 ][3 ]; Declaration and Initialization: inttable1[2][3] = {{1 ,2 ,3} ,{4 ,5 ,6}}; intgrid[][3] = {{1 ,2 ,3} ,{4 ,5 ,6}}; inttable2[2][3] = {1 ,2 ,3 ,4 ,5 ,6}; intth ree_ d _ array[3 ][3 ][3 ] = { {{10 , 20 , 30} ,{40 , 50 , 60} ,{70 , 80 , 90}} , {{11 , 21 , 31} ,{41 , 51 , 61} ,{71 , 81 , 91}} , {{33 , 34 , 35} ,{36 , 37 , 38} ,{39 , 40 , 41}} };
  • 17. Printing 2D Array 02/14/19PPSTheory 1 #include< stdio .h > 2 voidmain() { 3 introw= 3; 4 intcol= 4; 5 inttable[3][4] = 6 { 7 {1 , 2 , 3 , 4} , 8 {10 , 11 , 12 , 13} , 9 {20 , 21 , 22 , 23} 10 }; 11 // p rin tin g 2 d a rra y 12 inti,j; 13 for(i= 0;i<row; ++i) 14 { 15 for(j= 0;j<col; ++j) 16 { 17 printf( " d " ,table[i][j]) ; 18 } 19 printf( " n ") ; 20 } 21 22 }
  • 18. Printing 2D Array 02/14/19PPSTheory 1 #include< stdio .h > 2 voidmain() { 3 introw= 3; 4 intcol= 4; 5 inttable[3][4] = 6 { 7 {1 , 2 , 3 , 4} , 8 {10 , 11 , 12 , 13} , 9 {20 , 21 , 22 , 23} 10 }; 11 // p rin tin g 2 d a rra y 12 inti,j; 13 for(i= 0;i<row; ++i) 14 { 15 for(j= 0;j<col; ++j) 16 { 17 printf( " d " ,table[i][j]) ; 18 } 19 printf( " n ") ; 20 } 21 22 } Program Output: 1 2 3 4 10 11 12 13 20 21 22 23
  • 19. Printing 3D Array // p rin tin g 3 d a rra y inti,j,k; for(i=0;i<x;i++) { for(j=0;j<y;j++) { for(k=0;k<z;k++) { printf( " d t " ,th ree_ d _ array[i][j][k]) ; } printf( " n ") ; } printf( " n ") ; } 02/14/19PPSTheory 1#include< stdio .h > 2voidmain() { 3 intx= 3;inty= 3;intz= 3; 4 intth ree_ d _ array[3 ][3 ][3 ] = { 5 {{10 , 20 , 30} ,{40 , 50 , 60} ,{70 , 80 , 90}} , 6 {{11 , 21 , 31} ,{41 , 51 , 61} ,{71 , 81 , 91}} , 7 {{33 , 34 , 35} ,{36 , 37 , 38} ,{39 , 40 , 41}} 8 }; 9 10 11 12 13 14 15 16 17 18 19 20 21 }
  • 20. Printing 3D Array // p rin tin g 3 d a rra y inti,j,k; for(i=0;i<x;i++) { for(j=0;j<y;j++) { for(k=0;k<z;k++) { printf( " d t " ,th ree_ d _ array[i][j][k]) ; } printf( " n ") ; } printf( " n ") ; } 02/14/19PPSTheory 1 #include< stdio .h > 2 voidmain() { 3 intx= 3;inty= 3;intz= 3; 4 intth ree_ d _ array[3 ][3 ][3 ] = { 5 {{10 , 20 , 30} ,{40 , 50 , 60} ,{70 , 80 , 90}} , 6 {{11 , 21 , 31} ,{41 , 51 , 61} ,{71 , 81 , 91}} , 7 {{33 , 34 , 35} ,{36 , 37 , 38} ,{39 , 40 , 41}} 8 }; 9 10 11 12 13 14 15 16 17 18 19 20 21 } Program Output: 10 20 30 40 50 60 70 80 90 11 21 31 41 51 61 71 81 91 33 34 35 36 37 38 39 40 41
  • 21. 02/14/19PPSTheory Thanks for your time and attention! By: Anupam Sharma sharmaanupam99@gmail.com