SlideShare a Scribd company logo
©LPU CSE101 C Programming
CSE101-Lec#17
• Arrays
• (Arrays and Functions)
©LPU CSE101 C Programming
Outline
• To declare an array
• To initialize an array
• To pass an array to a function
©LPU CSE101 C Programming
Introduction
• Arrays
– Collection of related data items of same data
type.
– Static entity – i.e. they remain the same size
throughout program execution
©LPU CSE101 C Programming
Arrays
• Array
– Group of consecutive memory locations
– Same name and data type
• To refer to an element, specify:
– Array name
– Position number in square brackets([])
• Format:
arrayname[position_number]
– First element is always at position 0
– Eg. n element array named c:
• c[0], c[1]...c[n – 1]
Name of array (Note
that all elements of
this array have the
same name, c)
Position number of
the element within
array c
3
c[6]
-45
6
0
72
-89
0
62
-3
1
6453
78
c[0]
c[1]
c[2]
c[3]
c[11]
c[10]
c[9]
c[8]
c[7]
c[5]
c[4]
©LPU CSE101 C Programming
Arrays
• An array is an ordered list of values
c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9]
79 87 94 82 67 98 87 81 74 91
An array of size N is indexed from zero to N-1
c
The entire array
has a single name
Each value has a numeric index
This array holds 10 values that are indexed from 0 to 9
©LPU CSE101 C Programming
Arrays
• Array elements are like normal variables
c[0] = 3;/*stores 3 to c[0] element*/
scanf (“%d”, &c[1]);/*reads c[1] element*/
printf (“%d, %d”, c[0], c[1]); /*displays
c[0] & c[1] element*/
• The position number inside square brackets is called
subscript/index.
• Subscript must be integer or an integer expression
c[5 - 2] = 7; (i.e. c[3] = 7)
©LPU CSE101 C Programming
Defining Arrays
• When defining arrays, specify:
– Name
– Data Type of array
– Number of elements
datatype arrayName[numberOfElements];
– Examples:
int students[10];
float myArray[3284];
• Defining multiple arrays of same data type
– Format is similar to regular variables
– Example:
int b[100], x[27];
©LPU CSE101 C Programming
Initializing Arrays
• Initializers
int n[5] = { 1, 2, 3, 4, 5 };
– If not enough initializers given, then rightmost
elements become 0
– int n[5] = { 0 }; // initialize all elements to 0
– C arrays have no bounds checking.
• If size is omitted, initializers determine it
int n[] = { 1, 2, 3, 4, 5 };
– 5 initializers, therefore 5 element array.
©LPU CSE101 C Programming
Initializing Arrays
• Array is same as the variable can prompt for
value from the user at run time.
• Array is a group of elements so we use for
loop to get the values of every element
instead of getting single value at a time.
• Example: int array[5], i; // array of size 5
for(i=0;i<5;i++){// loop begins from 0 to 4
scanf(“%d”, &array[i]);
}
©LPU CSE101 C Programming
Program of
Initializing an
array to zero
using loop.
©LPU CSE101 C Programming
Element Value
0 0
1 0
2 0
3 0
4 0
5 0
6 0
7 0
8 0
9 0
0
n[6]
0
0
0
0
0
0
0
0
n[0]
n[1]
n[2]
n[3]
n[9]
n[8]
n[7]
n[5]
n[4]
0
©LPU CSE101 C Programming
Program of
Initializing an
array element
with
calculations
using loop.
©LPU CSE101 C Programming
Element Value
0 2
1 4
2 6
3 8
4 10
5 12
6 14
7 16
8 18
9 20
10
n[6]
4
6
8
12
14
16
18
20
n[0]
n[1]
n[2]
n[3]
n[9]
n[8]
n[7]
n[5]
n[4]
2
©LPU CSE101 C Programming
Total of array element values is 383
Program to
compute
sum of
elements of
array
©LPU CSE101 C Programming
array = 0012FF78
&array[0] = 0012FF78
&array = 0012FF78
#include <stdio.h>
/* function main begins program execution */
int main()
{
char array[ 5 ]; /* define an array of size 5 */
printf( " array = %pn&array[0] = %pn"
" &array = %pn",
array, &array[ 0 ], &array );
return 0; /* indicates successful termination */
} /* end main */
Program to
explain the
address of
array
©LPU CSE101 C Programming
Character Arrays
• Character arrays
– Character arrays can be initialized using string literals
char string1[] = "first";
– It is equivalent to
char string1[] = { 'f', 'i', 'r', 's', 't', '0' };
• Null character '0' terminates strings
• string1 actually has 6 elements
– Can access individual characters
string1[ 3 ] is character ‘s’
– Array name is address of array, so & not needed for scanf
scanf( "%s", string2 );
• Reads characters until whitespace encountered
©LPU CSE101 C Programming
,
Program to
print character
array as
strings.
©LPU CSE101 C Programming
Enter a string: Hello
string1 is: Hello
string2 is: string literal
©LPU CSE101 C Programming
Passing Arrays to Function
• Arrays can be passed to functions in two ways:
1. Pass entire array
2. Pass array element by element
©LPU CSE101 C Programming
Pass entire array
• Here entire array can be passed as an argument
to the function
• Function gets complete access to the original
array
• While passing entire array Address of first
element is passed to function, any changes made
inside function, directly affects the Original
value.
void modifyArray(int b[], int arraySize);
• Function passing method: “ Pass by Address”
©LPU CSE101 C Programming
Pass array element by element
• Here individual elements are passed to the
function as argument
• Duplicate carbon copy of Original variable is
passed to function
• So any changes made inside function does not
affects the original value
• Function doesn’t get complete access to the
original array element.
void modifyElement(int e);
• Function passing method: “ Pass by Value”
©LPU CSE101 C Programming
Passing Arrays to Functions
• Function prototype
void modifyArray(int b[], int arraySize);
– Parameter names optional in prototype
• int b[] could be written int []
• int arraySize could be simply int
void modifyArray(int [], int);
• Function call
int a[SIZE];
modifyArray(a, SIZE);
©LPU CSE101 C Programming
Passing arrays
and individual
array elements
to functions
©LPU CSE101 C Programming
©LPU CSE101 C Programming
©LPU CSE101 C Programming
Effects of passing entire array by reference:
The values of the original array are:
0 1 2 3 4
The values of the modified array are:
0 2 4 6 8
Effects of passing array element by value:
The value of a[3] is 6
Value in modifyElement is 12
The value of a[3] is 6
©LPU CSE101 C Programming
cse101@lpu.co.in
Next Class:
Applications of Arrays

More Related Content

What's hot

queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
somendra kumar
 
Team 6
Team 6Team 6
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
NUST Stuff
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
pinakspatel
 
Queue oop
Queue   oopQueue   oop
Queue oop
Gouda Mando
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
VisnuDharsini
 
Javascript function
Javascript functionJavascript function
Javascript function
LearningTech
 
Javascript Function
Javascript FunctionJavascript Function
Javascript Function
xxbeta
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
Sriram Raj
 
Control System Homework Help
Control System Homework HelpControl System Homework Help
Control System Homework Help
Matlab Assignment Experts
 
random forest regression
random forest regressionrandom forest regression
random forest regression
Akhilesh Joshi
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimension
Deepak Singh
 
2 a networkflow
2 a networkflow2 a networkflow
2 a networkflow
Aravindharamanan S
 
.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming
Alex Moore
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
Adam Mukharil Bachtiar
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
DurgaDeviCbit
 
Numpy string functions
Numpy string functionsNumpy string functions
Numpy string functions
TONO KURIAKOSE
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
Afaq Mansoor Khan
 
UNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CUNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN C
Raj vardhan
 

What's hot (20)

queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Team 6
Team 6Team 6
Team 6
 
Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++Lecture#8 introduction to array with examples c++
Lecture#8 introduction to array with examples c++
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Queue oop
Queue   oopQueue   oop
Queue oop
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
 
Javascript function
Javascript functionJavascript function
Javascript function
 
Javascript Function
Javascript FunctionJavascript Function
Javascript Function
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
Control System Homework Help
Control System Homework HelpControl System Homework Help
Control System Homework Help
 
random forest regression
random forest regressionrandom forest regression
random forest regression
 
Chapter12 array-single-dimension
Chapter12 array-single-dimensionChapter12 array-single-dimension
Chapter12 array-single-dimension
 
2 a networkflow
2 a networkflow2 a networkflow
2 a networkflow
 
.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming.Net 4.0 Threading and Parallel Programming
.Net 4.0 Threading and Parallel Programming
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
stacks and queues
stacks and queuesstacks and queues
stacks and queues
 
Numpy string functions
Numpy string functionsNumpy string functions
Numpy string functions
 
Linked List - Insertion & Deletion
Linked List - Insertion & DeletionLinked List - Insertion & Deletion
Linked List - Insertion & Deletion
 
UNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CUNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN C
 

Similar to Array and functions

Array i imp
Array  i impArray  i imp
Array i imp
Vivek Kumar
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Janpreet Singh
 
Arrays 06.ppt
Arrays 06.pptArrays 06.ppt
Arrays 06.ppt
ahtishamtariq511
 
arrays
arraysarrays
arrays
teach4uin
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
Rakesh Roshan
 
Notes-10-Array.pdf
Notes-10-Array.pdfNotes-10-Array.pdf
Notes-10-Array.pdf
umeshraoumesh40
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
arjurakibulhasanrrr7
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
NUST Stuff
 
Array
ArrayArray
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
naveed jamali
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
irdginfo
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
4.ArraysInC.pdf
4.ArraysInC.pdf4.ArraysInC.pdf
4.ArraysInC.pdf
FarHanWasif1
 
lecture7.ppt
lecture7.pptlecture7.ppt
lecture7.ppt
EdFeranil
 
Arrays
ArraysArrays
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
Abhishek Tirkey
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
GauravPandey43518
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
adityavarte
 
Ch08
Ch08Ch08

Similar to Array and functions (20)

Array i imp
Array  i impArray  i imp
Array i imp
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Arrays 06.ppt
Arrays 06.pptArrays 06.ppt
Arrays 06.ppt
 
arrays
arraysarrays
arrays
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Notes-10-Array.pdf
Notes-10-Array.pdfNotes-10-Array.pdf
Notes-10-Array.pdf
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
Array
ArrayArray
Array
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
 
4.ArraysInC.pdf
4.ArraysInC.pdf4.ArraysInC.pdf
4.ArraysInC.pdf
 
lecture7.ppt
lecture7.pptlecture7.ppt
lecture7.ppt
 
Arrays
ArraysArrays
Arrays
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
C++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptxC++ STL (quickest way to learn, even for absolute beginners).pptx
C++ STL (quickest way to learn, even for absolute beginners).pptx
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
 
Ch08
Ch08Ch08
Ch08
 

More from Aneesh Pavan Prodduturu

IDENTIFY THE MAJOR SOURCES/ CAUSES OF WASTAGE OF WATER RESOURCE IN OUR HOSTEL
IDENTIFY THE MAJOR SOURCES/ CAUSES OF WASTAGE OF WATER RESOURCE IN OUR HOSTELIDENTIFY THE MAJOR SOURCES/ CAUSES OF WASTAGE OF WATER RESOURCE IN OUR HOSTEL
IDENTIFY THE MAJOR SOURCES/ CAUSES OF WASTAGE OF WATER RESOURCE IN OUR HOSTEL
Aneesh Pavan Prodduturu
 
E waste Management
E waste ManagementE waste Management
E waste Management
Aneesh Pavan Prodduturu
 
Line following robot
Line following robotLine following robot
Line following robot
Aneesh Pavan Prodduturu
 
Mesh and nodal
Mesh and nodalMesh and nodal
Mesh and nodal
Aneesh Pavan Prodduturu
 
metal oxide semiconductor field effect transistor (Mosfet)
metal oxide semiconductor field effect transistor (Mosfet)metal oxide semiconductor field effect transistor (Mosfet)
metal oxide semiconductor field effect transistor (Mosfet)
Aneesh Pavan Prodduturu
 
KVL and KCL
KVL and KCLKVL and KCL
Ac circuits
Ac circuitsAc circuits
Filters
FiltersFilters
Dc circuits MCQ's
Dc circuits  MCQ'sDc circuits  MCQ's
Dc circuits MCQ's
Aneesh Pavan Prodduturu
 

More from Aneesh Pavan Prodduturu (10)

IDENTIFY THE MAJOR SOURCES/ CAUSES OF WASTAGE OF WATER RESOURCE IN OUR HOSTEL
IDENTIFY THE MAJOR SOURCES/ CAUSES OF WASTAGE OF WATER RESOURCE IN OUR HOSTELIDENTIFY THE MAJOR SOURCES/ CAUSES OF WASTAGE OF WATER RESOURCE IN OUR HOSTEL
IDENTIFY THE MAJOR SOURCES/ CAUSES OF WASTAGE OF WATER RESOURCE IN OUR HOSTEL
 
E waste Management
E waste ManagementE waste Management
E waste Management
 
Line following robot
Line following robotLine following robot
Line following robot
 
Mesh and nodal
Mesh and nodalMesh and nodal
Mesh and nodal
 
metal oxide semiconductor field effect transistor (Mosfet)
metal oxide semiconductor field effect transistor (Mosfet)metal oxide semiconductor field effect transistor (Mosfet)
metal oxide semiconductor field effect transistor (Mosfet)
 
KVL and KCL
KVL and KCLKVL and KCL
KVL and KCL
 
pn diode
pn diodepn diode
pn diode
 
Ac circuits
Ac circuitsAc circuits
Ac circuits
 
Filters
FiltersFilters
Filters
 
Dc circuits MCQ's
Dc circuits  MCQ'sDc circuits  MCQ's
Dc circuits MCQ's
 

Recently uploaded

Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
CVCSOfficial
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
Nada Hikmah
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
ijaia
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
aryanpankaj78
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
ElakkiaU
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
PKavitha10
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
21UME003TUSHARDEB
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
upoux
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 

Recently uploaded (20)

Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
TIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptxTIME TABLE MANAGEMENT SYSTEM testing.pptx
TIME TABLE MANAGEMENT SYSTEM testing.pptx
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
Curve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods RegressionCurve Fitting in Numerical Methods Regression
Curve Fitting in Numerical Methods Regression
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
Digital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptxDigital Twins Computer Networking Paper Presentation.pptx
Digital Twins Computer Networking Paper Presentation.pptx
 
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
An Introduction to the Compiler Designss
An Introduction to the Compiler DesignssAn Introduction to the Compiler Designss
An Introduction to the Compiler Designss
 
CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1CEC 352 - SATELLITE COMMUNICATION UNIT 1
CEC 352 - SATELLITE COMMUNICATION UNIT 1
 
Mechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdfMechanical Engineering on AAI Summer Training Report-003.pdf
Mechanical Engineering on AAI Summer Training Report-003.pdf
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
一比一原版(osu毕业证书)美国俄勒冈州立大学毕业证如何办理
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 

Array and functions

  • 1. ©LPU CSE101 C Programming CSE101-Lec#17 • Arrays • (Arrays and Functions)
  • 2. ©LPU CSE101 C Programming Outline • To declare an array • To initialize an array • To pass an array to a function
  • 3. ©LPU CSE101 C Programming Introduction • Arrays – Collection of related data items of same data type. – Static entity – i.e. they remain the same size throughout program execution
  • 4. ©LPU CSE101 C Programming Arrays • Array – Group of consecutive memory locations – Same name and data type • To refer to an element, specify: – Array name – Position number in square brackets([]) • Format: arrayname[position_number] – First element is always at position 0 – Eg. n element array named c: • c[0], c[1]...c[n – 1] Name of array (Note that all elements of this array have the same name, c) Position number of the element within array c 3 c[6] -45 6 0 72 -89 0 62 -3 1 6453 78 c[0] c[1] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4]
  • 5. ©LPU CSE101 C Programming Arrays • An array is an ordered list of values c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 c The entire array has a single name Each value has a numeric index This array holds 10 values that are indexed from 0 to 9
  • 6. ©LPU CSE101 C Programming Arrays • Array elements are like normal variables c[0] = 3;/*stores 3 to c[0] element*/ scanf (“%d”, &c[1]);/*reads c[1] element*/ printf (“%d, %d”, c[0], c[1]); /*displays c[0] & c[1] element*/ • The position number inside square brackets is called subscript/index. • Subscript must be integer or an integer expression c[5 - 2] = 7; (i.e. c[3] = 7)
  • 7. ©LPU CSE101 C Programming Defining Arrays • When defining arrays, specify: – Name – Data Type of array – Number of elements datatype arrayName[numberOfElements]; – Examples: int students[10]; float myArray[3284]; • Defining multiple arrays of same data type – Format is similar to regular variables – Example: int b[100], x[27];
  • 8. ©LPU CSE101 C Programming Initializing Arrays • Initializers int n[5] = { 1, 2, 3, 4, 5 }; – If not enough initializers given, then rightmost elements become 0 – int n[5] = { 0 }; // initialize all elements to 0 – C arrays have no bounds checking. • If size is omitted, initializers determine it int n[] = { 1, 2, 3, 4, 5 }; – 5 initializers, therefore 5 element array.
  • 9. ©LPU CSE101 C Programming Initializing Arrays • Array is same as the variable can prompt for value from the user at run time. • Array is a group of elements so we use for loop to get the values of every element instead of getting single value at a time. • Example: int array[5], i; // array of size 5 for(i=0;i<5;i++){// loop begins from 0 to 4 scanf(“%d”, &array[i]); }
  • 10. ©LPU CSE101 C Programming Program of Initializing an array to zero using loop.
  • 11. ©LPU CSE101 C Programming Element Value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 0 n[6] 0 0 0 0 0 0 0 0 n[0] n[1] n[2] n[3] n[9] n[8] n[7] n[5] n[4] 0
  • 12. ©LPU CSE101 C Programming Program of Initializing an array element with calculations using loop.
  • 13. ©LPU CSE101 C Programming Element Value 0 2 1 4 2 6 3 8 4 10 5 12 6 14 7 16 8 18 9 20 10 n[6] 4 6 8 12 14 16 18 20 n[0] n[1] n[2] n[3] n[9] n[8] n[7] n[5] n[4] 2
  • 14. ©LPU CSE101 C Programming Total of array element values is 383 Program to compute sum of elements of array
  • 15. ©LPU CSE101 C Programming array = 0012FF78 &array[0] = 0012FF78 &array = 0012FF78 #include <stdio.h> /* function main begins program execution */ int main() { char array[ 5 ]; /* define an array of size 5 */ printf( " array = %pn&array[0] = %pn" " &array = %pn", array, &array[ 0 ], &array ); return 0; /* indicates successful termination */ } /* end main */ Program to explain the address of array
  • 16. ©LPU CSE101 C Programming Character Arrays • Character arrays – Character arrays can be initialized using string literals char string1[] = "first"; – It is equivalent to char string1[] = { 'f', 'i', 'r', 's', 't', '0' }; • Null character '0' terminates strings • string1 actually has 6 elements – Can access individual characters string1[ 3 ] is character ‘s’ – Array name is address of array, so & not needed for scanf scanf( "%s", string2 ); • Reads characters until whitespace encountered
  • 17. ©LPU CSE101 C Programming , Program to print character array as strings.
  • 18. ©LPU CSE101 C Programming Enter a string: Hello string1 is: Hello string2 is: string literal
  • 19. ©LPU CSE101 C Programming Passing Arrays to Function • Arrays can be passed to functions in two ways: 1. Pass entire array 2. Pass array element by element
  • 20. ©LPU CSE101 C Programming Pass entire array • Here entire array can be passed as an argument to the function • Function gets complete access to the original array • While passing entire array Address of first element is passed to function, any changes made inside function, directly affects the Original value. void modifyArray(int b[], int arraySize); • Function passing method: “ Pass by Address”
  • 21. ©LPU CSE101 C Programming Pass array element by element • Here individual elements are passed to the function as argument • Duplicate carbon copy of Original variable is passed to function • So any changes made inside function does not affects the original value • Function doesn’t get complete access to the original array element. void modifyElement(int e); • Function passing method: “ Pass by Value”
  • 22. ©LPU CSE101 C Programming Passing Arrays to Functions • Function prototype void modifyArray(int b[], int arraySize); – Parameter names optional in prototype • int b[] could be written int [] • int arraySize could be simply int void modifyArray(int [], int); • Function call int a[SIZE]; modifyArray(a, SIZE);
  • 23. ©LPU CSE101 C Programming Passing arrays and individual array elements to functions
  • 24. ©LPU CSE101 C Programming
  • 25. ©LPU CSE101 C Programming
  • 26. ©LPU CSE101 C Programming Effects of passing entire array by reference: The values of the original array are: 0 1 2 3 4 The values of the modified array are: 0 2 4 6 8 Effects of passing array element by value: The value of a[3] is 6 Value in modifyElement is 12 The value of a[3] is 6
  • 27. ©LPU CSE101 C Programming cse101@lpu.co.in Next Class: Applications of Arrays

Editor's Notes

  1. This will allocate 10*2 bytes of space in memory