SlideShare a Scribd company logo
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

PPT
PDF
C programs
PPS
pointers 1
PDF
Some stuff about C++ and development
PDF
Most Important C language program
DOCX
Ejercicios de programacion
PDF
1z0 851 exam-java standard edition 6 programmer certified professional
C programs
pointers 1
Some stuff about C++ and development
Most Important C language program
Ejercicios de programacion
1z0 851 exam-java standard edition 6 programmer certified professional

What's hot (20)

PDF
C program
PDF
PDF
programs
PDF
C Code and the Art of Obfuscation
PPTX
C++ programming pattern
PDF
Understanding storage class using nm
PPTX
Lecture 2: arrays and pointers
PDF
Understanding static analysis php amsterdam 2018
PDF
Pattern printing-in-c(Jaydip Kikani)
PDF
Data struture lab
PDF
C programs
PPT
Data structures cs301 power point slides lecture 03
DOCX
FSOFT - Test Java Exam
DOC
Program for hamming code using c
DOCX
PPT
DOCX
Data Structure Project File
DOC
Ds lab manual by s.k.rath
PPT
computer notes - Data Structures - 3
C program
programs
C Code and the Art of Obfuscation
C++ programming pattern
Understanding storage class using nm
Lecture 2: arrays and pointers
Understanding static analysis php amsterdam 2018
Pattern printing-in-c(Jaydip Kikani)
Data struture lab
C programs
Data structures cs301 power point slides lecture 03
FSOFT - Test Java Exam
Program for hamming code using c
Data Structure Project File
Ds lab manual by s.k.rath
computer notes - Data Structures - 3
Ad

Similar to Arrays in c (20)

PPTX
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
PPT
Fp201 unit4
PDF
DSA UNIT II ARRAY AND LIST - notes
PPTX
Module_3_Arrays - Updated.pptx............
PPTX
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
PPT
PPT
2DArrays.ppt
PPT
Array 31.8.2020 updated
PPTX
A Deep Dive into C++ Arrays: Understanding its with Code Examples
PPTX
arrays.pptx
PPT
358 33 powerpoint-slides_5-arrays_chapter-5
PPTX
Yash Bhargava Array In programming in C
PPTX
array ppt of c programing language for exam
PDF
arraysfor engineering students sdf ppt on arrays
PPTX
Array ppt you can learn in very few slides.
PPTX
Arrays in C language
PPTX
arrays.pptx
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
Fp201 unit4
DSA UNIT II ARRAY AND LIST - notes
Module_3_Arrays - Updated.pptx............
C_Arrays(3)bzxhgvxgxg.xhjvxugvxuxuxuxvxugvx.pptx
2DArrays.ppt
Array 31.8.2020 updated
A Deep Dive into C++ Arrays: Understanding its with Code Examples
arrays.pptx
358 33 powerpoint-slides_5-arrays_chapter-5
Yash Bhargava Array In programming in C
array ppt of c programing language for exam
arraysfor engineering students sdf ppt on arrays
Array ppt you can learn in very few slides.
Arrays in C language
arrays.pptx
Ad

More from CGC Technical campus,Mohali (20)

PPTX
Gender Issues CS.pptx
PPTX
Intellectual Property Rights.pptx
PPTX
Cyber Safety ppt.pptx
PPTX
Python Modules .pptx
PPT
Dynamic allocation
PPT
Control statments in c
PPTX
Operators in c by anupam
PPTX
PPT
Fundamentals of-computer
PPT
PPTX
Structure in C language
PPTX
Function in c program
PPTX
PPTX
Data processing and Report writing in Research(Section E)
PPTX
data analysis and report wring in research (Section d)
PPTX
Section C(Analytical and descriptive surveys... )
PPTX
Researchmethodology
Gender Issues CS.pptx
Intellectual Property Rights.pptx
Cyber Safety ppt.pptx
Python Modules .pptx
Dynamic allocation
Control statments in c
Operators in c by anupam
Fundamentals of-computer
Structure in C language
Function in c program
Data processing and Report writing in Research(Section E)
data analysis and report wring in research (Section d)
Section C(Analytical and descriptive surveys... )
Researchmethodology

Recently uploaded (20)

PDF
Pre independence Education in Inndia.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
Cell Types and Its function , kingdom of life
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Complications of Minimal Access Surgery at WLH
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Pre independence Education in Inndia.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Structure & Organelles in detailed.
Cell Types and Its function , kingdom of life
human mycosis Human fungal infections are called human mycosis..pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
TR - Agricultural Crops Production NC III.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
STATICS OF THE RIGID BODIES Hibbelers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Complications of Minimal Access Surgery at WLH
Microbial disease of the cardiovascular and lymphatic systems
O5-L3 Freight Transport Ops (International) V1.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF

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