SlideShare a Scribd company logo
ARRAYS
ARRAYS
• Array
• Consecutive group of memory locations
• Same name and type
• Format: arrayname[ position number ]
• First element at position 0
• n element array c:
c[ 0 ], c[ 1 ]…c[ n - 1 ]
• Array elements are like normal variables
c[ 0 ] = 3;
cout << c[ 0 ];
• Performing operations in subscript. If x = 3,
c[ 5 – 2 ] == c[ 3 ] == c[ x ]
ARRAYS
c[6]
-45
6
0
72
1543
-89
0
62
-3
1
6453
78
Name of array (Note that all
elements of this array have
the same name, c)
c[0]
c[1]
c[2]
c[3]
c[11]
c[10]
c[9]
c[8]
c[7]
c[5]
c[4]
Position number of the element
within array c
DECLARING ARRAYS
• Declaring arrays - specify:
• Name
• Type of array
• Number of elements
• Examples
int c[ 10 ];
float hi[ 3284 ];
• Declaring multiple arrays of same type
• Similar format as other variables
• Example
int b[ 100 ], x[ 27 ];
EXAMPLES USING ARRAYS
• Initializers
int n[ 5 ] = { 1, 2, 3, 4, 5 };
• If not enough initializers, rightmost elements become 0
• If too many initializers, a syntax error is generated
int n[ 5 ] = { 0 }
• Sets all the elements to 0
• If size omitted, the initializers determine it
int n[] = { 1, 2, 3, 4, 5 };
• 5 initializers, therefore n is a 5 element array
EXAMPLES USING ARRAYS
• Strings
• Arrays of characters
• All strings end with null ('0')
• Examples:
char string1[] = "hello";
char string1[] = { 'h', 'e', 'l', 'l', 'o',
'0’ };
• Subscripting is the same as for a normal array
String1[ 0 ] is 'h'
string1[ 2 ] is 'l'
• Side effect: if too much text entered, data written
beyond array
PASSING ARRAYS TO FUNCTIONS
• Specify the name without any brackets
• To pass array myArray declared as
int myArray[ 24 ];
to function myFunction, a function call would
resemble
myFunction( myArray, 24 );
• Array size is usually passed to function
• Arrays passed call-by-reference
• Value of name of array is address of the first element
• Function knows where the array is stored
• Modifies original memory locations
• Individual array elements passed by call-by-value
• pass subscripted name (i.e., myArray[ 3 ]) to function
PASSING ARRAYS TO FUNCTIONS
• Function prototype:
void modifyArray( int b[], int arraySize );
• Parameter names optional in prototype
• int b[] could be simply int []
• int arraysize could be simply int
SORTING ARRAYS
• Sorting data
• Important computing application
• Virtually every organization must sort some data
• Massive amounts must be sorted
• Bubble sort (sinking sort)
• Several passes through the array
• Successive pairs of elements are compared
• If increasing order (or identical), no change
• If decreasing order, elements exchanged
• Repeat these steps for every element
SORTING ARRAYS
• Example:
• Original: 3 4 2 6 7
• Pass 1: 3 2 4 6 7
• Pass 2: 2 3 4 6 7
• Small elements "bubble" to the top
SEARCHING ARRAYS: LINEAR
SEARCH AND BINARY SEARCH
• Search array for a key value
• Linear search
• Compare each element of array with key value
• Useful for small and unsorted arrays
• Binary search
• Can only be used on sorted arrays
• Compares middle element with key
• Very fast; at most n steps, where 2^n > # of elements
• 30 element array takes at most 5 steps
• 2 > 30
MULTIPLE-SUBSCRIPTED ARRAYS
• Multiple subscripts - tables with rows, columns
• Like matrices: specify row, then column.
• Initialize
int b[ 2 ][ 2 ] = { {1, 2}, {3, 4}};
Initializers grouped by row in braces
int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } };
Row 0
Row 1
Row 2
Column 0 Column 1 Column 2 Column 3
a[ 0 ][ 0 ]
a[ 1 ][ 0 ]
a[ 2 ][ 0 ]
a[ 0 ][ 1 ]
a[ 1 ][ 1 ]
a[ 2 ][ 1 ]
a[ 0 ][ 2 ]
a[ 1 ][ 2 ]
a[ 2 ][ 2 ]
a[ 0 ][ 3 ]
a[ 1 ][ 3 ]
a[ 2 ][ 3 ]
Row subscript
Array name
Column subscript
1 2
3 4
1 0
3 4
MULTIPLE-SUBSCRIPTED ARRAYS
• Referenced like normal
cout << b[ 0 ][ 1 ];
• Will output the value of 0
• Cannot reference with commas
cout << b( 0, 1 );
• Will try to call function b, causing a syntax
error
TWO DIMENSIONALARRAY
• The two-dimensional array can be defined as an
array of arrays.
• The 2D array is organized as matrices which can be
represented as the collection of rows and columns.
• It provides ease of holding the bulk of data at once
which can be passed to any number of functions
wherever required.
• The syntax to declare the 2D array is:
data_type array_name[rows][columns];
TWO DIMENSIONALARRAY
int main(){
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
//traversing 2D array
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d n",i,j,arr[i][j]);
}//end of j
}//end of i
return 0;
}
arr[0][0] = 1
arr[0][1] = 2
arr[0][2] = 3
arr[1][0] = 2
arr[1][1] = 3
arr[1][2] = 4
arr[2][0] = 3
arr[2][1] = 4
arr[2][2] = 5
arr[3][0] = 4
arr[3][1] = 5
arr[3][2] = 6
PASSING ARRAY TO FUNCTION
1. return_type function(type arrayname[])
• Declaring blank subscript notation [] is the widely used
technique.
2. return_type function(type arrayname[SIZE])
• Optionally, we can define size in subscript notation [].
3. return_type function(type *arrayname)
• You can also use the concept of a pointer.

More Related Content

Similar to C-Programming Arrays.pptx

Array in Java
Array in JavaArray in Java
Array in Java
Ali shah
 
Array in Java
Array in JavaArray in Java
Array in Java
Ali shah
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
MANEESHValluru
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
MamataAnilgod
 
Arrays
ArraysArrays
Introduction to array and string
Introduction to array and stringIntroduction to array and string
Introduction to array and string
MuntasirMuhit
 
Lecture 2.8 Arrays.pdf
Lecture 2.8 Arrays.pdfLecture 2.8 Arrays.pdf
Lecture 2.8 Arrays.pdf
MianSaeedAkbar1
 
Arrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptxArrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptx
samreenghauri786
 
DataStructures.pptx
DataStructures.pptxDataStructures.pptx
DataStructures.pptx
Thanuj Pothula
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Skillspire LLC
 
Array
ArrayArray
Array
Fahuda E
 
Basics of Python
Basics of PythonBasics of Python
Basics of Python
Ease3
 
Arrays
ArraysArrays
Arrays
swathi reddy
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
Ain-ul-Moiz Khawaja
 
Python Basics it will teach you about data types
Python Basics it will teach you about data typesPython Basics it will teach you about data types
Python Basics it will teach you about data types
NimitSinghal2
 
Python array
Python arrayPython array
Python array
Arnab Chakraborty
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
أحمد محمد
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
أحمد محمد
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
nsitlokeshjain
 

Similar to C-Programming Arrays.pptx (20)

Array in Java
Array in JavaArray in Java
Array in Java
 
Array in Java
Array in JavaArray in Java
Array in Java
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Arrays
ArraysArrays
Arrays
 
Introduction to array and string
Introduction to array and stringIntroduction to array and string
Introduction to array and string
 
Lecture 2.8 Arrays.pdf
Lecture 2.8 Arrays.pdfLecture 2.8 Arrays.pdf
Lecture 2.8 Arrays.pdf
 
Arrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptxArrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptx
 
DataStructures.pptx
DataStructures.pptxDataStructures.pptx
DataStructures.pptx
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Array
ArrayArray
Array
 
Basics of Python
Basics of PythonBasics of Python
Basics of Python
 
Arrays
ArraysArrays
Arrays
 
ARRAYS.pptx
ARRAYS.pptxARRAYS.pptx
ARRAYS.pptx
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Python Basics it will teach you about data types
Python Basics it will teach you about data typesPython Basics it will teach you about data types
Python Basics it will teach you about data types
 
Python array
Python arrayPython array
Python array
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
 

More from SKUP1

serial_busses_i2c.pptx
serial_busses_i2c.pptxserial_busses_i2c.pptx
serial_busses_i2c.pptx
SKUP1
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptx
SKUP1
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
SKUP1
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
SKUP1
 
C-Programming File-handling-C.pptx
C-Programming  File-handling-C.pptxC-Programming  File-handling-C.pptx
C-Programming File-handling-C.pptx
SKUP1
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptx
SKUP1
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
SKUP1
 
Finite State Machine.ppt.pptx
Finite State Machine.ppt.pptxFinite State Machine.ppt.pptx
Finite State Machine.ppt.pptx
SKUP1
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
SKUP1
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptx
SKUP1
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
SKUP1
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
SKUP1
 
cprogramming Structures.pptx
cprogramming Structures.pptxcprogramming Structures.pptx
cprogramming Structures.pptx
SKUP1
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptx
SKUP1
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
SKUP1
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
SKUP1
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
SKUP1
 
C MEMORY MODEL​.pptx
C MEMORY MODEL​.pptxC MEMORY MODEL​.pptx
C MEMORY MODEL​.pptx
SKUP1
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxDATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
SKUP1
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
SKUP1
 

More from SKUP1 (20)

serial_busses_i2c.pptx
serial_busses_i2c.pptxserial_busses_i2c.pptx
serial_busses_i2c.pptx
 
DESIGN PATTERN.pptx
DESIGN PATTERN.pptxDESIGN PATTERN.pptx
DESIGN PATTERN.pptx
 
INTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptxINTER PROCESS COMMUNICATION (IPC).pptx
INTER PROCESS COMMUNICATION (IPC).pptx
 
DATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptxDATA STRUCTURES AND LINKED LISTS IN C.pptx
DATA STRUCTURES AND LINKED LISTS IN C.pptx
 
C-Programming File-handling-C.pptx
C-Programming  File-handling-C.pptxC-Programming  File-handling-C.pptx
C-Programming File-handling-C.pptx
 
Processes, Threads.pptx
Processes, Threads.pptxProcesses, Threads.pptx
Processes, Threads.pptx
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 
Finite State Machine.ppt.pptx
Finite State Machine.ppt.pptxFinite State Machine.ppt.pptx
Finite State Machine.ppt.pptx
 
FUNCTIONS IN C.pptx
FUNCTIONS IN C.pptxFUNCTIONS IN C.pptx
FUNCTIONS IN C.pptx
 
cprogramming strings.pptx
cprogramming strings.pptxcprogramming strings.pptx
cprogramming strings.pptx
 
UNIONS IN C.pptx
UNIONS IN C.pptxUNIONS IN C.pptx
UNIONS IN C.pptx
 
OPERATORS IN C.pptx
OPERATORS IN C.pptxOPERATORS IN C.pptx
OPERATORS IN C.pptx
 
cprogramming Structures.pptx
cprogramming Structures.pptxcprogramming Structures.pptx
cprogramming Structures.pptx
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptx
 
POINTERS.pptx
POINTERS.pptxPOINTERS.pptx
POINTERS.pptx
 
STACKS AND QUEUES.pptx
STACKS AND QUEUES.pptxSTACKS AND QUEUES.pptx
STACKS AND QUEUES.pptx
 
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptxC-Programming  C LIBRARIES AND USER DEFINED LIBRARIES.pptx
C-Programming C LIBRARIES AND USER DEFINED LIBRARIES.pptx
 
C MEMORY MODEL​.pptx
C MEMORY MODEL​.pptxC MEMORY MODEL​.pptx
C MEMORY MODEL​.pptx
 
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptxDATATYPES,KEYWORDS,FORMATSPECS[1].pptx
DATATYPES,KEYWORDS,FORMATSPECS[1].pptx
 
DYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptxDYNAMIC MEMORY ALLOCATION.pptx
DYNAMIC MEMORY ALLOCATION.pptx
 

Recently uploaded

一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
nedcocy
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
upoux
 
Butterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdfButterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdf
Lubi Valves
 
Presentation on Food Delivery Systems
Presentation on Food Delivery SystemsPresentation on Food Delivery Systems
Presentation on Food Delivery Systems
Abdullah Al Noman
 
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
OKORIE1
 
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUESAN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
drshikhapandey2022
 
Sri Guru Hargobind Ji - Bandi Chor Guru.pdf
Sri Guru Hargobind Ji - Bandi Chor Guru.pdfSri Guru Hargobind Ji - Bandi Chor Guru.pdf
Sri Guru Hargobind Ji - Bandi Chor Guru.pdf
Balvir Singh
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
Divyanshu
 
EV BMS WITH CHARGE MONITOR AND FIRE DETECTION.pptx
EV BMS WITH CHARGE MONITOR AND FIRE DETECTION.pptxEV BMS WITH CHARGE MONITOR AND FIRE DETECTION.pptx
EV BMS WITH CHARGE MONITOR AND FIRE DETECTION.pptx
nikshimanasa
 
SELENIUM CONF -PALLAVI SHARMA - 2024.pdf
SELENIUM CONF -PALLAVI SHARMA - 2024.pdfSELENIUM CONF -PALLAVI SHARMA - 2024.pdf
SELENIUM CONF -PALLAVI SHARMA - 2024.pdf
Pallavi Sharma
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
q30122000
 
Ericsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.pptEricsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.ppt
wafawafa52
 
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdfAsymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
felixwold
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
PriyankaKilaniya
 
Call Girls Chennai +91-8824825030 Vip Call Girls Chennai
Call Girls Chennai +91-8824825030 Vip Call Girls ChennaiCall Girls Chennai +91-8824825030 Vip Call Girls Chennai
Call Girls Chennai +91-8824825030 Vip Call Girls Chennai
paraasingh12 #V08
 
Accident detection system project report.pdf
Accident detection system project report.pdfAccident detection system project report.pdf
Accident detection system project report.pdf
Kamal Acharya
 
Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...
pvpriya2
 
Levelised Cost of Hydrogen (LCOH) Calculator Manual
Levelised Cost of Hydrogen  (LCOH) Calculator ManualLevelised Cost of Hydrogen  (LCOH) Calculator Manual
Levelised Cost of Hydrogen (LCOH) Calculator Manual
Massimo Talia
 
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
sydezfe
 
Impartiality as per ISO /IEC 17025:2017 Standard
Impartiality as per ISO /IEC 17025:2017 StandardImpartiality as per ISO /IEC 17025:2017 Standard
Impartiality as per ISO /IEC 17025:2017 Standard
MuhammadJazib15
 

Recently uploaded (20)

一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
一比一原版(uofo毕业证书)美国俄勒冈大学毕业证如何办理
 
Butterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdfButterfly Valves Manufacturer (LBF Series).pdf
Butterfly Valves Manufacturer (LBF Series).pdf
 
Presentation on Food Delivery Systems
Presentation on Food Delivery SystemsPresentation on Food Delivery Systems
Presentation on Food Delivery Systems
 
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
DESIGN AND MANUFACTURE OF CEILING BOARD USING SAWDUST AND WASTE CARTON MATERI...
 
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUESAN INTRODUCTION OF AI & SEARCHING TECHIQUES
AN INTRODUCTION OF AI & SEARCHING TECHIQUES
 
Sri Guru Hargobind Ji - Bandi Chor Guru.pdf
Sri Guru Hargobind Ji - Bandi Chor Guru.pdfSri Guru Hargobind Ji - Bandi Chor Guru.pdf
Sri Guru Hargobind Ji - Bandi Chor Guru.pdf
 
Null Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAMNull Bangalore | Pentesters Approach to AWS IAM
Null Bangalore | Pentesters Approach to AWS IAM
 
EV BMS WITH CHARGE MONITOR AND FIRE DETECTION.pptx
EV BMS WITH CHARGE MONITOR AND FIRE DETECTION.pptxEV BMS WITH CHARGE MONITOR AND FIRE DETECTION.pptx
EV BMS WITH CHARGE MONITOR AND FIRE DETECTION.pptx
 
SELENIUM CONF -PALLAVI SHARMA - 2024.pdf
SELENIUM CONF -PALLAVI SHARMA - 2024.pdfSELENIUM CONF -PALLAVI SHARMA - 2024.pdf
SELENIUM CONF -PALLAVI SHARMA - 2024.pdf
 
Height and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdfHeight and depth gauge linear metrology.pdf
Height and depth gauge linear metrology.pdf
 
Ericsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.pptEricsson LTE Throughput Troubleshooting Techniques.ppt
Ericsson LTE Throughput Troubleshooting Techniques.ppt
 
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdfAsymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
Asymmetrical Repulsion Magnet Motor Ratio 6-7.pdf
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
 
Call Girls Chennai +91-8824825030 Vip Call Girls Chennai
Call Girls Chennai +91-8824825030 Vip Call Girls ChennaiCall Girls Chennai +91-8824825030 Vip Call Girls Chennai
Call Girls Chennai +91-8824825030 Vip Call Girls Chennai
 
Accident detection system project report.pdf
Accident detection system project report.pdfAccident detection system project report.pdf
Accident detection system project report.pdf
 
Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...Determination of Equivalent Circuit parameters and performance characteristic...
Determination of Equivalent Circuit parameters and performance characteristic...
 
Levelised Cost of Hydrogen (LCOH) Calculator Manual
Levelised Cost of Hydrogen  (LCOH) Calculator ManualLevelised Cost of Hydrogen  (LCOH) Calculator Manual
Levelised Cost of Hydrogen (LCOH) Calculator Manual
 
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
一比一原版(uoft毕业证书)加拿大多伦多大学毕业证如何办理
 
Impartiality as per ISO /IEC 17025:2017 Standard
Impartiality as per ISO /IEC 17025:2017 StandardImpartiality as per ISO /IEC 17025:2017 Standard
Impartiality as per ISO /IEC 17025:2017 Standard
 

C-Programming Arrays.pptx

  • 2. ARRAYS • Array • Consecutive group of memory locations • Same name and type • Format: arrayname[ position number ] • First element at position 0 • n element array c: c[ 0 ], c[ 1 ]…c[ n - 1 ] • Array elements are like normal variables c[ 0 ] = 3; cout << c[ 0 ]; • Performing operations in subscript. If x = 3, c[ 5 – 2 ] == c[ 3 ] == c[ x ]
  • 3. ARRAYS c[6] -45 6 0 72 1543 -89 0 62 -3 1 6453 78 Name of array (Note that all elements of this array have the same name, c) c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] Position number of the element within array c
  • 4. DECLARING ARRAYS • Declaring arrays - specify: • Name • Type of array • Number of elements • Examples int c[ 10 ]; float hi[ 3284 ]; • Declaring multiple arrays of same type • Similar format as other variables • Example int b[ 100 ], x[ 27 ];
  • 5. EXAMPLES USING ARRAYS • Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; • If not enough initializers, rightmost elements become 0 • If too many initializers, a syntax error is generated int n[ 5 ] = { 0 } • Sets all the elements to 0 • If size omitted, the initializers determine it int n[] = { 1, 2, 3, 4, 5 }; • 5 initializers, therefore n is a 5 element array
  • 6. EXAMPLES USING ARRAYS • Strings • Arrays of characters • All strings end with null ('0') • Examples: char string1[] = "hello"; char string1[] = { 'h', 'e', 'l', 'l', 'o', '0’ }; • Subscripting is the same as for a normal array String1[ 0 ] is 'h' string1[ 2 ] is 'l' • Side effect: if too much text entered, data written beyond array
  • 7. PASSING ARRAYS TO FUNCTIONS • Specify the name without any brackets • To pass array myArray declared as int myArray[ 24 ]; to function myFunction, a function call would resemble myFunction( myArray, 24 ); • Array size is usually passed to function • Arrays passed call-by-reference • Value of name of array is address of the first element • Function knows where the array is stored • Modifies original memory locations • Individual array elements passed by call-by-value • pass subscripted name (i.e., myArray[ 3 ]) to function
  • 8. PASSING ARRAYS TO FUNCTIONS • Function prototype: void modifyArray( int b[], int arraySize ); • Parameter names optional in prototype • int b[] could be simply int [] • int arraysize could be simply int
  • 9. SORTING ARRAYS • Sorting data • Important computing application • Virtually every organization must sort some data • Massive amounts must be sorted • Bubble sort (sinking sort) • Several passes through the array • Successive pairs of elements are compared • If increasing order (or identical), no change • If decreasing order, elements exchanged • Repeat these steps for every element
  • 10. SORTING ARRAYS • Example: • Original: 3 4 2 6 7 • Pass 1: 3 2 4 6 7 • Pass 2: 2 3 4 6 7 • Small elements "bubble" to the top
  • 11. SEARCHING ARRAYS: LINEAR SEARCH AND BINARY SEARCH • Search array for a key value • Linear search • Compare each element of array with key value • Useful for small and unsorted arrays • Binary search • Can only be used on sorted arrays • Compares middle element with key • Very fast; at most n steps, where 2^n > # of elements • 30 element array takes at most 5 steps • 2 > 30
  • 12. MULTIPLE-SUBSCRIPTED ARRAYS • Multiple subscripts - tables with rows, columns • Like matrices: specify row, then column. • Initialize int b[ 2 ][ 2 ] = { {1, 2}, {3, 4}}; Initializers grouped by row in braces int b[ 2 ][ 2 ] = { { 1 }, { 3, 4 } }; Row 0 Row 1 Row 2 Column 0 Column 1 Column 2 Column 3 a[ 0 ][ 0 ] a[ 1 ][ 0 ] a[ 2 ][ 0 ] a[ 0 ][ 1 ] a[ 1 ][ 1 ] a[ 2 ][ 1 ] a[ 0 ][ 2 ] a[ 1 ][ 2 ] a[ 2 ][ 2 ] a[ 0 ][ 3 ] a[ 1 ][ 3 ] a[ 2 ][ 3 ] Row subscript Array name Column subscript 1 2 3 4 1 0 3 4
  • 13. MULTIPLE-SUBSCRIPTED ARRAYS • Referenced like normal cout << b[ 0 ][ 1 ]; • Will output the value of 0 • Cannot reference with commas cout << b( 0, 1 ); • Will try to call function b, causing a syntax error
  • 14. TWO DIMENSIONALARRAY • The two-dimensional array can be defined as an array of arrays. • The 2D array is organized as matrices which can be represented as the collection of rows and columns. • It provides ease of holding the bulk of data at once which can be passed to any number of functions wherever required. • The syntax to declare the 2D array is: data_type array_name[rows][columns];
  • 15. TWO DIMENSIONALARRAY int main(){ int i=0,j=0; int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}}; //traversing 2D array for(i=0;i<4;i++){ for(j=0;j<3;j++){ printf("arr[%d] [%d] = %d n",i,j,arr[i][j]); }//end of j }//end of i return 0; } arr[0][0] = 1 arr[0][1] = 2 arr[0][2] = 3 arr[1][0] = 2 arr[1][1] = 3 arr[1][2] = 4 arr[2][0] = 3 arr[2][1] = 4 arr[2][2] = 5 arr[3][0] = 4 arr[3][1] = 5 arr[3][2] = 6
  • 16. PASSING ARRAY TO FUNCTION 1. return_type function(type arrayname[]) • Declaring blank subscript notation [] is the widely used technique. 2. return_type function(type arrayname[SIZE]) • Optionally, we can define size in subscript notation []. 3. return_type function(type *arrayname) • You can also use the concept of a pointer.