SlideShare a Scribd company logo
1 of 25
Array
2/22/2020PPSTheory
Er. Anupam Sharma
Assistant Professor
anupamcgctc@gmail.com
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.
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.
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].
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 ’ } ;
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 ] ;
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 }
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
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 }
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
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 }
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
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 ] ) ;
} }
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
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}}
} ;
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 }
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
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
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
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
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
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
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 }
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
2/22/2020PPSTheory
Thanks for your time and attention!
By: Anupam Sharma
sharmaanupam99@gmail.com

More Related Content

What's hot

What's hot (20)

C program
C programC program
C program
 
BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
programs
programsprograms
programs
 
C Code and the Art of Obfuscation
C Code and the Art of ObfuscationC Code and the Art of Obfuscation
C Code and the Art of Obfuscation
 
C++ programming pattern
C++ programming patternC++ programming pattern
C++ programming pattern
 
Understanding storage class using nm
Understanding storage class using nmUnderstanding storage class using nm
Understanding storage class using nm
 
Lecture 2: arrays and pointers
Lecture 2: arrays and pointersLecture 2: arrays and pointers
Lecture 2: arrays and pointers
 
Understanding static analysis php amsterdam 2018
Understanding static analysis   php amsterdam 2018Understanding static analysis   php amsterdam 2018
Understanding static analysis php amsterdam 2018
 
Vcs16
Vcs16Vcs16
Vcs16
 
Pattern printing-in-c(Jaydip Kikani)
Pattern printing-in-c(Jaydip Kikani)Pattern printing-in-c(Jaydip Kikani)
Pattern printing-in-c(Jaydip Kikani)
 
Data struture lab
Data struture labData struture lab
Data struture lab
 
C programs
C programsC programs
C programs
 
Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03
 
FSOFT - Test Java Exam
FSOFT - Test Java ExamFSOFT - Test Java Exam
FSOFT - Test Java Exam
 
Program for hamming code using c
Program for hamming code using cProgram for hamming code using c
Program for hamming code using c
 
Prog
ProgProg
Prog
 
C aptitude
C aptitudeC aptitude
C aptitude
 
Data Structure Project File
Data Structure Project FileData Structure Project File
Data Structure Project File
 
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
 
computer notes - Data Structures - 3
computer notes - Data Structures - 3computer notes - Data Structures - 3
computer notes - Data Structures - 3
 

Similar to Arrays in c

Similar to Arrays in c (20)

C arrays
C arraysC arrays
C arrays
 
Java, Up to Date Sources
Java, Up to Date SourcesJava, Up to Date Sources
Java, Up to Date Sources
 
C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
 
javapravticalfile.doc
javapravticalfile.docjavapravticalfile.doc
javapravticalfile.doc
 
Arrays
ArraysArrays
Arrays
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Qno 1 (d)
Qno 1 (d)Qno 1 (d)
Qno 1 (d)
 
Arrays
ArraysArrays
Arrays
 
Arrays in C
Arrays in CArrays in C
Arrays in C
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
Bai Giang 11
Bai Giang 11Bai Giang 11
Bai Giang 11
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
C Programs.pptx
C Programs.pptxC Programs.pptx
C Programs.pptx
 
Thoughts On Learning A New Programming Language
Thoughts On Learning A New Programming LanguageThoughts On Learning A New Programming Language
Thoughts On Learning A New Programming Language
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
c programming
c programmingc programming
c programming
 
Array
ArrayArray
Array
 
GraphQL Relay Introduction
GraphQL Relay IntroductionGraphQL Relay Introduction
GraphQL Relay Introduction
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
Arrays
ArraysArrays
Arrays
 

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

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...KokoStevan
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 

Recently uploaded (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
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
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

Arrays in c

  • 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
  • 25. 2/22/2020PPSTheory Thanks for your time and attention! By: Anupam Sharma sharmaanupam99@gmail.com