SlideShare ist ein Scribd-Unternehmen logo
1 von 7
Programming Fundamentals
(Week 12)
Lecturer: Muhammad Bilal
Deptt. of Computer Science
Cecos University Peshawar
Arrays
• An array is a type of data structure that stores elements of
same type.
• Array size is fixed and cannot be changed.
• Elements(data) are sequentially stored in array.
• All arrays consist of contiguous memory locations.
• The lowest address corresponds to the first element and the
highest address to the last element.
Why arrays?
• You need to find out the average of 100 integer numbers entered by user.
In C, you have two ways to do this:
1. Define 100 variables with int data type and then perform 100 scanf()
operations to store the entered values in the variables and then at last
calculate the average of them.
2. Have a single integer array to store all the values, loop the array to store
all the entered values in array and later calculate the average.
Which option is better? Justify.
Array declaration
To declare an array in C, you need to specify two things.
1. the type of the elements
2. the number of elements in array
type arrayName [ arraySize ];
• type can be any valid C data type like int, float, char etc.
• arrayName is given according to the variable naming rules in C.
• arraySize must be integer greater than zero.
Array declaration
• Below is the example of declaring marks array of type int with
capacity of storing ten marks.
Array initialization
• You can initialize an array during declaration.
int mark[5] = {19, 10, 8, 17, 9};
• You can also initialize an array like this.
int mark[] = {19, 10, 8, 17, 9};
• we haven't specified the size. However, the compiler knows its size is
5 as we are initializing it with 5 elements.
• Arrays have 0 as the first index, not 1. In this example, mark[0] is the
first element.
• If the size of an array is n, to access the last element, the n-1 index is
used. In this example, mark[4]
• Suppose the starting address of mark[0] is 2120d. Then, the address
of the mark[1] will be 2124d. Similarly, the address of mark[2] will be
2128d and so on.
• This is because the size of a float is 4 bytes.

Weitere ähnliche Inhalte

Ähnlich wie Programming fundamentals week 12.pptx

5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
teddiyfentaw
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
eShikshak
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
Uma mohan
 

Ähnlich wie Programming fundamentals week 12.pptx (20)

Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
5 ARRAYS AND STRINGSjiuojhiooioiiouioi.pptx
 
Array
ArrayArray
Array
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
 
Array and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdfArray and its types and it's implemented programming Final.pdf
Array and its types and it's implemented programming Final.pdf
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Programming in c arrays
Programming in c   arraysProgramming in c   arrays
Programming in c arrays
 
Arrays & Strings
Arrays & StringsArrays & Strings
Arrays & Strings
 
Programming in c Arrays
Programming in c ArraysProgramming in c Arrays
Programming in c Arrays
 
Arrays
ArraysArrays
Arrays
 
Unit 2
Unit 2Unit 2
Unit 2
 
Arrays
ArraysArrays
Arrays
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Arrays
ArraysArrays
Arrays
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
Arrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptxArrays_and_Strings_in_C_Programming.pptx
Arrays_and_Strings_in_C_Programming.pptx
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 

KĂźrzlich hochgeladen

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

KĂźrzlich hochgeladen (20)

Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 

Programming fundamentals week 12.pptx

  • 1. Programming Fundamentals (Week 12) Lecturer: Muhammad Bilal Deptt. of Computer Science Cecos University Peshawar
  • 2. Arrays • An array is a type of data structure that stores elements of same type. • Array size is fixed and cannot be changed. • Elements(data) are sequentially stored in array. • All arrays consist of contiguous memory locations. • The lowest address corresponds to the first element and the highest address to the last element.
  • 3. Why arrays? • You need to find out the average of 100 integer numbers entered by user. In C, you have two ways to do this: 1. Define 100 variables with int data type and then perform 100 scanf() operations to store the entered values in the variables and then at last calculate the average of them. 2. Have a single integer array to store all the values, loop the array to store all the entered values in array and later calculate the average. Which option is better? Justify.
  • 4. Array declaration To declare an array in C, you need to specify two things. 1. the type of the elements 2. the number of elements in array type arrayName [ arraySize ]; • type can be any valid C data type like int, float, char etc. • arrayName is given according to the variable naming rules in C. • arraySize must be integer greater than zero.
  • 5. Array declaration • Below is the example of declaring marks array of type int with capacity of storing ten marks.
  • 6. Array initialization • You can initialize an array during declaration. int mark[5] = {19, 10, 8, 17, 9}; • You can also initialize an array like this. int mark[] = {19, 10, 8, 17, 9}; • we haven't specified the size. However, the compiler knows its size is 5 as we are initializing it with 5 elements.
  • 7. • Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element. • If the size of an array is n, to access the last element, the n-1 index is used. In this example, mark[4] • Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will be 2124d. Similarly, the address of mark[2] will be 2128d and so on. • This is because the size of a float is 4 bytes.