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].
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