SlideShare a Scribd company logo
1 of 19
Download to read offline
ARRAYS IN C
Dr. M.A. Jawale
Associate Professor
Department of Information Technology
Sanjivani College of Engineering, Kopargaon
ARRAY
 The C language provides a capability that
enables the user to design a set of similar data
types, called array.
 An array is a collection of similar elements.
 These similar elements could be all ints, or all
floats, or all chars, etc.
 Usually, the array of characters is called a
‘string’,
 whereas an array of ints or floats is called
simply an array.
 All elements of any given array must be of the
same type. i.e. we cannot have an array of 10
numbers, of which 5 are ints and 5 are floats.
2
Dr.M.A.JawaleITDept.
ARRAY DECLARATION
 int marks[30] ;
 Here, int specifies the type of the variable, and
the word marks specifies the name of the
variable.
 The [30] tells how many elements of the type int
will be in our array.
 This number is often called the ‘dimension’ of the
array.
 The bracket [ ] tells the compiler that we are
dealing with an array.
3
Dr.M.A.JawaleITDept.
 An array is a collection of similar elements.
 The first element in the array is numbered 0, so
the last element is 1 less than the size of the
array.
 An array is also known as a subscripted variable.
 Before using an array its type and dimension
must be declared.
 However big, an array its elements are always
stored in contiguous memory locations.
4
Dr.M.A.JawaleITDept.
A SIMPLE PROGRAM USING ARRAY
Let us try to write a program to find average marks obtained by
a class of 30 students in a test.
main( )
{
int avg, sum = 0 ;
int i ;
int marks[30] ; /* array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ; /* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
{
sum = sum + marks[i] ; /* read data from an
} array*/
avg = sum / 30 ;
printf ( "n Average marks = %d", avg ) ;
}
5
Dr.M.A.JawaleITDept.
Entering Data into an Array
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ;
}
6
Dr.M.A.JawaleITDept.
ARRAY INITIALISATION
 We can initialize an array while declaring it.
 Following are a few examples that demonstrate
this.
int num[6] = { 2, 4, 12, 5, 45, 5 } ;
int n[ ] = { 2, 4, 12, 5, 45, 5 } ;
float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ;
 Note the following points carefully:
(a) Till the array elements are not given any
specific values, they are supposed to contain
garbage values.
(b) If the array is initialised where it is declared,
mentioning the dimension of the array is optional
as in the 2nd example above. 7
Dr.M.A.JawaleITDept.
ARRAY ELEMENTS IN MEMORY
 Consider the following array declaration:
int arr[8] ;
 What happens in memory when we make this declaration?
 16 bytes get immediately reserved in memory, 2 bytes each
for the 8 integers.
 And since the array is not initialized, all eight values
present in it would be garbage values.
 This so happens because the storage class of this array is
assumed to be auto.
 If the storage class is declared to be static then all the
array elements would have a default initial value as
zero.
 Whatever be the initial values, all the array elements
would always be present in contiguous memory locations.
8
Dr.M.A.JawaleITDept.
ARRANGEMENT OF ARRAY ELEMENTS IN
MEMORY
9
Dr.M.A.JawaleITDept.
TWO DIMENSIONAL ARRAYS
 It is also possible for arrays to have two or more dimensions.
 The two dimensional array is also called a matrix.
 Here is a sample program that stores roll number and marks
obtained by a student side by side in a matrix.
main( )
{
int stud[4][2] ;
int i, j ;
for ( i = 0 ; i <= 3 ; i++ )
{
printf ( "n Enter roll no. and marks" ) ;
scanf ( "%d %d", &stud[i][0], &stud[i][1] ) ;
}
for ( i = 0 ; i <= 3 ; i++ )
printf ( "n %d %d", stud[i][0], stud[i][1] ) ;
}
10
Dr.M.A.JawaleITDept.
 Look at the scanf( ) statement used in the first
for loop:
scanf ( "%d %d", &stud[i][0], &stud[i][1] ) ;
 In stud[i][0] and stud[i][1] the first subscript of
the variable stud, is row number which changes
for every student.
 The second subscript tells which of the two
columns are we talking about
 the zeroth column which contains the roll no.
 or the first column which contains the marks.
 Remember the counting of rows and
columns begin with zero.
11
Dr.M.A.JawaleITDept.
THE COMPLETE ARRAY ARRANGEMENT IS
SHOWN BELOW.
 Thus, 1234 is stored in stud[0][0], 56 is stored in
stud[0][1] and so on.
12
Dr.M.A.JawaleITDept.
INITIALISING A 2-DIMENSIONAL ARRAY
 int stud[4][2] = {
{ 1234, 56 },
{ 1212, 33 },
{ 1434, 80 },
{ 1312, 78 }
} ;
 or even this would work...
int stud[4][2] = { 1234, 56, 1212, 33, 1434, 80, 1312, 78 } ;
13
Dr.M.A.JawaleITDept.
 It is important to remember that while initializing a
2-D array it is necessary to mention the second
(column) dimension, whereas the first dimension
(row) is optional.
 Thus the declarations,
int arr[2][3] = { 12, 34, 23, 45, 56, 45 } ;
int arr[ ][3] = { 12, 34, 23, 45, 56, 45 } ;
are perfectly acceptable,
 whereas,
int arr[2][ ] = { 12, 34, 23, 45, 56, 45 } ;
int arr[ ][ ] = { 12, 34, 23, 45, 56, 45 } ;
would never work. 14
Dr.M.A.JawaleITDept.
MEMORY MAP OF A 2-DIMENSIONAL ARRAY
 Memory doesn’t contain rows and columns.
 In memory whether it is a one-dimensional or a
two-dimensional array the array elements are
stored in one continuous chain.
 The arrangement of array elements of a two-
dimensional array in memory is shown below:
15
Dr.M.A.JawaleITDept.
THREE-DIMENSIONAL ARRAY
16
Dr.M.A.JawaleITDept.
 A three-dimensional array can be thought of as an array of
arrays of arrays.
 The outer array has three elements, each of which is a two-
dimensional array of four one-dimensional arrays, each of
which contains two integers.
 In other words, a one-dimensional array of two elements is
constructed first.
 Then four such one dimensional arrays are placed one
below the other to give a two dimensional array containing
four rows.
 Then, three such two dimensional arrays are placed one
behind the other to yield a three dimensional array
containing three 2-dimensional arrays.
 In the array declaration note how the commas have been
given
17
Dr.M.A.JawaleITDept.
 In memory the same array elements are stored
linearly as shown
18
Dr.M.A.JawaleITDept.
SUMMARY
 An array is similar to an ordinary variable except
that it can store multiple elements of similar
type.
 Compiler doesn’t perform bounds checking on an
array.
 The array variable acts as a pointer to the zeroth
element of the array.
 On incrementing a pointer it points to the next
location of its type.
 Array elements are stored in contiguous memory
locations.
19
Dr.M.A.JawaleITDept.

More Related Content

What's hot

What's hot (20)

Array
ArrayArray
Array
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
arrays in c
arrays in carrays in c
arrays in c
 
Chap09
Chap09Chap09
Chap09
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Preparation Data Structures 03 abstract data_types
Preparation Data Structures 03 abstract data_typesPreparation Data Structures 03 abstract data_types
Preparation Data Structures 03 abstract data_types
 
Arrays in c language
Arrays in c languageArrays in c language
Arrays in c language
 
One dimensional 2
One dimensional 2One dimensional 2
One dimensional 2
 
Arrays
ArraysArrays
Arrays
 
Array
ArrayArray
Array
 
Array Presentation
Array PresentationArray Presentation
Array Presentation
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Data Types and Structures in R
Data Types and Structures in RData Types and Structures in R
Data Types and Structures in R
 
Array in c++
Array in c++Array in c++
Array in c++
 
Array in C
Array in CArray in C
Array in C
 
A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)A Presentation About Array Manipulation(Insertion & Deletion in an array)
A Presentation About Array Manipulation(Insertion & Deletion in an array)
 
Array and Collections in c#
Array and Collections in c#Array and Collections in c#
Array and Collections in c#
 
intorduction to Arrays in java
intorduction to Arrays in javaintorduction to Arrays in java
intorduction to Arrays in java
 
ARRAY
ARRAYARRAY
ARRAY
 

Similar to Arrays

3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptxFolkAdonis
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programmingnmahi96
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Kumar Boro
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm KristinaBorooah
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsKuntal Bhowmick
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functionsSwarup Boro
 
Array assignment
Array assignmentArray assignment
Array assignmentAhmad Kamal
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfaroraopticals15
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptxSajalFayyaz
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfajajkhan16
 

Similar to Arrays (20)

3.ArraysandPointers.pptx
3.ArraysandPointers.pptx3.ArraysandPointers.pptx
3.ArraysandPointers.pptx
 
arrays.docx
arrays.docxarrays.docx
arrays.docx
 
Arrays-Computer programming
Arrays-Computer programmingArrays-Computer programming
Arrays-Computer programming
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Arrays
ArraysArrays
Arrays
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
Arrays
ArraysArrays
Arrays
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Arrays
ArraysArrays
Arrays
 
Array assignment
Array assignmentArray assignment
Array assignment
 
Homework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdfHomework Assignment – Array Technical DocumentWrite a technical .pdf
Homework Assignment – Array Technical DocumentWrite a technical .pdf
 
Arrays
ArraysArrays
Arrays
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
Array&amp;string
Array&amp;stringArray&amp;string
Array&amp;string
 
Data structure.pptx
Data structure.pptxData structure.pptx
Data structure.pptx
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
 

Recently uploaded

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 

Recently uploaded (20)

Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 

Arrays

  • 1. ARRAYS IN C Dr. M.A. Jawale Associate Professor Department of Information Technology Sanjivani College of Engineering, Kopargaon
  • 2. ARRAY  The C language provides a capability that enables the user to design a set of similar data types, called array.  An array is a collection of similar elements.  These similar elements could be all ints, or all floats, or all chars, etc.  Usually, the array of characters is called a ‘string’,  whereas an array of ints or floats is called simply an array.  All elements of any given array must be of the same type. i.e. we cannot have an array of 10 numbers, of which 5 are ints and 5 are floats. 2 Dr.M.A.JawaleITDept.
  • 3. ARRAY DECLARATION  int marks[30] ;  Here, int specifies the type of the variable, and the word marks specifies the name of the variable.  The [30] tells how many elements of the type int will be in our array.  This number is often called the ‘dimension’ of the array.  The bracket [ ] tells the compiler that we are dealing with an array. 3 Dr.M.A.JawaleITDept.
  • 4.  An array is a collection of similar elements.  The first element in the array is numbered 0, so the last element is 1 less than the size of the array.  An array is also known as a subscripted variable.  Before using an array its type and dimension must be declared.  However big, an array its elements are always stored in contiguous memory locations. 4 Dr.M.A.JawaleITDept.
  • 5. A SIMPLE PROGRAM USING ARRAY Let us try to write a program to find average marks obtained by a class of 30 students in a test. main( ) { int avg, sum = 0 ; int i ; int marks[30] ; /* array declaration */ for ( i = 0 ; i <= 29 ; i++ ) { printf ( "nEnter marks " ) ; scanf ( "%d", &marks[i] ) ; /* store data in array */ } for ( i = 0 ; i <= 29 ; i++ ) { sum = sum + marks[i] ; /* read data from an } array*/ avg = sum / 30 ; printf ( "n Average marks = %d", avg ) ; } 5 Dr.M.A.JawaleITDept.
  • 6. Entering Data into an Array for ( i = 0 ; i <= 29 ; i++ ) { printf ( "nEnter marks " ) ; scanf ( "%d", &marks[i] ) ; } 6 Dr.M.A.JawaleITDept.
  • 7. ARRAY INITIALISATION  We can initialize an array while declaring it.  Following are a few examples that demonstrate this. int num[6] = { 2, 4, 12, 5, 45, 5 } ; int n[ ] = { 2, 4, 12, 5, 45, 5 } ; float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ;  Note the following points carefully: (a) Till the array elements are not given any specific values, they are supposed to contain garbage values. (b) If the array is initialised where it is declared, mentioning the dimension of the array is optional as in the 2nd example above. 7 Dr.M.A.JawaleITDept.
  • 8. ARRAY ELEMENTS IN MEMORY  Consider the following array declaration: int arr[8] ;  What happens in memory when we make this declaration?  16 bytes get immediately reserved in memory, 2 bytes each for the 8 integers.  And since the array is not initialized, all eight values present in it would be garbage values.  This so happens because the storage class of this array is assumed to be auto.  If the storage class is declared to be static then all the array elements would have a default initial value as zero.  Whatever be the initial values, all the array elements would always be present in contiguous memory locations. 8 Dr.M.A.JawaleITDept.
  • 9. ARRANGEMENT OF ARRAY ELEMENTS IN MEMORY 9 Dr.M.A.JawaleITDept.
  • 10. TWO DIMENSIONAL ARRAYS  It is also possible for arrays to have two or more dimensions.  The two dimensional array is also called a matrix.  Here is a sample program that stores roll number and marks obtained by a student side by side in a matrix. main( ) { int stud[4][2] ; int i, j ; for ( i = 0 ; i <= 3 ; i++ ) { printf ( "n Enter roll no. and marks" ) ; scanf ( "%d %d", &stud[i][0], &stud[i][1] ) ; } for ( i = 0 ; i <= 3 ; i++ ) printf ( "n %d %d", stud[i][0], stud[i][1] ) ; } 10 Dr.M.A.JawaleITDept.
  • 11.  Look at the scanf( ) statement used in the first for loop: scanf ( "%d %d", &stud[i][0], &stud[i][1] ) ;  In stud[i][0] and stud[i][1] the first subscript of the variable stud, is row number which changes for every student.  The second subscript tells which of the two columns are we talking about  the zeroth column which contains the roll no.  or the first column which contains the marks.  Remember the counting of rows and columns begin with zero. 11 Dr.M.A.JawaleITDept.
  • 12. THE COMPLETE ARRAY ARRANGEMENT IS SHOWN BELOW.  Thus, 1234 is stored in stud[0][0], 56 is stored in stud[0][1] and so on. 12 Dr.M.A.JawaleITDept.
  • 13. INITIALISING A 2-DIMENSIONAL ARRAY  int stud[4][2] = { { 1234, 56 }, { 1212, 33 }, { 1434, 80 }, { 1312, 78 } } ;  or even this would work... int stud[4][2] = { 1234, 56, 1212, 33, 1434, 80, 1312, 78 } ; 13 Dr.M.A.JawaleITDept.
  • 14.  It is important to remember that while initializing a 2-D array it is necessary to mention the second (column) dimension, whereas the first dimension (row) is optional.  Thus the declarations, int arr[2][3] = { 12, 34, 23, 45, 56, 45 } ; int arr[ ][3] = { 12, 34, 23, 45, 56, 45 } ; are perfectly acceptable,  whereas, int arr[2][ ] = { 12, 34, 23, 45, 56, 45 } ; int arr[ ][ ] = { 12, 34, 23, 45, 56, 45 } ; would never work. 14 Dr.M.A.JawaleITDept.
  • 15. MEMORY MAP OF A 2-DIMENSIONAL ARRAY  Memory doesn’t contain rows and columns.  In memory whether it is a one-dimensional or a two-dimensional array the array elements are stored in one continuous chain.  The arrangement of array elements of a two- dimensional array in memory is shown below: 15 Dr.M.A.JawaleITDept.
  • 17.  A three-dimensional array can be thought of as an array of arrays of arrays.  The outer array has three elements, each of which is a two- dimensional array of four one-dimensional arrays, each of which contains two integers.  In other words, a one-dimensional array of two elements is constructed first.  Then four such one dimensional arrays are placed one below the other to give a two dimensional array containing four rows.  Then, three such two dimensional arrays are placed one behind the other to yield a three dimensional array containing three 2-dimensional arrays.  In the array declaration note how the commas have been given 17 Dr.M.A.JawaleITDept.
  • 18.  In memory the same array elements are stored linearly as shown 18 Dr.M.A.JawaleITDept.
  • 19. SUMMARY  An array is similar to an ordinary variable except that it can store multiple elements of similar type.  Compiler doesn’t perform bounds checking on an array.  The array variable acts as a pointer to the zeroth element of the array.  On incrementing a pointer it points to the next location of its type.  Array elements are stored in contiguous memory locations. 19 Dr.M.A.JawaleITDept.