SlideShare una empresa de 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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (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
 

Similar 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
 
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
 

Similar 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
 
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
 
DSA-Lecture-05
DSA-Lecture-05DSA-Lecture-05
DSA-Lecture-05
 

Más 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
 

Más 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

4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsPooky Knightsmith
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...DhatriParmar
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 

Último (20)

4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Mental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young mindsMental Health Awareness - a toolkit for supporting young minds
Mental Health Awareness - a toolkit for supporting young minds
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
Blowin' in the Wind of Caste_ Bob Dylan's Song as a Catalyst for Social Justi...
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 

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