2. Textbook and Reference
Books
2/22/2020PPSTheory
Textbook:
• Let Us C, byYashavant Kanetkar, 13th Edition, Published byBPB Publications.
Reference books:
• Teach Yourself C, byHerbert Schildt, 4thEdition, Published by Osborne.
• Sams Teach Yourself C, byBradley L. Jones & Peter Aitken, 6thEdition.
• The C Programming Language, byBrian W. Kernighan& Dennis M. Ritchie, 2nd
Edition, Published byPrentice Hall.
3. Array
2/22/2020PPSTheory
Anarray is a collectionofdata storage locations, each storing the same type of
data and having the same name.
int num b e r [ 5 ] ;
f l o a t m a r k s [ 1 0 ] ;
d o u b l e s a l a r y [ 1 0 ] ;
c h a r a l p h a b e t [ 5 ] ;
–Eachstorage location in an array is called anarray element.
4. Array (Cont’d)
2/22/2020PPSTheory
–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 integralvalue.
–Individual elements in an array can be accessed by the name of the array and an
integer enclosedin squarebracket 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].
5. Array Declaration and Initialization
2/22/2020PPSTheory
Array Declaration:
int num b e r [ 5 ] ;
f l o a t m a r k s [ 5 ] ;
doubl e s a l a r y[ 5] ;
Array Initialization:
marks[0] = 5 ;
marks[1] = 2 ;
marks[2] = 9 ;
marks[3] = 1 ;
marks[4] = 1 ;
Array DeclarationandInitialization:
int num b e r [ 5 ] = {1 , 2 , 3 , 4 , 5 0 0 } ;
f l o a t s c o r e [ 1 0 ] = {10 , 8 , 7 , 9 , 4 . 5 , 6 , 7 , 9 , 8 . 5 , 1 0 } ;
d o u b l e s a l a r y [ 5 ] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 30000. 0 0} ;
d o u b l e s a l a r y [ ] = {10000.00 , 15000.00 , 20500.500 , 5050.50 , 30000. 0 0} ;
c h a r a l p h a b et [ 5 ] = { ’A’ , ’B ’ , ’C ’ , ’D’ , ’E ’ } ;
6. Array Index
2/22/2020PPSTheory
-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 d o u b l e s a l a r y [ 5 ] = {10000.00 , 15000.00 , 20500.50 , 5050.50 , 30000. 0 0} ;
2 d o u b l e s a l a r y 1 = s a l a r y [ 0 ] ;
3 d o u b l e s a l a r y 2 = s a l a r y [ 1 ] ;
4 d o u b l e s a l a r y 3 = s a l a r y [ 2 ] ;
5 d o u b l e s a l a r y 4 = s a l a r y [ 3 ] ;
6 d o u b l e s a l a r y 5 = s a l a r y [ 4 ] ;
7. Accessing elements using index andprinting with loops
2/22/2020PPSTheory
1 #include< s t d i o . h >
2 void main() {
3 c h a r a l p h a b e t [ 5 ] = { ’A’ , ’B ’ , ’C ’ , ’ D ’ , ’ E ’ } ;
4
5 / * Let ’ s pr i n t a l l al p habe t s one by one * /
6 i n t i ;
7 for(i = 0 ; i < 5 ; i + + ) {
8 p r i n t f ( " c " , a l p h a b e t [ i ] ) ;
9 }
10 }
1 #include< s t d i o . h >
2 void main() {
3 d o u b l e s a l a r y [ ] = {10000.00 , 15000.00 , 20500.50 ,
5050.50 , 30000. 0 0} ;
4
5 / * Let ’ s pr i n t a l l sa l a ry one by one * /
6 i n t i ;
7 for(i = 0 ; i < 5 ; i + + ) {
8 p r i n t f ( " Sa l a r y [ d] : . 2 l f n" , i , s a l a r y [ i ] ) ;
9 }
10 }
8. Accessing elements using index andprinting with loops
2/22/2020PPSTheory
1 #include< s t d i o . h >
2 void main() {
3 c h a r a l p h a b e t [ 5 ] = { ’A’ , ’B ’ , ’C ’ , ’ D ’ , ’ E ’ } ;
4
5 / * Let ’ s pr i n t a l l al p habe t s one by one * /
6 i n t i ;
7 for(i = 0 ; i < 5 ; i + + ) {
8 p r i n t f ( " c " , a l p h a b e t [ i ] ) ;
9 }
10 }
1 #include< s t d i o . h >
2 void main() {
3 d o u b l e s a l a r y [ ] = {10000.00 , 15000.00 , 20500.50 ,
5050.50 , 30000. 0 0} ;
4
5 / * Le t ’ s pr i n t a l l sa l a ry one by one * /
6 i n t i ;
7 for(i = 0 ; i < 5 ; i + + ) {
8 p r i n t f ( " Sa l a r y [ d] : . 2 l f n" , i , s a l a r y [ i ] ) ;
9 }
10 }
Program Output:
ABCDE
Program Output:
Sa l a r y [ 0 ] : 1000 0 . 0 0
Sa l a r y [ 1 ] : 1500 0 . 0 0
Sa l a r y [ 2 ] : 2050 0 . 5 0
Sa l a r y [ 3 ] : 5050.50
Sa l a r y [ 4 ] : 3000 0 . 0 0
9. Insertion: Insert elements in an array
2/22/2020PPSTheory
1 #include< s t d i o . h >
2 voi d ma i n( ) {
3 i n t s i z e = 5 ;
4 f l o a t m a r k s [ s i z e ] ;
5
6 / * In s e r t i n g ma rk s i n t o mar k s arra y * /
7 i n t i ;
8 f o r ( i = 0 ; i < = si ze; i + +) {
9 p r i n t f ( " E n t e r a mark : " ) ;
10 s c a n f ( " f " , &marks[i]) ;
11 }
12
13 }
10. Insertion: Insert elements in an array
2/22/2020PPSTheory
1 #include< s t d i o . h >
2 voi d ma i n( ) {
3 i n t s i z e = 5 ;
4 f l o a t m a r k s [ s i z e ] ;
5
6 / * In s e r t i n g ma rk s i n t o mar k s arra y * /
7 i n t i ;
8 f o r ( i = 0 ; i < = si ze; i + +) {
9 p r i n t f ( " E n t e r a mark : " ) ;
10 s c a n f ( " f " , &marks[i]) ;
11 }
12
13 }
Program Output:
E n t er a mark : 5 0 . 5
E n t e r a mark : 40
E n t e r a mark : 3 0 . 5
E n t e r a mark : 7 5 . 5
E n t e r a mark : 80
E n t e r a mark : 90
11. Insertion and Printing
2/22/2020PPSTheory
1 #include< s t d i o . h >
2 voi d ma i n ( ) {
3 i n t s i z e = 5 ;
4 f l o a t m a r k s [ s i z e ] ;
5
6 / * In s e r t i n g ma rk s i n t o mar k s arra y * /
7 i n t i ;
8 f o r ( i = 0 ; i < = si ze; i + +) { p r i n t f ( " E n t e r a marks : " ) ;
10 s c a n f ( " f " , &marks[i]) ;
11 }
12
13 / * Le t ’ s p r i n t a l l ma rk s i n t h e ma r k s a rra y * /
14
15 p r i n t f ( " nPr i n t i n g a l l marks : " ) ;
16
17 i n t j ;
18 f o r ( j = 0 ; j < = si ze; j + +) {
19 p r i n t f ( " n . f " , m a r k s [ j ] ) ;
20 }
21
22 }
12. Insertion and Printing
2/22/2020PPSTheory
1 #include< s t d i o . h >
2 voi d ma i n( ) {
3 i n t s i z e = 5 ;
4 f l o a t m a r k s [ s i z e ] ;
5
6 / * In s e r t i n g ma rk s i n t o mar k s arra y * /
7 i n t i ;
8 f o r ( i = 0 ; i < = si ze; i + +) {
9 p r i n t f ( " E n t e r a marks : " ) ;
10 s c a n f ( " f " , &marks[i]) ;
11 }
12
13 / * Le t ’ s p r i n t a l l ma rk s i n t h e ma r k s a rra y * /
14
15 p r i n t f ( " nPr i n t i n g a l l marks : " ) ;
16
17 i n t j ;
18 f o r ( j = 0 ; j < = si ze; j ++) {
19 p r i n t f ( " n . 1 f " , m a r k s [ j ] ) ;
20 }
21
22 }
Program Output:
E n t e r a marks : 80
E n t e r a marks : 8 5 . 5
E n t er a marks : 75
E n t e r a marks : 6 3 . 5
E n t e r a marks : 90
E n t e r a marks : 1 1 . 0
Pr i n t i n g a l l marks :
8 0 . 0
8 5 . 5
7 5 . 0
6 3 . 5
9 0 . 0
13. Deleting / Removing an element
2/22/2020PPSTheory
#include< s t d i o . h >
void main() {
i n t n _ a r r a y [ 5 ] = {11 , 12 , 13 , 14 , 1 5 };
/ / l e t ’ s pr i n t a l l elem en t s i n t h e n_ ar r a y
p r i n t f ( " Array b e f o r e de l e t i o n : n" ) ;
i n t i ;
f o r ( i = 0;i< 5 ; i ++) {
p r i n t f ( " [ d] d n" , i , n _ a r r a y [ i ] ) ;
}
p r i n t f ( " l e t ’ s d e l e t e elem e n t a t i n d ex 3 n" ) ;
i n t i n d e x ;
for(i n d ex= 3 ;index< 5 - 1 ; i n dex++)
{
n_a r r a y [ i n d e x ] = n_ a r r a y [ i n d e x + 1 ] ;
}
p r i n t f ( " Array a f t e r de l e t i o n : n" ) ;
f o r ( i = 0;i< 5 - 1 ; i + +) {
p r i n t f ( " [ d] d n" , i , n _ a r r a y [ i ] ) ;
} }
14. Deleting / Removing an element
2/22/2020PPSTheory
1 #include< s t d i o . h >
2 void main() {
3 i n t n _ a r r a y [ 5 ] = {11 , 12 , 13 , 14 , 1 5 };
4
5 / / l e t ’ s pr i n t a l l elem en t s i n t h e n_ ar r a y
6 p r i n t f ( " Array b e f o r e de l e t i o n : n" ) ;
7 i n t i ;
8 f o r ( i = 0;i< 5 ; i ++) {
9 p r i n t f ( " [ d] d n" , i , n _ a r r a y [ i ] ) ;
10 }
11
12 p r i n t f ( " l e t ’ s d e l e t e elem e n t a t i n d ex 3 n" ) ;
13 i n t i n d e x ;
14 for(i n d ex= 3 ;index< 5 - 1 ; i n dex++) {
15 n_ a r r a y [ i n d e x ] =n_ a r r a y [ i n d e x + 1 ] ;
16 }
17
18 p r i n t f ( " Array a f t e r de l e t i o n : n" ) ;
19 f o r ( i = 0;i< 5 - 1 ; i + +)
{
20 p r i n t f ( " [ d] d n" , i , n _ a r r a y [ i ] ) ;
21 }
22 }
Program Output:
Array b e f o r e d e l e t i o n :
[ 0 ] 11
[ 1 ] 12
[ 2 ] 13
[ 3 ] 14
[ 4 ] 15
l e t ’ s d e l e t e elem e n t a t
i n d ex 3
Array a f t e r d e l e t i o n:
[ 0 ] 11
[ 1 ] 12
[ 2 ] 13
[ 3 ] 15
15. Multi-dimensional Array
2/22/2020PPSTheory
Declaration:
i n t t a b l e [ 2 ] [ 3 ] ; i n t t h r ee_ d_a r r a y [ 3 ] [ 3 ] [ 3 ] ;
Declaration and Initialization:
i n t t a b l e 1 [ 2 ] [ 3 ] = {{1 , 2 , 3 } , { 4 , 5 , 6 } } ;
i n t g r i d [ ] [ 3 ] = {{1 , 2 , 3 } , { 4 , 5 , 6 } } ;
i n t t a b l e 2 [ 2 ] [ 3 ] = {1 , 2 , 3 , 4 , 5 , 6 } ;
i n t t h r e e _ d_a r r a y [ 3 ] [ 3 ] [ 3 ] = {
{{10 , 20 , 30} , {4 0 , 50 , 60} , {7 0 , 80 , 90}} ,
{{11 , 21 , 31} , {4 1 , 51 , 61} , {7 1 , 81 , 91}} ,
{{33 , 34 , 35} , {3 6 , 37 , 38} , {3 9 , 40 , 41}}
} ;
16. Printing 2D Array
2/22/2020PPSTheory
1 #include< s t d i o . h >
2 void main() {
3 int row= 3 ;
4 i n t c o l = 4 ;
5 i n t t a b l e [ 3 ] [ 4 ] =
6 {
7 {1 , 2 , 3 , 4} ,
8 {10 , 11 , 12 , 13} ,
9 {20 , 21 , 22 , 23}
10 } ;
11 / / pr i n t i n g 2d arra y
12 i n t i , j ;
13 f o r ( i = 0;i<row; ++i)
14 {
15 f o r ( j = 0 ; j < c o l ; ++j)
16 {
17
p r i n t f ( " d " , t a b l e [ i ] [ j ] ) ;
18 }
19 p r i n t f ( " n" ) ;
20 }
21
22 }
17. Printing 2D Array
2/22/2020PPSTheory
1 #include< s t d i o . h >
2 void main() {
3 int row= 3 ;
4 i n t c o l = 4 ;
5 i n t t a b l e [ 3 ] [ 4 ] =
6 {
7 {1 , 2 , 3 , 4} ,
8 {10 , 11 , 12 , 13} ,
9 {20 , 21 , 22 , 23}
10 } ;
11 / / pr i n t i n g 2d arra y
12 i n t i , j ;
13 f o r ( i = 0;i<row; ++i)
14 {
15 f o r ( j = 0 ; j < c o l ; ++j)
16 {
17
p r i n t f ( " d " , t a b l e [ i ] [ j ] ) ;
18 }
19 p r i n t f ( " n" ) ;
20 }
21
22 }
Program Output:
1 2 3 4
10 11 12 13
20 21 22 23
18. Additionof two matrixin C
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrixn");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrixn");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrixn");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-n");
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%dt", sum[c][d]);
}
printf("n");
}
return 0;
}
2/22/2020PPSTheory
19. C Program to Subtract Two Matrices
#include <stdio.h>
int main()
{
int m, n, c, d, first[5][5], second[5][5] , difference[5][5];
printf("Enter the number of rows and columns of matrixn");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrixn");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrixn");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &second[c][d]);
printf("Difference of entered matrices:-n");
for (c = 0; c < m; c++) {
for (d = 0; d < n; d++) {
difference[c][d] = first[c][d] - second[c][d];
printf("%dt“,difference[c][d]);
}
printf("n");
}
return 0; }
2/22/2020PPSTheory
20. Matrix Multiplication in C
#include<stdio.h>
int main()
{ int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
printf("Enter rows and columns of first
matrix:");
scanf("%d%d",&m,&n);
printf("Enter rows and columns of second
matrix:");
scanf("%d%d",&p,&q);
if(n==p)
{ printf("nEnter first matrix:n");
for(i=0;i<m;++i)
for(j=0;j<n;++j)
scanf("%d",&a[i][j]);
printf("nEnter second matrix:n");
for(i=0;i<p;++i)
for(j=0;j<q;++j)
scanf("%d",&b[i][j]);
printf("nThe new matrix is:n");
for(i=0;i<m;++i)
{ for(j=0;j<q;++j)
{ c[i][j]=0;
2/22/2020PPSTheory
for(k=0;k<n;++k)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
printf("%d ",c[i][j]);
} printf("n"); }
}
else
printf("nSorry!!!! Matrix multiplication can't be
done");
return 0; }
Output
Enter rows and columns of first matrix:3
3
Enter rows and columns of second matrix:3
3
Enter first matrix:
1 2 3
4 5 6
7 8 9
Enter second matrix:
9 8 7
6 5 4
3 2 1
The new matrix is:
30 24 18
84 69 54
138 114 90
21. CProgram toFind Largest and Smallest Element inArray
#include<stdio.h>
int main()
{ int a[50],i,n,large,small;
printf("How many elements:");
scanf("%d",&n);
printf("Enter the Array:");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<n;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i]; }
printf("The largest element is %d",large);
printf("nThe smallest element is %d",small);
return 0; }
2/22/2020PPSTheory
Output
How many elements:5
Enter the Array:2 8 12 7
The largest element is 12
The smallest element is 2
22. Cprogramto acceptNnumbersandarrangetheminanascendingorder
#include <stdio.h>
void main()
{ int i, j, a, n, number[30];
printf("Enter the value of N n");
scanf("%d", &n);
printf("Enter the numbers n");
for (i = 0; i < n; ++i)
scanf("%d", &number[i]);
for (i = 0; i < n; ++i)
{ for (j = i + 1; j < n; ++j)
{ if (number[i] > number[j])
{ a = number[i];
number[i] = number[j];
number[j] = a;
} } }
printf("The numbers arranged in ascending order are given below n");
for (i = 0; i < n; ++i)
printf("%dn", number[i]); }
2/22/2020PPSTheory
Output:
Enter the value of N 6
Enter the numbers 30
87
90
456
781
200
The numbers
arranged in ascending
order are given below
30
87
90
200
456
781
23. Printing 3D Array
/ / prin t i n g 3d arra y
i n t i , j , k ;
for(i=0;i<x;i++) {
for(j=0;j<y;j++) {
for(k=0;k<z;k++) {
p r i n t f ( " d t " , t h ree_ d_array [i][j][k]) ;
}
p r i n t f ( " n" ) ;
}
p r i n t f ( " n" ) ;
}
2/22/2020PPSTheory
1#include< s t d i o . h >
2void main() {
3 int x= 3 ; i nt y= 3 ; i n t z= 3 ;
4 i n t t h r e e _ d_a r r a y [ 3 ] [ 3 ] [ 3 ] = {
5 {{10 , 20 , 30} , {4 0 , 50 , 60} , {7 0 , 80, 90}} ,
6 {{11 , 21 , 31} , {4 1 , 51 , 61} , {7 1 , 81, 91}} ,
7 {{33 , 34 , 35} , {3 6 , 37 , 38} , {3 9 , 40, 41}}
8 } ;
9
10
11
12
13
14
15
16
17
18
19
20
21 }
24. Printing 3D Array
/ / prin t i n g 3 d arra y
i n t i , j , k ;
for(i=0;i<x;i++) { for(j=0;j<y;j++) {
for(k=0;k<z;k++) {
p r i n t f ( " d t " , t h ree_ d_array[i][j][k]) ;
}
p r i n t f ( " n" ) ;
}
p r i n t f ( " n" ) ;
}
2/22/2020PPSTheory
1 #include< s t d i o . h >
2 void main() {
3 i nt x= 3 ; i nt y= 3 ; i n t z= 3 ;
4 i n t t h r e e _ d_a r r a y [ 3 ] [ 3 ] [ 3 ] = {
5 {{10 , 20 , 30} , {4 0 , 50 , 60} , {7 0 , 80, 90}} ,
6 {{11 , 21 , 31} , {4 1 , 51 , 61} , {7 1 , 81, 91}} ,
7 {{33 , 34 , 35} , {3 6 , 37 , 38} , {3 9 , 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