SlideShare uma empresa Scribd logo
1 de 24
Lecture 11Lecture 11
Version 1.0Version 1.0
Arrays: One DimensionalArrays: One Dimensional
2Rushdi Shams, Dept of CSE, KUET, Bangladesh
ArraysArrays
 one of the C’s most essential data structuresone of the C’s most essential data structures
 Arrays are data structures consisting of relatedArrays are data structures consisting of related
data items of the same typedata items of the same type
 it is a group of memory locations related by theit is a group of memory locations related by the
fact that they all have the same name and samefact that they all have the same name and same
typetype
3Rushdi Shams, Dept of CSE, KUET, Bangladesh
Why ArraysWhy Arrays
 No doubt, this program will print the value ofNo doubt, this program will print the value of xx as 10as 10
 Because when a value 10 is assigned toBecause when a value 10 is assigned to xx, the earlier, the earlier
value ofvalue of xx, i.e. 5, is lost, i.e. 5, is lost
 ordinary variables (the ones which we have used so far)ordinary variables (the ones which we have used so far)
are capable of holding only one value at a timeare capable of holding only one value at a time
4Rushdi Shams, Dept of CSE, KUET, Bangladesh
Why ArraysWhy Arrays
 However, there are situations in which we would wantHowever, there are situations in which we would want
to store more than one value at a time in a singleto store more than one value at a time in a single
variablevariable
 suppose we wish to arrange the percentage markssuppose we wish to arrange the percentage marks
obtained by 100 students in ascending order. In such aobtained by 100 students in ascending order. In such a
case we have two options to store these marks incase we have two options to store these marks in
memory:memory:
1.1. Construct 100 variables, each variable containing oneConstruct 100 variables, each variable containing one
student’s marks.student’s marks.
2.2. Construct one variable capable of storing or holdingConstruct one variable capable of storing or holding
all the hundred values.all the hundred values.
5Rushdi Shams, Dept of CSE, KUET, Bangladesh
Why ArraysWhy Arrays
 the second alternative is betterthe second alternative is better
 it would be much easier to handle one variableit would be much easier to handle one variable
than handling 100 different variablesthan handling 100 different variables
 Moreover, there are certain logics that cannotMoreover, there are certain logics that cannot
be dealt with, without the use of an arraybe dealt with, without the use of an array
6Rushdi Shams, Dept of CSE, KUET, Bangladesh
One Dimensional ArrayOne Dimensional Array
 a one dimensional array is a list of variables thata one dimensional array is a list of variables that
are all of the same type and accessed through aare all of the same type and accessed through a
common namecommon name
 An individual element in an array is calledAn individual element in an array is called arrayarray
elementelement
 Array is helpful to handle a group of similarArray is helpful to handle a group of similar
types of datatypes of data
7Rushdi Shams, Dept of CSE, KUET, Bangladesh
One Dimensional ArrayOne Dimensional Array
 To declare a one dimensional array, we use-To declare a one dimensional array, we use-
data_type array_name [size];data_type array_name [size];
 data_type is a valid C data type,data_type is a valid C data type,
 array_name is the name of that array andarray_name is the name of that array and
 size specifies the number of elements in thesize specifies the number of elements in the
arrayarray
8Rushdi Shams, Dept of CSE, KUET, Bangladesh
One Dimensional ArrayOne Dimensional Array
int my_array[20];int my_array[20];
 Declares an array name my_array of type integerDeclares an array name my_array of type integer
that contains 20 elementsthat contains 20 elements
9Rushdi Shams, Dept of CSE, KUET, Bangladesh
One Dimensional ArrayOne Dimensional Array
 An array element is accessed by indexing theAn array element is accessed by indexing the
array using the number of elementarray using the number of element
 all arrays begin at zeroall arrays begin at zero
 if you want to access the first element in anif you want to access the first element in an
array, use zero for the indexarray, use zero for the index
 To index an array, specify the index of theTo index an array, specify the index of the
element you want inside square bracketselement you want inside square brackets
10Rushdi Shams, Dept of CSE, KUET, Bangladesh
One Dimensional ArrayOne Dimensional Array
 The second element of my_array will be-The second element of my_array will be-
my_array [1]my_array [1]
11Rushdi Shams, Dept of CSE, KUET, Bangladesh
One Dimensional ArrayOne Dimensional Array
 C stores one dimensionalC stores one dimensional
array in one contiguousarray in one contiguous
memory location withmemory location with
first element at the lowerfirst element at the lower
addressaddress
 an array namedan array named aa of 10of 10
elements can occupy theelements can occupy the
memory as follows-memory as follows-
12Rushdi Shams, Dept of CSE, KUET, Bangladesh
One Dimensional ArrayOne Dimensional Array
 a program that declares an array of 10 elements anda program that declares an array of 10 elements and
initializes every element of that array with 0initializes every element of that array with 0
13Rushdi Shams, Dept of CSE, KUET, Bangladesh
One Dimensional ArrayOne Dimensional Array
 an array can also be initialized by following thean array can also be initialized by following the
declaration with an equal sign and a comma separateddeclaration with an equal sign and a comma separated
list of values within a pair of curly bracelist of values within a pair of curly brace
14Rushdi Shams, Dept of CSE, KUET, Bangladesh
One Dimensional ArrayOne Dimensional Array
 If there are fewer values than elements in array, theIf there are fewer values than elements in array, the
remaining elements are initialized automatically withremaining elements are initialized automatically with
zerozero
15Rushdi Shams, Dept of CSE, KUET, Bangladesh
One Dimensional ArrayOne Dimensional Array
 If you put more values than the array can hold, thereIf you put more values than the array can hold, there
will be a syntax errorwill be a syntax error
16Rushdi Shams, Dept of CSE, KUET, Bangladesh
One Dimensional ArrayOne Dimensional Array
 If array size is omitted during declaration, the numberIf array size is omitted during declaration, the number
of values during array initialization will be the numberof values during array initialization will be the number
of elements the array can holdof elements the array can hold
17Rushdi Shams, Dept of CSE, KUET, Bangladesh
Simple program using ArraySimple program using Array
18Rushdi Shams, Dept of CSE, KUET, Bangladesh
Character ArraysCharacter Arrays
 Character arrays have several unique featuresCharacter arrays have several unique features
 A character array can be initialized using aA character array can be initialized using a stringstring
literalliteral
char string1[ ] = “first”;char string1[ ] = “first”;
19Rushdi Shams, Dept of CSE, KUET, Bangladesh
Character ArraysCharacter Arrays
 ““first” string literal contains five characters plusfirst” string literal contains five characters plus
a special string termination character called nulla special string termination character called null
character (0)character (0)
 string1 array actually has 6 elements-f, i, r, s, tstring1 array actually has 6 elements-f, i, r, s, t
and 0and 0
20Rushdi Shams, Dept of CSE, KUET, Bangladesh
Character ArraysCharacter Arrays
 Character arrays can be initialized as follows asCharacter arrays can be initialized as follows as
well-well-
char string1 [ ]= {‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ‘0’};char string1 [ ]= {‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ‘0’};
 We can access individual characters in a stringWe can access individual characters in a string
directly using array subscript notation. Fordirectly using array subscript notation. For
example, string1 [3] is the character ‘s’example, string1 [3] is the character ‘s’
21Rushdi Shams, Dept of CSE, KUET, Bangladesh
Character ArraysCharacter Arrays
 We can also input a string directly from theWe can also input a string directly from the
keyboard using scanf () function and %skeyboard using scanf () function and %s
specifier.specifier.
char string1 [20];char string1 [20];
scanf (“%s”, string1);scanf (“%s”, string1);
22Rushdi Shams, Dept of CSE, KUET, Bangladesh
Character ArraysCharacter Arrays
 the name of the array is passed to scanf ()the name of the array is passed to scanf ()
without the preceding & used with otherwithout the preceding & used with other
variablesvariables
 The & is normally used to provide scanf () withThe & is normally used to provide scanf () with
a variable’s location in a memory so a value cana variable’s location in a memory so a value can
be stored therebe stored there
 Array name is the address of the start of theArray name is the address of the start of the
array, therefore, & is not necessaryarray, therefore, & is not necessary
23Rushdi Shams, Dept of CSE, KUET, Bangladesh
Character ArraysCharacter Arrays
 scanf () reads characters from the keyboard untilscanf () reads characters from the keyboard until
the first whitespace character is encounteredthe first whitespace character is encountered
 scanf () takes upto the first whitespacescanf () takes upto the first whitespace
24Rushdi Shams, Dept of CSE, KUET, Bangladesh

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Array
ArrayArray
Array
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
Lec 16. Strings
Lec 16. StringsLec 16. Strings
Lec 16. Strings
 
Arrays
ArraysArrays
Arrays
 
OOPs with java
OOPs with javaOOPs with java
OOPs with java
 
Lec 25 - arrays-strings
Lec 25 - arrays-stringsLec 25 - arrays-strings
Lec 25 - arrays-strings
 
Arrays In C
Arrays In CArrays In C
Arrays In C
 
javaarray
javaarrayjavaarray
javaarray
 
Array in c#
Array in c#Array in c#
Array in c#
 
Week06
Week06Week06
Week06
 
Association rule mining
Association rule miningAssociation rule mining
Association rule mining
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
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
 
Array in c
Array in cArray in c
Array in c
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Cso gaddis java_chapter8
Cso gaddis java_chapter8Cso gaddis java_chapter8
Cso gaddis java_chapter8
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
 
arrays in c
arrays in carrays in c
arrays in c
 
Array in C# 3.5
Array in C# 3.5Array in C# 3.5
Array in C# 3.5
 

Semelhante a Lec 11. One Dimensional Arrays

Lec 12. Multidimensional Arrays / Passing Arrays to Functions
Lec 12. Multidimensional Arrays / Passing Arrays to FunctionsLec 12. Multidimensional Arrays / Passing Arrays to Functions
Lec 12. Multidimensional Arrays / Passing Arrays to FunctionsRushdi Shams
 
Lec 15. Pointers and Arrays
Lec 15. Pointers and ArraysLec 15. Pointers and Arrays
Lec 15. Pointers and ArraysRushdi Shams
 
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
 
arrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdfarrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdfMarlonMagtibay2
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docxPamarthi Kumar
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsOCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsİbrahim Kürce
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxAmanRai352102
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web ProgrammingAmirul Azhar
 
Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Abbott
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2Abbott
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useMukesh Tekwani
 
dizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.ppsxdizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.ppsxVijayKumarLokanadam
 
dizital pods session 6-arrays.pptx
dizital pods session 6-arrays.pptxdizital pods session 6-arrays.pptx
dizital pods session 6-arrays.pptxVijayKumarLokanadam
 

Semelhante a Lec 11. One Dimensional Arrays (20)

Lec 12. Multidimensional Arrays / Passing Arrays to Functions
Lec 12. Multidimensional Arrays / Passing Arrays to FunctionsLec 12. Multidimensional Arrays / Passing Arrays to Functions
Lec 12. Multidimensional Arrays / Passing Arrays to Functions
 
Lec 15. Pointers and Arrays
Lec 15. Pointers and ArraysLec 15. Pointers and Arrays
Lec 15. Pointers and Arrays
 
arrays.docx
arrays.docxarrays.docx
arrays.docx
 
Unit 2
Unit 2Unit 2
Unit 2
 
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
 
arrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdfarrays-130116232821-phpapp02.pdf
arrays-130116232821-phpapp02.pdf
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
 
OCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIsOCA Java SE 8 Exam Chapter 3 Core Java APIs
OCA Java SE 8 Exam Chapter 3 Core Java APIs
 
unit 2.pptx
unit 2.pptxunit 2.pptx
unit 2.pptx
 
ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)Data structure lecture 2 (pdf)
Data structure lecture 2 (pdf)
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
 
Java chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and useJava chapter 6 - Arrays -syntax and use
Java chapter 6 - Arrays -syntax and use
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arrays
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
dizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.ppsxdizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.ppsx
 
dizital pods session 6-arrays.pptx
dizital pods session 6-arrays.pptxdizital pods session 6-arrays.pptx
dizital pods session 6-arrays.pptx
 

Mais de Rushdi Shams

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchRushdi Shams
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IRRushdi Shams
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101Rushdi Shams
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processingRushdi Shams
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: ParsingRushdi Shams
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translationRushdi Shams
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translationRushdi Shams
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semanticsRushdi Shams
 
Propositional logic
Propositional logicPropositional logic
Propositional logicRushdi Shams
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logicRushdi Shams
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structureRushdi Shams
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representationRushdi Shams
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hackingRushdi Shams
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)Rushdi Shams
 

Mais de Rushdi Shams (20)

Research Methodology and Tips on Better Research
Research Methodology and Tips on Better ResearchResearch Methodology and Tips on Better Research
Research Methodology and Tips on Better Research
 
Common evaluation measures in NLP and IR
Common evaluation measures in NLP and IRCommon evaluation measures in NLP and IR
Common evaluation measures in NLP and IR
 
Machine learning with nlp 101
Machine learning with nlp 101Machine learning with nlp 101
Machine learning with nlp 101
 
Semi-supervised classification for natural language processing
Semi-supervised classification for natural language processingSemi-supervised classification for natural language processing
Semi-supervised classification for natural language processing
 
Natural Language Processing: Parsing
Natural Language Processing: ParsingNatural Language Processing: Parsing
Natural Language Processing: Parsing
 
Types of machine translation
Types of machine translationTypes of machine translation
Types of machine translation
 
L1 l2 l3 introduction to machine translation
L1 l2 l3  introduction to machine translationL1 l2 l3  introduction to machine translation
L1 l2 l3 introduction to machine translation
 
Syntax and semantics
Syntax and semanticsSyntax and semantics
Syntax and semantics
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Probabilistic logic
Probabilistic logicProbabilistic logic
Probabilistic logic
 
L15 fuzzy logic
L15  fuzzy logicL15  fuzzy logic
L15 fuzzy logic
 
Knowledge structure
Knowledge structureKnowledge structure
Knowledge structure
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 
First order logic
First order logicFirst order logic
First order logic
 
Belief function
Belief functionBelief function
Belief function
 
L5 understanding hacking
L5  understanding hackingL5  understanding hacking
L5 understanding hacking
 
L4 vpn
L4  vpnL4  vpn
L4 vpn
 
L3 defense
L3  defenseL3  defense
L3 defense
 
L2 Intrusion Detection System (IDS)
L2  Intrusion Detection System (IDS)L2  Intrusion Detection System (IDS)
L2 Intrusion Detection System (IDS)
 
L1 phishing
L1  phishingL1  phishing
L1 phishing
 

Último

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 

Último (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 

Lec 11. One Dimensional Arrays

  • 1. Lecture 11Lecture 11 Version 1.0Version 1.0 Arrays: One DimensionalArrays: One Dimensional
  • 2. 2Rushdi Shams, Dept of CSE, KUET, Bangladesh ArraysArrays  one of the C’s most essential data structuresone of the C’s most essential data structures  Arrays are data structures consisting of relatedArrays are data structures consisting of related data items of the same typedata items of the same type  it is a group of memory locations related by theit is a group of memory locations related by the fact that they all have the same name and samefact that they all have the same name and same typetype
  • 3. 3Rushdi Shams, Dept of CSE, KUET, Bangladesh Why ArraysWhy Arrays  No doubt, this program will print the value ofNo doubt, this program will print the value of xx as 10as 10  Because when a value 10 is assigned toBecause when a value 10 is assigned to xx, the earlier, the earlier value ofvalue of xx, i.e. 5, is lost, i.e. 5, is lost  ordinary variables (the ones which we have used so far)ordinary variables (the ones which we have used so far) are capable of holding only one value at a timeare capable of holding only one value at a time
  • 4. 4Rushdi Shams, Dept of CSE, KUET, Bangladesh Why ArraysWhy Arrays  However, there are situations in which we would wantHowever, there are situations in which we would want to store more than one value at a time in a singleto store more than one value at a time in a single variablevariable  suppose we wish to arrange the percentage markssuppose we wish to arrange the percentage marks obtained by 100 students in ascending order. In such aobtained by 100 students in ascending order. In such a case we have two options to store these marks incase we have two options to store these marks in memory:memory: 1.1. Construct 100 variables, each variable containing oneConstruct 100 variables, each variable containing one student’s marks.student’s marks. 2.2. Construct one variable capable of storing or holdingConstruct one variable capable of storing or holding all the hundred values.all the hundred values.
  • 5. 5Rushdi Shams, Dept of CSE, KUET, Bangladesh Why ArraysWhy Arrays  the second alternative is betterthe second alternative is better  it would be much easier to handle one variableit would be much easier to handle one variable than handling 100 different variablesthan handling 100 different variables  Moreover, there are certain logics that cannotMoreover, there are certain logics that cannot be dealt with, without the use of an arraybe dealt with, without the use of an array
  • 6. 6Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  a one dimensional array is a list of variables thata one dimensional array is a list of variables that are all of the same type and accessed through aare all of the same type and accessed through a common namecommon name  An individual element in an array is calledAn individual element in an array is called arrayarray elementelement  Array is helpful to handle a group of similarArray is helpful to handle a group of similar types of datatypes of data
  • 7. 7Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  To declare a one dimensional array, we use-To declare a one dimensional array, we use- data_type array_name [size];data_type array_name [size];  data_type is a valid C data type,data_type is a valid C data type,  array_name is the name of that array andarray_name is the name of that array and  size specifies the number of elements in thesize specifies the number of elements in the arrayarray
  • 8. 8Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array int my_array[20];int my_array[20];  Declares an array name my_array of type integerDeclares an array name my_array of type integer that contains 20 elementsthat contains 20 elements
  • 9. 9Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  An array element is accessed by indexing theAn array element is accessed by indexing the array using the number of elementarray using the number of element  all arrays begin at zeroall arrays begin at zero  if you want to access the first element in anif you want to access the first element in an array, use zero for the indexarray, use zero for the index  To index an array, specify the index of theTo index an array, specify the index of the element you want inside square bracketselement you want inside square brackets
  • 10. 10Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  The second element of my_array will be-The second element of my_array will be- my_array [1]my_array [1]
  • 11. 11Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  C stores one dimensionalC stores one dimensional array in one contiguousarray in one contiguous memory location withmemory location with first element at the lowerfirst element at the lower addressaddress  an array namedan array named aa of 10of 10 elements can occupy theelements can occupy the memory as follows-memory as follows-
  • 12. 12Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  a program that declares an array of 10 elements anda program that declares an array of 10 elements and initializes every element of that array with 0initializes every element of that array with 0
  • 13. 13Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  an array can also be initialized by following thean array can also be initialized by following the declaration with an equal sign and a comma separateddeclaration with an equal sign and a comma separated list of values within a pair of curly bracelist of values within a pair of curly brace
  • 14. 14Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  If there are fewer values than elements in array, theIf there are fewer values than elements in array, the remaining elements are initialized automatically withremaining elements are initialized automatically with zerozero
  • 15. 15Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  If you put more values than the array can hold, thereIf you put more values than the array can hold, there will be a syntax errorwill be a syntax error
  • 16. 16Rushdi Shams, Dept of CSE, KUET, Bangladesh One Dimensional ArrayOne Dimensional Array  If array size is omitted during declaration, the numberIf array size is omitted during declaration, the number of values during array initialization will be the numberof values during array initialization will be the number of elements the array can holdof elements the array can hold
  • 17. 17Rushdi Shams, Dept of CSE, KUET, Bangladesh Simple program using ArraySimple program using Array
  • 18. 18Rushdi Shams, Dept of CSE, KUET, Bangladesh Character ArraysCharacter Arrays  Character arrays have several unique featuresCharacter arrays have several unique features  A character array can be initialized using aA character array can be initialized using a stringstring literalliteral char string1[ ] = “first”;char string1[ ] = “first”;
  • 19. 19Rushdi Shams, Dept of CSE, KUET, Bangladesh Character ArraysCharacter Arrays  ““first” string literal contains five characters plusfirst” string literal contains five characters plus a special string termination character called nulla special string termination character called null character (0)character (0)  string1 array actually has 6 elements-f, i, r, s, tstring1 array actually has 6 elements-f, i, r, s, t and 0and 0
  • 20. 20Rushdi Shams, Dept of CSE, KUET, Bangladesh Character ArraysCharacter Arrays  Character arrays can be initialized as follows asCharacter arrays can be initialized as follows as well-well- char string1 [ ]= {‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ‘0’};char string1 [ ]= {‘f’, ‘i’, ‘r’, ‘s’, ‘t’, ‘0’};  We can access individual characters in a stringWe can access individual characters in a string directly using array subscript notation. Fordirectly using array subscript notation. For example, string1 [3] is the character ‘s’example, string1 [3] is the character ‘s’
  • 21. 21Rushdi Shams, Dept of CSE, KUET, Bangladesh Character ArraysCharacter Arrays  We can also input a string directly from theWe can also input a string directly from the keyboard using scanf () function and %skeyboard using scanf () function and %s specifier.specifier. char string1 [20];char string1 [20]; scanf (“%s”, string1);scanf (“%s”, string1);
  • 22. 22Rushdi Shams, Dept of CSE, KUET, Bangladesh Character ArraysCharacter Arrays  the name of the array is passed to scanf ()the name of the array is passed to scanf () without the preceding & used with otherwithout the preceding & used with other variablesvariables  The & is normally used to provide scanf () withThe & is normally used to provide scanf () with a variable’s location in a memory so a value cana variable’s location in a memory so a value can be stored therebe stored there  Array name is the address of the start of theArray name is the address of the start of the array, therefore, & is not necessaryarray, therefore, & is not necessary
  • 23. 23Rushdi Shams, Dept of CSE, KUET, Bangladesh Character ArraysCharacter Arrays  scanf () reads characters from the keyboard untilscanf () reads characters from the keyboard until the first whitespace character is encounteredthe first whitespace character is encountered  scanf () takes upto the first whitespacescanf () takes upto the first whitespace
  • 24. 24Rushdi Shams, Dept of CSE, KUET, Bangladesh