SlideShare uma empresa Scribd logo
1 de 25
Ar rays(I) 
Lecture 8 
Dr. Hakem Beitollahi 
Computer Engineering Department 
Soran University
Outline 
 Arrays 
 Declaring and allocating arrays 
 Examples using arrays 
Arrays(I)— 2
Arrays 
Arrays(I)— 3
Arrays (I) 
 An array is a group of contiguous memory locations that 
all have the same name and type 
 To refer to a particular location or element in the array, 
we specify the name of the array and the position 
number 
 Array indexes 
 Position number used to refer to a specific location/element 
 Also called subscript 
 Place in square brackets 
o Must be positive integer or integer expression 
 First element has index zero 
 Example (assume a = 5 and b = 6) 
o c[ a + b ] = c[11] = 2; 
Arrays(I)— 4
Arrays (II) 
Arrays(I)— 5
Arrays (III) 
A -- -- -- -- 
-- -- -- -- -- -- 
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 
Arrays(I)— 6 
 Suppose 
int[] A = new int [10]; // array of 
10 uninitialized ints 
 To access an individual element we 
must apply a subscript to list name A
Arrays (IV) 
Arrays(I)— 7 
 Consider 
int i = 7, j = 2, k = 4; 
A[0] = 1; 
A[i] = 5; 
A[j] = A[i] + 3; 
A[j+1] = A[i] + A[0]; 
A[A[j]] = 12; 
A -- -- -- -- 
-- -- -- -- -- -- 
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Arrays (V) 
Arrays(I)— 8 
 Consider 
int i = 7, j = 2, k = 4; 
A[0] = 1; 
A[i] = 5; 
A[j] = A[i] + 3; 
A[j+1] = A[i] + A[0]; 
A[A[j]] = 12; 
A 1 -- -- -- 
-- -- -- -- -- -- 
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Arrays (VI) 
Arrays(I)— 9 
 Consider 
int i = 7, j = 2, k = 4; 
A[0] = 1; 
A[i] = 5; 
A[j] = A[i] + 3; 
A[j+1] = A[i] + A[0]; 
A[A[j]] = 12; 
A 1 -- -- -- 
-- -- -- 5 -- -- 
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Arrays (VII) 
Arrays(I)— 10 
 Consider 
int i = 7, j = 2, k = 4; 
A[0] = 1; 
A[i] = 5; 
A[j] = A[i] + 3; 
A[j+1] = A[i] + A[0]; 
A[A[j]] = 12; 
A 1 -- 8 -- 
-- -- -- 5 -- -- 
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Arrays (VIII) 
Arrays(I)— 11 
 Consider 
int i = 7, j = 2, k = 4; 
A[0] = 1; 
A[i] = 5; 
A[j] = A[i] + 3; 
A[j+1] = A[i] + A[0]; 
A[A[j]] = 12; 
A 1 -- 8 6 
-- -- -- 5 -- -- 
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Arrays (IX) 
Arrays(I)— 12 
 Consider 
int i = 7, j = 2, k = 4; 
A[0] = 1; 
A[i] = 5; 
A[j] = A[i] + 3; 
A[j+1] = A[i] + A[0]; 
A[A[j]] = 12; 
A 1 -- 8 6 
-- -- -- 5 12 -- 
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Arrays (X) 
Arrays(I)— 13 
 Consider 
int i = 7, j = 2, k = 4; 
A[0] = 1; 
A[i] = 5; 
A[j] = A[i] + 3; 
A[j+1] = A[i] + A[0]; 
A[A[j]] = 12; 
A 1 -- 8 6 
3 -- -- 5 12 -- 
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
Common Programming Error 7.1 
 It is important to note the difference between the 
“seventh element of the array” and “array element 
seven.” Array subscripts begin at 0, thus the “seventh 
element of the array” has a subscript of 6, while “array 
element seven” has a subscript of 7 and is actually the 
eighth element of the array. 
Arrays(I)— 14
Array Definition (I) 
Arrays(I)— 15
Array Definition (II) 
 When arrays are allocated, the elements are initialized to 
zero for the numeric primitive data-type variables, to 
false for bool variables and to null for reference types. 
 Multiple declaration in single line: 
 In an array of value types, every element of the array 
contains one value of the declared type. 
 For example, every element of an int array is an int value. 
Arrays(I)— 16 
string[] b = new string[ 100 ], x = new 
string[ 27 ]; 
double[] array1 = new double[ 10 ], 
array2 = new double[ 20 ];
Examples Using Arrays 
Arrays(I)— 17
Initializing the elements of an array 
 Example 1: Allocating an Array and 
Initializing Its Elements 
 The program creates three integer arrays of 
10 elements and displays those arrays in 
tabular format. The program demonstrates 
several techniques for declaring and 
initializing arrays 
Arrays(I)— 18
Arrays(I)— 19
Summing the elements of an array 
 Example 2: Totaling the Elements of an 
Array 
 Often, the elements of an array represent 
series of values to be used in calculations. 
 Elements of an array represent the grades for 
an exam, the professor may wish to total the 
elements, then calculate the class average. 
Arrays(I)— 20
Arrays(I)— 21
Histogram Example 
 Example 3: Using Histograms to 
Display Array Data Graphically 
 Many programs present data to users in a 
graphical manner 
 Numeric values often are displayed as bars in 
a bar chart. 
 In such a chart, longer bars represent larger 
numeric values. 
 One simple way to display numeric data 
graphically is with a histogram that shows 
each numeric value as a bar of asterisks (*). 
Arrays(I)— 22
Arrays(I)— 23 
Show length of an array
Use arrays as counters 
 Example 4: Using the Elements of an Array as Counters 
 Sometimes programs use a series of counter variables to summarize 
data, such as the results of a survey. 
 Calculate the number of occurrences of each side on a six-sided 
die 
Arrays(I)— 24
Arrays(I)— 25

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Arrays
ArraysArrays
Arrays
 
1.Array and linklst definition
1.Array and linklst definition1.Array and linklst definition
1.Array and linklst definition
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Array,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN CArray,MULTI ARRAY, IN C
Array,MULTI ARRAY, IN C
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Arrays in c
Arrays in cArrays in c
Arrays in c
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Array
ArrayArray
Array
 
Two dimensional array
Two dimensional arrayTwo dimensional array
Two dimensional array
 
Group p1
Group p1Group p1
Group p1
 
Arrays
ArraysArrays
Arrays
 
Array in c programming
Array in c programmingArray in c programming
Array in c programming
 
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 Language
Arrays In C LanguageArrays In C Language
Arrays In C Language
 
Array in C
Array in CArray in C
Array in C
 
Array in c++
Array in c++Array in c++
Array in c++
 
Data structure array
Data structure  arrayData structure  array
Data structure array
 
Array in C
Array in CArray in C
Array in C
 

Destaque

Senior Contract Specialist Roles and Responsibilities at PG&E
Senior Contract Specialist Roles and Responsibilities at PG&ESenior Contract Specialist Roles and Responsibilities at PG&E
Senior Contract Specialist Roles and Responsibilities at PG&EAlberto Rocha
 
A Paisagem Urbana
A Paisagem UrbanaA Paisagem Urbana
A Paisagem UrbanaAline Naue
 
Math Project for Mr. Medina's Class
Math Project for Mr. Medina's ClassMath Project for Mr. Medina's Class
Math Project for Mr. Medina's Classsmit5008
 
Gym registration - 2014 Apps for Good Entry
Gym registration - 2014 Apps for Good EntryGym registration - 2014 Apps for Good Entry
Gym registration - 2014 Apps for Good Entryjackojgy
 
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابعالشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابعnawal al-matary
 
"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive
"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive
"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' DriveClean Energy Canada
 
Bingham mc cutchen interview questions and answer
Bingham mc cutchen interview questions and answerBingham mc cutchen interview questions and answer
Bingham mc cutchen interview questions and answerJulianDraxler
 
Planificación
PlanificaciónPlanificación
Planificaciónrcordova83
 

Destaque (17)

กำเนิด
กำเนิดกำเนิด
กำเนิด
 
Reversting system
Reversting systemReversting system
Reversting system
 
силіцій
силіційсиліцій
силіцій
 
Presentation1
Presentation1Presentation1
Presentation1
 
Senior Contract Specialist Roles and Responsibilities at PG&E
Senior Contract Specialist Roles and Responsibilities at PG&ESenior Contract Specialist Roles and Responsibilities at PG&E
Senior Contract Specialist Roles and Responsibilities at PG&E
 
A Paisagem Urbana
A Paisagem UrbanaA Paisagem Urbana
A Paisagem Urbana
 
เนื้อเยื่อและเมมเบรน
เนื้อเยื่อและเมมเบรนเนื้อเยื่อและเมมเบรน
เนื้อเยื่อและเมมเบรน
 
Math Project for Mr. Medina's Class
Math Project for Mr. Medina's ClassMath Project for Mr. Medina's Class
Math Project for Mr. Medina's Class
 
Gym registration - 2014 Apps for Good Entry
Gym registration - 2014 Apps for Good EntryGym registration - 2014 Apps for Good Entry
Gym registration - 2014 Apps for Good Entry
 
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابعالشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
الشروط والضوابط العامة للمشاركة في المؤتمر الطلابي السابع
 
Malware
MalwareMalware
Malware
 
"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive
"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive
"How to Sell Electric Vehicles to Canadians," Cara Clairman, Plug n' Drive
 
Bingham mc cutchen interview questions and answer
Bingham mc cutchen interview questions and answerBingham mc cutchen interview questions and answer
Bingham mc cutchen interview questions and answer
 
ฟิสิกส์พื้นฐาน
ฟิสิกส์พื้นฐานฟิสิกส์พื้นฐาน
ฟิสิกส์พื้นฐาน
 
Presidential Service Award
Presidential Service AwardPresidential Service Award
Presidential Service Award
 
Question 4
Question 4Question 4
Question 4
 
Planificación
PlanificaciónPlanificación
Planificación
 

Semelhante a Lecture 8 (20)

CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
2DArrays.ppt
2DArrays.ppt2DArrays.ppt
2DArrays.ppt
 
Arrays
ArraysArrays
Arrays
 
Arrays in CPP
Arrays in CPPArrays in CPP
Arrays in CPP
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
SP-First-Lecture.ppt
SP-First-Lecture.pptSP-First-Lecture.ppt
SP-First-Lecture.ppt
 
Lecture 6 - Arrays
Lecture 6 - ArraysLecture 6 - Arrays
Lecture 6 - Arrays
 
Chapter 10.ppt
Chapter 10.pptChapter 10.ppt
Chapter 10.ppt
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
2D Array
2D Array 2D Array
2D Array
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Topic20Arrays_Part2.ppt
Topic20Arrays_Part2.pptTopic20Arrays_Part2.ppt
Topic20Arrays_Part2.ppt
 
Chapter 13.pptx
Chapter 13.pptxChapter 13.pptx
Chapter 13.pptx
 
Arrays and library functions
Arrays and library functionsArrays and library functions
Arrays and library functions
 
Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016) Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016)
 
Arrays
ArraysArrays
Arrays
 
Arrays basics
Arrays basicsArrays basics
Arrays basics
 
7.basic array
7.basic array7.basic array
7.basic array
 
Chapter 3 ds
Chapter 3 dsChapter 3 ds
Chapter 3 ds
 

Mais de Soran University (8)

Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
Administrative
AdministrativeAdministrative
Administrative
 

Último

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdfKamal Acharya
 

Último (20)

Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 

Lecture 8

  • 1. Ar rays(I) Lecture 8 Dr. Hakem Beitollahi Computer Engineering Department Soran University
  • 2. Outline  Arrays  Declaring and allocating arrays  Examples using arrays Arrays(I)— 2
  • 4. Arrays (I)  An array is a group of contiguous memory locations that all have the same name and type  To refer to a particular location or element in the array, we specify the name of the array and the position number  Array indexes  Position number used to refer to a specific location/element  Also called subscript  Place in square brackets o Must be positive integer or integer expression  First element has index zero  Example (assume a = 5 and b = 6) o c[ a + b ] = c[11] = 2; Arrays(I)— 4
  • 6. Arrays (III) A -- -- -- -- -- -- -- -- -- -- A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] Arrays(I)— 6  Suppose int[] A = new int [10]; // array of 10 uninitialized ints  To access an individual element we must apply a subscript to list name A
  • 7. Arrays (IV) Arrays(I)— 7  Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; A -- -- -- -- -- -- -- -- -- -- A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
  • 8. Arrays (V) Arrays(I)— 8  Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; A 1 -- -- -- -- -- -- -- -- -- A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
  • 9. Arrays (VI) Arrays(I)— 9  Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; A 1 -- -- -- -- -- -- 5 -- -- A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
  • 10. Arrays (VII) Arrays(I)— 10  Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; A 1 -- 8 -- -- -- -- 5 -- -- A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
  • 11. Arrays (VIII) Arrays(I)— 11  Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; A 1 -- 8 6 -- -- -- 5 -- -- A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
  • 12. Arrays (IX) Arrays(I)— 12  Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; A 1 -- 8 6 -- -- -- 5 12 -- A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
  • 13. Arrays (X) Arrays(I)— 13  Consider int i = 7, j = 2, k = 4; A[0] = 1; A[i] = 5; A[j] = A[i] + 3; A[j+1] = A[i] + A[0]; A[A[j]] = 12; A 1 -- 8 6 3 -- -- 5 12 -- A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9]
  • 14. Common Programming Error 7.1  It is important to note the difference between the “seventh element of the array” and “array element seven.” Array subscripts begin at 0, thus the “seventh element of the array” has a subscript of 6, while “array element seven” has a subscript of 7 and is actually the eighth element of the array. Arrays(I)— 14
  • 15. Array Definition (I) Arrays(I)— 15
  • 16. Array Definition (II)  When arrays are allocated, the elements are initialized to zero for the numeric primitive data-type variables, to false for bool variables and to null for reference types.  Multiple declaration in single line:  In an array of value types, every element of the array contains one value of the declared type.  For example, every element of an int array is an int value. Arrays(I)— 16 string[] b = new string[ 100 ], x = new string[ 27 ]; double[] array1 = new double[ 10 ], array2 = new double[ 20 ];
  • 17. Examples Using Arrays Arrays(I)— 17
  • 18. Initializing the elements of an array  Example 1: Allocating an Array and Initializing Its Elements  The program creates three integer arrays of 10 elements and displays those arrays in tabular format. The program demonstrates several techniques for declaring and initializing arrays Arrays(I)— 18
  • 20. Summing the elements of an array  Example 2: Totaling the Elements of an Array  Often, the elements of an array represent series of values to be used in calculations.  Elements of an array represent the grades for an exam, the professor may wish to total the elements, then calculate the class average. Arrays(I)— 20
  • 22. Histogram Example  Example 3: Using Histograms to Display Array Data Graphically  Many programs present data to users in a graphical manner  Numeric values often are displayed as bars in a bar chart.  In such a chart, longer bars represent larger numeric values.  One simple way to display numeric data graphically is with a histogram that shows each numeric value as a bar of asterisks (*). Arrays(I)— 22
  • 23. Arrays(I)— 23 Show length of an array
  • 24. Use arrays as counters  Example 4: Using the Elements of an Array as Counters  Sometimes programs use a series of counter variables to summarize data, such as the results of a survey.  Calculate the number of occurrences of each side on a six-sided die Arrays(I)— 24